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)

hwnd = 0x260814
rect = win32gui.GetWindowRect(hwnd)
print("Window rect:", rect)

# Capture window
img = ImageGrab.grab(bbox=rect)
save_path = r'c:\Projects\JX\logs\launcher_view.png'
img.save(save_path)
print(f"Saved launcher screenshot to {save_path}")

# Enumerate child controls
children = []
def child_cb(ch, _):
    cls_buf = ctypes.create_unicode_buffer(256)
    user32.GetClassNameW(ch, cls_buf, 256)
    txt_len = user32.GetWindowTextLengthW(ch) + 1
    txt_buf = ctypes.create_unicode_buffer(txt_len)
    user32.GetWindowTextW(ch, txt_buf, txt_len)
    ctrl_id = user32.GetDlgCtrlID(ch)
    ch_rect = win32gui.GetWindowRect(ch)
    children.append((ch, ctrl_id, cls_buf.value, txt_buf.value, ch_rect))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumChildWindows(hwnd, WNDENUMPROC(child_cb), 0)
for ch, cid, cls, txt, r in children:
    print(f'{hex(ch)} | ID: {cid} | {cls} | "{txt}" | Rect: {r}')
