import ctypes, sys
from ctypes import wintypes
import win32gui, win32con, time
from PIL import ImageGrab
import psutil

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

h_btn = 0x27c0636
win32gui.SendMessage(h_btn, win32con.BM_CLICK, 0, 0)
time.sleep(0.8)

pids = [p.pid for p in psutil.process_iter(['pid', 'name']) if 'AutoVLBS' in (p.info['name'] or '')]
target_pid = pids[0]

hwnds = []
def cb(h, _):
    pid = wintypes.DWORD()
    user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
    if pid.value == target_pid and user32.IsWindowVisible(h):
        txt_len = user32.GetWindowTextLengthW(h) + 1
        buf = ctypes.create_unicode_buffer(txt_len)
        user32.GetWindowTextW(h, buf, txt_len)
        hwnds.append((h, buf.value))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows(WNDENUMPROC(cb), 0)

for h, title in hwnds:
    rect = win32gui.GetWindowRect(h)
    print(f'VLBS Window: {hex(h)}, title="{title}", rect={rect}')
    if rect[2] > rect[0] and rect[3] > rect[1]:
        img = ImageGrab.grab(bbox=rect)
        save_path = f'C:\\Users\\Admin\\.gemini\\antigravity\\brain\\5f637e30-06d3-43e8-a064-a259fea7be1a\\vlbs_buff_win_{hex(h)}.png'
        img.save(save_path)
        print(f'Saved {save_path}')
