55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
"""Bring WeChat main window to front and dump its UIA tree."""
|
|
import sys, time, ctypes
|
|
import win32gui, uiautomation as auto
|
|
|
|
def find_main():
|
|
hwnd = ctypes.windll.user32.FindWindowW('Qt51514QWindowIcon', None)
|
|
if hwnd:
|
|
return hwnd
|
|
hits = []
|
|
def cb(h, _):
|
|
cls = win32gui.GetClassName(h)
|
|
if cls in ('Qt51514QWindowIcon', 'WeChatMainWndForPC'):
|
|
hits.append(h)
|
|
return True
|
|
win32gui.EnumWindows(cb, None)
|
|
return hits[0] if hits else 0
|
|
|
|
def dump(ctrl, depth=0, max_depth=6, lines=None, max_lines=600):
|
|
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__':
|
|
hwnd = find_main()
|
|
print("hwnd:", hwnd)
|
|
if hwnd:
|
|
u = ctypes.windll.user32
|
|
u.ShowWindow(hwnd, 9)
|
|
u.SetForegroundWindow(hwnd)
|
|
time.sleep(1.2)
|
|
print("visible:", bool(u.IsWindowVisible(hwnd)), "rect:", win32gui.GetWindowRect(hwnd))
|
|
ctrl = auto.ControlFromHandle(hwnd)
|
|
lines = dump(ctrl, 0, int(sys.argv[1]) if len(sys.argv) > 1 else 6)
|
|
out = sys.argv[2] if len(sys.argv) > 2 else r'C:\Users\GuoAn\AppData\Local\Temp\wx_tree.txt'
|
|
with open(out, 'w', encoding='utf-8') as f:
|
|
f.write("\n".join(lines))
|
|
print(f"wrote {len(lines)} lines to {out}")
|