diff --git a/monitor.py b/monitor.py index 11f28e4..d8131df 100644 --- a/monitor.py +++ b/monitor.py @@ -168,19 +168,15 @@ class MessageMonitor: return None def _force_uia_refresh(self, platform: str): - """强制窗口重绘以触发 UIA 更新。""" + """通过交互强制窗口更新 UIA 控件树。""" + # 方式1: 向窗口发送非客户区点击(不影响焦点) hwnd = self._cached_hwnd.get(platform) - if not hwnd: - return - try: - rect = win32gui.GetWindowRect(hwnd) - # 缩小 1px 再恢复 → 强制重绘 - w, h = rect[2] - rect[0], rect[3] - rect[1] - win32gui.SetWindowPos(hwnd, 0, rect[0], rect[1], w + 1, h, 0x0002 | 0x0004) - win32gui.SetWindowPos(hwnd, 0, rect[0], rect[1], w, h, 0x0002 | 0x0004) - logger.debug(f"[{platform}] UIA强制刷新 (resize 1px)") - except Exception as e: - logger.debug(f"[{platform}] 强制刷新失败: {e}") + if hwnd: + try: + win32gui.PostMessage(hwnd, 0x00A1, 2, 0) # WM_NCLBUTTONDOWN, HTCAPTION + win32gui.PostMessage(hwnd, 0x00A2, 2, 0) # WM_NCLBUTTONUP + except Exception: + pass # ── 核心:读消息 ────────────────────────────────────────── @@ -233,6 +229,7 @@ class MessageMonitor: try: lst = window.ListControl(Name=name) if lst.Exists(maxSearchSeconds=0.2): + self._scroll_to_bottom(lst) texts = self._extract_last_line(lst) if texts: logger.debug(f"[S-A] ListControl({name}) → {len(texts)} items, last=\"{texts[-1][:50]}\"") @@ -246,7 +243,7 @@ class MessageMonitor: try: lst = window.ListControl(Name=name) if lst.Exists(maxSearchSeconds=0.2): - texts = self._extract_second_line(lst) + self._scroll_to_bottom(lst) if texts: logger.debug(f"[S-B] 会话({name}) → {len(texts)} items, last=\"{texts[-1][:50]}\"") return texts @@ -254,6 +251,15 @@ class MessageMonitor: pass return [] + def _scroll_to_bottom(self, lst): + """滚动 ListControl 到底部,触发应用加载新消息。""" + try: + scroll = lst.GetScrollPattern() + if scroll: + scroll.SetScrollPercent(100, 0) + except Exception: + pass + @staticmethod def _extract_last_line(lst) -> list: texts = []