我刚刚编写了简单的脚本来启动应用程序,并且我正在尝试使用“SendKeys”模块将击键发送到该应用程序。有一个“快照”按钮,但我无法让 Python 单击“快照”按钮,因为新窗口没有焦点。所以我计划使用 Win32gui 模块的 win32gui.FindWindow
和 win32gui.SetForegroundWindow
功能。但它给了我错误-无效句柄
。我的应用程序名称是“DMCap”
这是 Python 中的代码片段:
handle = win32gui.FindWindow(0, "DMCap") //paassing 0 as I dont know classname
win32gui.SetForegroundWindow(handle) //put the window in foreground
有人可以帮助我吗?这段Python代码正确吗?我可以像这样直接发送句柄吗?
最佳答案
如果确实有一个标题为“DMCap”的窗口,您的代码应该按原样运行良好。要获取句柄和标题列表,请运行以下代码:
import win32gui
def window_enum_handler(hwnd, resultList):
if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
def get_app_list(handles=[]):
mlst=[]
win32gui.EnumWindows(window_enum_handler, handles)
for handle in handles:
mlst.append(handle)
return mlst
appwindows = get_app_list()
for i in appwindows:
print i
这将生成一个包含句柄、标题对的元组列表。
关于Python:win32gui.SetForegroundWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16770909/