75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
"""Wait for WeChat main window (post-login) and dump its UIA tree."""
|
|
import sys, time, ctypes, json
|
|
import win32gui, win32process, psutil, uiautomation as auto
|
|
|
|
def weixin_pids():
|
|
pids = set()
|
|
for p in psutil.process_iter(['pid', 'name']):
|
|
try:
|
|
if p.info['name'] and p.info['name'].lower() == 'weixin.exe':
|
|
pids.add(p.info['pid'])
|
|
except Exception:
|
|
pass
|
|
return pids
|
|
|
|
def windows():
|
|
rows = []
|
|
def cb(h, _):
|
|
_, pid = win32process.GetWindowThreadProcessId(h)
|
|
if pid in weixin_pids():
|
|
rect = win32gui.GetWindowRect(h)
|
|
rows.append(dict(hwnd=h, pid=pid, cls=win32gui.GetClassName(h),
|
|
title=win32gui.GetWindowText(h),
|
|
w=rect[2] - rect[0], h=rect[3] - rect[1]))
|
|
return True
|
|
win32gui.EnumWindows(cb, None)
|
|
return rows
|
|
|
|
def dump(ctrl, depth=0, max_depth=8, lines=None, max_lines=1500):
|
|
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 = "?"
|
|
kids = []
|
|
try:
|
|
child = ctrl.GetFirstChildControl()
|
|
while child:
|
|
kids.append(child)
|
|
child = child.GetNextSiblingControl()
|
|
except Exception:
|
|
pass
|
|
lines.append(" " * depth + f"[{ctrl.ControlTypeName}] name={ctrl.Name!r} aid={ctrl.AutomationId!r} cls={ctrl.ClassName!r} n={len(kids)} {rect}")
|
|
for child in kids:
|
|
dump(child, depth + 1, max_depth, lines, max_lines)
|
|
return lines
|
|
|
|
if __name__ == '__main__':
|
|
deadline = time.time() + int(sys.argv[1] if len(sys.argv) > 1 else 600)
|
|
out = sys.argv[2] if len(sys.argv) > 2 else r'C:\Users\GuoAn\AppData\Local\Temp\wx_main_tree.txt'
|
|
target = None
|
|
while time.time() < deadline:
|
|
for w in windows():
|
|
if w['w'] >= 600 and 'Login' not in w['cls']:
|
|
target = w
|
|
break
|
|
if target:
|
|
break
|
|
time.sleep(5)
|
|
if not target:
|
|
print("TIMEOUT: main window not found")
|
|
sys.exit(2)
|
|
print("main window:", json.dumps(target, ensure_ascii=False))
|
|
u = ctypes.windll.user32
|
|
u.ShowWindow(target['hwnd'], 9)
|
|
u.SetForegroundWindow(target['hwnd'])
|
|
time.sleep(1.5)
|
|
ctrl = auto.ControlFromHandle(target['hwnd'])
|
|
lines = dump(ctrl)
|
|
with open(out, 'w', encoding='utf-8') as f:
|
|
f.write("\n".join(lines))
|
|
print(f"wrote {len(lines)} lines to {out}")
|