import ctypes, sys
from ctypes import wintypes
import win32gui
from PIL import ImageGrab

sys.stdout.reconfigure(encoding='utf-8')
user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
    user32.SetThreadDesktop(hdesk)

windows = [
    (0xdf137a, 53668, "game_53668.png"),
    (0x3050512, 48784, "game_48784.png"),
]

for hwnd, pid, fname in windows:
    if user32.IsWindow(hwnd):
        rect = win32gui.GetWindowRect(hwnd)
        print(f"HWND {hex(hwnd)} (PID {pid}) rect: {rect}")
        if rect[2] > rect[0] and rect[3] > rect[1]:
            img = ImageGrab.grab(bbox=rect)
            save_path = fr'c:\Projects\JX\logs\{fname}'
            img.save(save_path)
            print(f"Saved {save_path}")
