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

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

windows = []
def enum_cb(hwnd, _):
    if win32gui.GetClassName(hwnd) == "Sword3 Class":
        _, pid = win32process.GetWindowThreadProcessId(hwnd)
        title = win32gui.GetWindowText(hwnd)
        rect = win32gui.GetWindowRect(hwnd)
        windows.append((hwnd, pid, title, rect))

win32gui.EnumWindows(enum_cb, None)
print(f"Total Sword3 Class windows found: {len(windows)}")
for i, (hwnd, pid, title, rect) in enumerate(windows):
    print(f"[{i}] HWND: 0x{hwnd:08X} ({hwnd}), PID: {pid}, Title: '{title}', Rect: {rect}")
    try:
        img = ImageGrab.grab(bbox=rect, all_screens=True)
        path = f"c:\\Projects\\JX\\logs\\current_game_win_{i}_pid_{pid}.png"
        img.save(path)
        print(f"    Saved screenshot: {path}")
    except Exception as e:
        print(f"    Screenshot error: {e}")
