import win32gui, win32process, win32api
from PIL import ImageGrab
import os, ctypes

user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
    user32.SetThreadDesktop(hdesk)

results = []
def enum_cb(hwnd, _):
    _, pid = win32process.GetWindowThreadProcessId(hwnd)
    try:
        cname = win32gui.GetClassName(hwnd)
        title = win32gui.GetWindowText(hwnd)
        rect = win32gui.GetWindowRect(hwnd)
        results.append((hwnd, pid, cname, title, rect))
    except Exception as e:
        pass

win32gui.EnumWindows(enum_cb, None)
pids_of_interest = [30076, 32572]
matched = [x for x in results if x[1] in pids_of_interest or x[2] == "Sword3 Class"]
print(f"Matched {len(matched)} windows:")
for h, p, c, t, r in matched:
    print(f"  HWND: 0x{h:08X} ({h}), PID: {p}, Class: {c}, Rect: {r}, Title: '{t}'")
    try:
        img = ImageGrab.grab(bbox=r, all_screens=True)
        path = f"c:\\Projects\\JX\\logs\\win_0x{h:08X}_pid{p}.png"
        img.save(path)
        print(f"  Saved screenshot: {path}")
    except Exception as e:
        print(f"  Error grabbing screenshot: {e}")

