python 使用pywin32库 查找 子窗口句柄
要使用pywin32库查找子窗口句柄,你可以使用以下步骤:
- 导入所需的模块:
import win32gui
import win32con
- 定义一个回调函数,该函数将在每个窗口枚举时被调用,以判断是否为目标窗口:
def enum_callback(hwnd, lParam):
# 判断窗口是否为目标窗口
if win32gui.GetClassName(hwnd) == "目标窗口类名":
lParam.append(hwnd)
- 枚举所有顶层窗口,并在回调函数中判断是否为目标窗口:
def find_child_windows(parent_hwnd):
# 存储子窗口句柄的列表
child_hwnds = []
# 枚举所有顶层窗口
win32gui.EnumChildWindows(parent_hwnd, enum_callback, child_hwnds)
return child_hwnds
- 在主程序中调用上述函数,找到目标窗口的子窗口句柄:
# 获取父窗口句柄
parent_hwnd = win32gui.FindWindow(None, "父窗口标题")
# 查找子窗口句柄
child_hwnds = find_child_windows(parent_hwnd)
# 输出子窗口句柄
for hwnd in child_hwnds:
print(hwnd)
请注意替换代码中的"目标窗口类名"、"父窗口标题"等为实际的窗口类名和窗口标题
原文地址: http://www.cveoy.top/t/topic/iXr3 著作权归作者所有。请勿转载和采集!