python 遍历 windows 窗口

想要判断一个应用程序是否已经打开,我们可以查找它的应用窗口名字,然后进行判断

import win32gui


collected = {}


def get_windows(hd, arg):
    global collected
    if win32gui.IsWindow(hd) and win32gui.IsWindowVisible(hd) and win32gui.IsWindowEnabled(hd):
        text = win32gui.GetWindowText(hd)  # 通过句柄,获取每个窗口的标题
        if text:
            collected[hd] = text

# EnumWindows(callback, arg); 第二个参数会传递给 callback 的第二个参数。callback 的第一个参数是 hwnd 句柄
# callback 如果返回了 False 或引发了异常,会停止遍历窗口
win32gui.EnumWindows(get_windows, 0)

for k, v in collected.items():
    print(k, v, sep=' | ')
原文地址:https://www.cnblogs.com/wztshine/p/15800973.html