"""Probe WeChat (Weixin 4.x) windows and their UI Automation tree.""" import sys, ctypes, json import win32gui, win32process def list_windows(): out = [] def cb(hwnd, _): if not win32gui.IsWindowVisible(hwnd): return True title = win32gui.GetWindowText(hwnd) cls = win32gui.GetClassName(hwnd) _, pid = win32process.GetWindowThreadProcessId(hwnd) try: import psutil name = psutil.Process(pid).name() except Exception: name = '?' out.append(dict(hwnd=hwnd, cls=cls, title=title, pid=pid, proc=name, rect=win32gui.GetWindowRect(hwnd))) return True win32gui.EnumWindows(cb, None) return out def dump(ctrl, depth=0, max_depth=5, lines=None, max_lines=400): lines = lines if lines is not None else [] if depth > max_depth or len(lines) > max_lines: return lines try: r = ctrl.BoundingRectangle rect = f"({r.left},{r.top},{r.right - r.left}x{r.bottom - r.top})" except Exception: rect = "?" child = ctrl.GetFirstChildControl() nchild = 0 while child: nchild += 1 child = child.GetNextSiblingControl() lines.append(" " * depth + f"[{ctrl.ControlTypeName}] name={ctrl.Name!r} aid={ctrl.AutomationId!r} cls={ctrl.ClassName!r} children={nchild} {rect}") child = ctrl.GetFirstChildControl() while child: dump(child, depth + 1, max_depth, lines, max_lines) child = child.GetNextSiblingControl() return lines if __name__ == '__main__': mode = sys.argv[1] if len(sys.argv) > 1 else 'list' if mode == 'list': for w in list_windows(): print(json.dumps(w, ensure_ascii=False)) else: hwnd = int(sys.argv[2]) depth = int(sys.argv[3]) if len(sys.argv) > 3 else 5 out = sys.argv[4] if len(sys.argv) > 4 else None import uiautomation as auto ctrl = auto.ControlFromHandle(hwnd) lines = dump(ctrl, 0, depth) text = "\n".join(lines) if out: with open(out, 'w', encoding='utf-8') as f: f.write(text) print(f"wrote {len(lines)} lines to {out}") else: print(text)