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)

pid_target = 52500
hwnds = []
def cb(h, _):
    pid = wintypes.DWORD()
    user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
    if pid.value == pid_target:
        cls_buf = ctypes.create_unicode_buffer(256)
        user32.GetClassNameW(h, cls_buf, 256)
        txt_len = user32.GetWindowTextLengthW(h) + 1
        txt_buf = ctypes.create_unicode_buffer(txt_len)
        user32.GetWindowTextW(h, txt_buf, txt_len)
        vis = user32.IsWindowVisible(h)
        hwnds.append((h, cls_buf.value, txt_buf.value, vis))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows(WNDENUMPROC(cb), 0)
for h, cls, txt, vis in hwnds:
    print(f'HWND: {hex(h)}, Class: {cls}, Text: "{txt}", Visible: {vis}')
    rect = win32gui.GetWindowRect(h)
    img = ImageGrab.grab(bbox=rect)
    img.save(r'c:\Projects\JX\logs\jxhelper_restarted_ui.png')
    print("Saved jxhelper_restarted_ui.png")
