EnumChildWindows无法在pywin32中运行

时间:2012-09-03 20:48:42

标签: python winapi pywin32

我想让特定IE实例的子节点看看是否有弹出窗口。

我制作了一个弹出窗口的.html页面。弹出窗口的标题是“来自网页的消息”,因为它始终适用于此版本的IE。

我可以从子窗口获取父级:

>>> 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'

但是,似乎我无法从父级获取子窗口:

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

为什么会这样?

1 个答案:

答案 0 :(得分:9)

确实为每个孩子调用了处理程序:

>>> 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...

只是EnumChildWindows本身不会返回任何内容。如果要在列表中包含所有子窗口句柄,请在处理程序中执行此操作:

>>> 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]
>>>