Windows窗口句柄监控

我说想要一段实现了 当发现windows桌面上冒出新窗口就截图保存的功能,AI写的,测了下能用。

# pip install pyautogui pywin32
import time
import win32gui
import pyautogui

def capture_window_screenshot(hwnd):
    """截取截图"""
    screenshot = pyautogui.screenshot()
    screenshot.save(f"screenshot_{hwnd}.png")

def monitor_new_windows():
    # 存储已知窗口句柄
    known_hwnds = set()  
    all_hwnds = []
    
    def enum_callback(hwnd, _):
        all_hwnds.append(hwnd)
    
    win32gui.EnumWindows(enum_callback, None)
    for hwnd in set(all_hwnds):
        known_hwnds.add(hwnd)
    
    flag = 0
    while True:
        all_hwnds = []
        flag = 0
        win32gui.EnumWindows(enum_callback, None)
        # 检查新出现的窗口
        new_hwnds = set(all_hwnds) - known_hwnds
        for hwnd in new_hwnds:
            known_hwnds.add(hwnd)
            flag = 1

        if flag == 1:
            capture_window_screenshot(hwnd)

        time.sleep(180)

if __name__ == "__main__":
    monitor_new_windows()

我倒要看看工位电脑每晚是怎么自动关机的

posted @ 2024-04-15 15:39  anyiya  阅读(27)  评论(0编辑  收藏  举报