win32gui 显示一些未打开的 windows

win32gui shows some windows, that are not open

我是第一次使用 win32gui 包。我找到了以下示例来打印所有打开的 windows。

但我想知道 log_app_list() 函数包含未打开的 windows。例如 'Microsoft Store' 和 'Einstellungen'(德语中的设置)。有人可以向我解释这种意外行为吗?

import win32gui

def window_enum_handler(hwnd, resultList):
    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
        resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def get_app_list(handles=[]):
    mlst=[]
    win32gui.EnumWindows(window_enum_handler, handles)
    for handle in handles:
        mlst.append(handle)
    return mlst

def log_app_list():
    appwindows = get_app_list()
    for i in appwindows:
        print(i)

log_app_list()

取决于后台进程的状态,这些windows不是"unopened",而是进程处于suspended状态,可以在任务中查看manager,进程"WinStore.App.exe"和"SystemSettings.exe"处于Suspended状态:

您可以将 DwmGetWindowAttributeDWMWA_CLOAKED 结合使用来隐藏其 属性,以排除它们:

import win32gui
import ctypes
from ctypes import c_int
import ctypes.wintypes
from ctypes.wintypes import HWND, DWORD
dwmapi = ctypes.WinDLL("dwmapi")
DWMWA_CLOAKED = 14 
isCloacked = c_int(0)
def window_enum_handler(hwnd, resultList):
    if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
        dwmapi.DwmGetWindowAttribute(HWND(hwnd), DWORD(DWMWA_CLOAKED), ctypes.byref(isCloacked), ctypes.sizeof(isCloacked))
        if(isCloacked.value == 0):
            resultList.append((hwnd, win32gui.GetWindowText(hwnd)))

def get_app_list(handles=[]):
    mlst=[]
    win32gui.EnumWindows(window_enum_handler, handles)
    for handle in handles:
        mlst.append(handle)
    return mlst

def log_app_list():
    appwindows = get_app_list()
    for i in appwindows:
        print(i)

log_app_list()