# Screenshot an existing window by handle (decimal), dump tree, pixel stats. import ctypes, ctypes.wintypes as w, subprocess, sys, os sys.stdout.reconfigure(encoding='utf-8', errors='replace') user32 = ctypes.windll.user32 hwnd = int(sys.argv[1], 16) if len(sys.argv) > 1 else 0 out = sys.argv[2] if len(sys.argv) > 2 else "probe_win.png" def dump_tree(hwnd, depth=0): out = [] buf = ctypes.create_unicode_buffer(256) user32.GetClassNameW(hwnd, buf, 256) cls = buf.value user32.GetWindowTextW(hwnd, buf, 256) title = buf.value r = w.RECT() user32.GetWindowRect(hwnd, ctypes.byref(r)) vis = user32.IsWindowVisible(hwnd) out.append(f"{' '*depth}h={hwnd:08X} cls='{cls}' title='{title}' rect=({r.left},{r.top},{r.right},{r.bottom}) vis={vis}") children = [] @ctypes.WINFUNCTYPE(w.BOOL, w.HWND, w.LPARAM) def cb(child, lp): children.append(child) return True user32.EnumChildWindows(hwnd, cb, 0) for c in children: out.extend(dump_tree(c, depth + 1)) return out print("--- window tree ---") for ln in dump_tree(hwnd): print(ln) r = w.RECT() user32.GetWindowRect(hwnd, ctypes.byref(r)) wdt, hgt = r.right - r.left, r.bottom - r.top print("size", wdt, hgt) ps = f""" Add-Type -AssemblyName System.Drawing $bmp = New-Object System.Drawing.Bitmap({wdt}, {hgt}) $g = [System.Drawing.Graphics]::FromImage($bmp) $g.CopyFromScreen({r.left}, {r.top}, 0, 0, $bmp.Size) $bmp.Save('C:/Users/locpham/SonicForgeStudio/native_bridge/debug/{out}') $g.Dispose(); $bmp.Dispose() """ subprocess.run(["powershell", "-NoProfile", "-Command", ps], check=False) print("saved", out)