EnumChildWindows not working in pywin32

a 夏天 提交于 2019-12-22 08:05:25

问题


I want to get the children of a particular IE instance to see if there are any popups.

I've made an .html page that pops up a window. The title of the popup is "Message from webpage", as it always is for this version of IE.

I can get the parent from the child window:

>>> child_handle = 15208472
>>> win32gui.GetWindowText(child_handle)
'Message from webpage'
>>> win32gui.GetParent(child_handle)
33230502
>>> parent_handle = 33230502
>>> win32gui.GetWindowText(parent_handle)
'pop-up example - Windows Internet Explorer'

However, it seems that i can't get the child window from the parent:

>>> def all_ok(hwnd, param): return True
>>> win32.EnumChildWindows(parent_handle, all_ok, None)
>>>

Why is this?


回答1:


The handler is indeed called for each child:

>>> def all_ok(hwnd, param):
...     print hwnd
...     return True
...

>>> win32gui.EnumChildWindows(parent_handle, all_ok, None)
17630538
12911940
8260536
4131432
14356400
11471888
9048526
18942076
8523526
#etc...

It's just that EnumChildWindows itself doesn't return anything. If you want to have all of the child window handles in a list, do that in the handler:

>>> parent_handle = 33230502
>>> child_handles = []
>>> def all_ok(hwnd, param):
...     child_handles.append(hwnd)
...
>>> win32gui.EnumChildWindows(parent_handle, all_ok, None)
>>> child_handles
[17630538, 12911940, 8260536, 4131432, 14356400, 11471888, 9048526, 18942076, 8523526, 6951400, 5968556, 19203900, 4459544, 15208240, 9700614, 5769012, 11277176, 7409598, 10225510, 8392342, 19270296, 32377256, 7276984, 20449052, 8262502, 11734380, 14749460, 5310608, 3935978, 125374254, 8457268, 2621704, 24840652, 5706936, 35261636, 10357170, 5641372, 8260848, 6559366]
>>>


来源:https://stackoverflow.com/questions/12253858/enumchildwindows-not-working-in-pywin32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!