此代码显示具有一定长度的WindowsText的EditPlus子窗口的hwnd:
您必须找到应用程序的hwnd,然后将此句柄与EnumChildWindows一起使用。我用它扩展了示例代码。一旦得到应用程序hwnd,就只能枚举其窗口。当您将0作为hwnd给EnumChildWindows时,您将获得所有运行窗口的句柄。在我的代码中添加一些指纹并检查它!
扩展代码:
import win32gui
MAIN_HWND = 0
def is_win_ok(hwnd, starttext):
s = win32gui.GetWindowText(hwnd)
if s.startswith(starttext):
print s
global MAIN_HWND
MAIN_HWND = hwnd
return None
return 1
def find_main_window(starttxt):
global MAIN_HWND
win32gui.EnumChildWindows(0, is_win_ok, starttxt)
return MAIN_HWND
def winfun(hwnd, lparam):
s = win32gui.GetWindowText(hwnd)
if len(s) > 3:
print("winfun, child_hwnd: %d txt: %s" % (hwnd, s))
return 1
def main():
main_app = 'EditPlus'
hwnd = win32gui.FindWindow(None, main_app)
print hwnd
if hwnd < 1:
hwnd = find_main_window(main_app)
print hwnd
if hwnd:
win32gui.EnumChildWindows(hwnd, winfun, None)
main()