我目前正在尝试从subprocess.Popen
开始的进程中获取所有窗口句柄,如下所示:
def openMP():
mp_p = os.path.abspath("C:\Program Files (x86)\Mobile Partner\Mobile Partner.exe")
mp = subprocess.Popen(mp_p, stdin=None, stdout=None, stderr=None, close_fds=True)
return mp.pid
pid = openMP()
wins = get_hwnds_for_pid(pid)
for win in wins:
print(win, "=>", win32gui.GetWindowText(win))
这正在使用此功能(found here):
def get_hwnds_for_pid (pid):
def callback (hwnd, hwnds):
if win32gui.IsWindowVisible (hwnd) and win32gui.IsWindowEnabled (hwnd):
_, found_pid = win32process.GetWindowThreadProcessId (hwnd)
if found_pid == pid:
hwnds.append (hwnd)
return True
hwnds = []
win32gui.EnumWindows (callback, hwnds)
return hwnds
这种类型的作品,但不是我想要的那样。
它返回一个hwnd
,我感觉它缺少现有的子窗口。
我已经使用AutoIts WinList
和一个类似下面的简单正则表达式对相同的pid
进行了测试:
;THIS IS AUTOIT CODE
Local $aWinList = WinList("[REGEXPTITLE:(?i)(.*Mobile Partner.*)]")
,它的确返回三个而不是一个hwnd
。
我希望我的python代码也能获得这三个句柄。
我已经使用win32.guiEnumChildWindows
进行了测试,但似乎无法正常工作:
parent = hwnd
childs = []
def callback(hwnd,isx):
print(hwnd)
print(isx)
childs.append(hwnd)
ax = win32gui.EnumChildWindows(parent,callback,None)
print(ax)
for child in childs:
print(child)
不幸的是,这只会返回:
pywintypes.error: (2, 'EnumChildWindows', 'The system cannot find the file specified.')
我真的不知道为什么。我知道这很复杂,但是也许您的聪明才智可以帮助我。
再次总结一下:
我正在寻找一种在Windows 10计算机上使用python(不必是nativ)查找由已知进程ID产生的每个窗口句柄的方法。
它应该返回与AutoIts WinList
相同的句柄。