SetForegroundWindow() 在服务器的 RDP 窗口最小化或锁定时不运行

Pra*_*bal 0 python window win32gui pywinauto setforegroundwindow

我正在从事一个自动化项目,该项目要求我搜索 acrobat 进程,然后将其置于前台。我使用的是 Windows Server 2012 R2 Standard 和 Python。我在进程列表中搜索该特定 PDF 文件,然后尝试将该窗口置于前台。

当服务器打开并处于活动状态时,我的代码运行良好。一旦它被最小化或锁定,它就不起作用。具体来说,SetForegroundWindow() 行不起作用并抛出错误“(0, 'SetForegroundWindow', 'No error message is available')”。

接下来,我浏览了此处列出的许多示例,有人建议在该行之前使用“Alt”选项卡。试过了,没用。然后我根据另一个用户使用 ShowWindow() 但又出现了同样的错误。现在,我完全被困住了。

我已经查看了许多与该问题相关的线程,例如: 1. SetForegroundWindow 不适用于最小化进程 2.最小化时前移窗口 3.最小化时前移窗口

我试过 MainWindowHandle 可以修复错误,但我无法在 python 中实现它。

另外,我安装了 pywinauto 并使用了 pywinauto 的 findwindows 和 SetForegroundWindow 功能。当服务器处于活动状态时它运行良好,但是一旦我们最小化或锁定服务器,它就不起作用并给出相同的错误“(0, 'SetForegroundWindow', 'No error message is available')”。

现在,我完全没有选择。任何想法/建议?

这是片段:

import win32gui,win32con
import win32com.client
import time

time.sleep(3)
def windowEnumerationHandler(hwnd, top_windows):
    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd)!='':
        top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))

results = []
top_windows = []
file_name_of_pdf = "ARW_AR_2016.pdf"
win32gui.EnumWindows(windowEnumerationHandler, top_windows)

for i in top_windows:

 if i[1].find(file_name_of_pdf[:len(file_name_of_pdf)-4])>-1:

    print(i)
    shell = win32com.client.Dispatch("WScript.Shell")
    win32gui.ShowWindow(i[0],9)
    try:
        shell.SendKeys('%')
        win32gui.ShowWindow(i[0], win32con.SW_RESTORE)                                 
        win32gui.SetForegroundWindow(i[0])            
    except Exception as e:
        print(1,e)
        try:
            shell.SendKeys('%')                
            win32gui.SetForegroundWindow(i[0])
        except Exception as r:
            print(2,r)
            pass 
Run Code Online (Sandbox Code Playgroud)

Vas*_*bov 5

远程执行指南应解释一切,你可以用这个局面做。