过滤后台进程 PyWin32

我一直致力于从 EnumWindows 中过滤掉窗口,仅包括最小化或打开到列表的窗口。


代码


def winEnumHandler(hwnd, ctx):

    title = win32gui.GetWindowText(hwnd)


    # Append HWND to list

    if win32gui.IsWindowVisible(hwnd) and title != '':

        app = ApplicationWindow(hwnd, title)

        applications.append(app)



def scanApplication():

    applications.clear()

    win32gui.EnumWindows(winEnumHandler, None)

    return applications

预期/实际


此代码的问题在于,它无法正确过滤掉通过以下方式找到的一些窗口EnumWindows:例如,目前我在计算机上打开了 Chrome、IDE 和 Discord,并且只期望这些窗口出现在应用程序列表中。但是,我不仅获得这些窗口,还获得后台任务,例如:计算器、邮件、Geforce Overlay 等...这些后台任务处于活动状态,但桌面上没有窗口,也没有最小化。我怎样才能过滤掉后台任务EnumWindows?谢谢阅读!


杨魅力
浏览 49回答 1
1回答

有只小跳蛙

在做了更多研究后,我遇到了 DWM,DWM 提供了一种可以查找窗口属性以从中获取更多信息的方法。其中一个选项称为斗篷,它可以很好地过滤掉所有窗口中的后台进程。我的代码如下。def winEnumHandler(hwnd, ctx):    # DWM    isCloacked = ctypes.c_int(0)    ctypes.WinDLL("dwmapi").DwmGetWindowAttribute(hwnd, 14, ctypes.byref(isCloacked), ctypes.sizeof(isCloacked))    # Variables    title = win32gui.GetWindowText(hwnd)    # Append HWND to list    if win32gui.IsWindowVisible(hwnd) and title != '' and isCloacked.value == 0:        app = ApplicationWindow(hwnd, title)        applications.append(app)DWM 过滤:此处从该链接获取更多信息后,最终解决方案显示了所有真实的窗口:class TITLEBARINFO(ctypes.Structure):    _fields_ = [("cbSize", ctypes.wintypes.DWORD), ("rcTitleBar", ctypes.wintypes.RECT),                ("rgstate", ctypes.wintypes.DWORD * 6)]def winEnumHandler(hwnd, ctx):    # Title Info Initialization    title_info = TITLEBARINFO()    title_info.cbSize = ctypes.sizeof(title_info)    ctypes.windll.user32.GetTitleBarInfo(hwnd, ctypes.byref(title_info))    # DWM Cloaked Check    isCloaked = ctypes.c_int(0)    ctypes.WinDLL("dwmapi").DwmGetWindowAttribute(hwnd, 14, ctypes.byref(isCloaked), ctypes.sizeof(isCloaked))    # Variables    title = wg.GetWindowText(hwnd)    # Append HWND to list    if wg.IsWindowVisible(hwnd) and title != '' and isCloaked.value == 0:        if not (title_info.rgstate[0] & wc.STATE_SYSTEM_INVISIBLE):            app = ApplicationWindow(hwnd, title)            applications.append(app)任何简化请告诉我!谢谢!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python