From d7e50675f0179e2202ff624337e3511e321aeb87 Mon Sep 17 00:00:00 2001 From: zimoyin <2556608754@qq.com> Date: Tue, 13 Oct 2026 16:41:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A7=E4=BB=B6=E5=B1=9E=E6=80=A7=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=86=85=E5=AE=B9=E6=96=87=E6=9C=AC=EF=BC=9B=E6=8B=BE?= =?UTF-8?q?=E5=8F=96=E5=91=BD=E4=B8=AD=E5=AE=B9=E5=99=A8=E6=97=B6=E5=90=91?= =?UTF-8?q?=E6=B7=B1=E5=A4=84=E6=94=B6=E6=95=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户反馈两点: 1) 控件属性里也要看到内容(输入框/富文本/多行编辑器的正文) - UiaElementInfo 新增只读展示字段「文本内容」(不序列化,选中节点时现算) - 与取值键「文本内容」同源:Value → Text → Name - 实测(真实 Windows 11 记事本文档节点): 文本内容="agfa\r\raaaaa",Name="文本编辑器"(通用标题) ControlPath=/Window[1]/Pane[@ClassName='NotepadTextBox']/Document[@ClassName='RichEditD2DPT'] 2) 拾取不能正确识别(实测:记事本正文拾取到 Pane[NotepadTextBox] 而不是里面的 Document[RichEditD2DPT]) - 根因:提供者的命中测试经常先返回"容器",代码直接接受了它 - FromHandleAtPoint 命中后新增 RefineHitAtPoint:在命中元素内部继续往 "包含光标的最深/最小元素"收敛(有界:节点数/深度/3 秒) - MainForm 的"只拿到窗口再重探一次"扩展到"只拿到容器(Window/Pane/Group/Custom) 也重探一次":无障碍树按需生成的客户端第一次通常只给容器,600ms 后第二次才给到叶子 实测(真实记事本窗口内 49 个采样点): 42 × ControlType.Document / RichEditD2DPT(正文),其余为工具栏按钮/输入法宿主 修复前同一组采样中正文区域会命中 Pane 回归 27/27 通过 --- docs/代码导览.md | 2 ++ src/pacs_win_uia/Models/UiaElementInfo.cs | 11 +++++++ .../Services/UiAutomationService.cs | 28 +++++++++++++++++- src/pacs_win_uia/UI/MainForm.cs | 29 +++++++++++++++---- 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/docs/代码导览.md b/docs/代码导览.md index b4cc1fb..93667a7 100644 --- a/docs/代码导览.md +++ b/docs/代码导览.md @@ -48,6 +48,7 @@ MainForm(UI/MainForm.cs) 三栏主界面:窗口列表 | 控件树/列 |---|---|---| | 主界面布局/按钮/三栏比例 | `UI/MainForm.cs` | 布局自检 `VerifyLayout()` 会报错,改动后看日志 | | 控件树怎么显示、要不要闪烁 | `UI/ControlTreeReconciler.cs` | 增量更新,绝不清空重建 | +| 拾取为什么选到容器 | `Services/UiAutomationService.cs` 的 `FromHandleAtPoint` | 命中后向深处收敛 | | 控件树读取来源与顺序 | `Services/ControlTreeService.cs` | Auto = UIA → MSAA → Win32 | | UIA 快照/缓存/Pattern | `Services/UiAutomationService.cs` | 统一 RawView + CacheRequest | | 列表读取(行列/单元格/降级) | `Services/ListReaderService.cs` | 最长的文件,见其顶部导览 | @@ -81,6 +82,7 @@ MainForm(UI/MainForm.cs) 三栏主界面:窗口列表 | 控件树/列 - **不要"取第一个匹配"**:不确定就报错,让人重新拾取。 - **PropertyGrid 正在编辑时不要去 rebind**:会打断用户输入,`MainForm` 里有专门判断。 - **悬浮条的"名称"不是定位条件**:定位条件是进程名/窗口类/标题关键词 + 可选导入 XPath。 +- **拾取到"容器"要往深处收敛**:提供者的命中测试经常先给容器(实测 Windows 11 记事本正文返回 `Pane[NotepadTextBox]`),拾取会在命中后往下找"包含光标的更深控件",并且只拿到容器时会在 600ms 后自动重探一次。改这块看 `UiAutomationService.FromHandleAtPoint` 与 `MainForm.ScheduleDeeperPickRetry`。 - **输入框/富文本的内容常常不在 `Name` 里**:WinForms 文本框的 `Name` 就是内容,但 WinForms 富文本、Chromium/CEF、UWP 文本框的 `Name` 可能是通用标题(例如 `RichEdit Control`),内容只在 `Value`/`Text` 模式里。取内容请用跨来源键 **`文本内容`**(顺序 Value → Text → Name,MSAA 侧 = accValue → accName,Win32 侧 = 窗口标题);新增/拾取控件时它已经是默认的 valueKey。 - **旧路径格式已停用**:`[Uia] A[id] > B:name`、`Type[id]` 之类不再执行,只保留给人工编辑修正。 - **本工具自己的窗口不算遮挡**:主界面/设置窗口压住目标不影响悬浮条显示(否则在设置页里根本测不了)。 diff --git a/src/pacs_win_uia/Models/UiaElementInfo.cs b/src/pacs_win_uia/Models/UiaElementInfo.cs index 3a18406..42f68ad 100644 --- a/src/pacs_win_uia/Models/UiaElementInfo.cs +++ b/src/pacs_win_uia/Models/UiaElementInfo.cs @@ -55,6 +55,17 @@ namespace pacs_win_uia.Models // ControlPath:显示用——从顶层窗口到该控件的 UIA 路径(与监控保存同格式,可粘去「按 path 定位」验证),不序列化。 public string ControlPath { get; set; } + /// + /// 展示用:该控件的内容文本(Value → Text → Name)。不序列化,展示时现算。 + /// 输入框/富文本/记事本正文的内容都在这里,而 Name 往往只是通用标题(例如 RichEdit Control)。 + /// + [IgnoreDataMember] + [Browsable(true)] + [ReadOnly(true)] + [Description("控件内容文本(Value → Text → Name)。输入框、富文本、多行编辑器要读的就是它;" + + "Name 可能只是通用标题。取值键「文本内容」读的就是同一个来源。")] + public string TextContent { get; set; } + [IgnoreDataMember] [Browsable(true)] [ReadOnly(true)] diff --git a/src/pacs_win_uia/Services/UiAutomationService.cs b/src/pacs_win_uia/Services/UiAutomationService.cs index b8aeb4b..3ce153f 100644 --- a/src/pacs_win_uia/Services/UiAutomationService.cs +++ b/src/pacs_win_uia/Services/UiAutomationService.cs @@ -265,7 +265,11 @@ namespace pacs_win_uia.Services AutomationElement hit = AutomationElement.FromPoint(new System.Windows.Point(point.X, point.Y)); if (BelongsToWindow(hit, root, handle)) { - UiaElementInfo snapshot = Inspect(hit); + // 提供者的命中测试经常只给出"容器"(实测:Windows 11 记事本正文返回 + // Pane[NotepadTextBox],而不是里面的 Document[RichEditD2DPT])。用户要的是 + // 光标下最深的那一个控件,和控件树/控件框一致,所以命中之后再往下收敛一层。 + AutomationElement deepest = RefineHitAtPoint(hit, point); + UiaElementInfo snapshot = Inspect(deepest ?? hit); if (snapshot.IsAvailable) return snapshot; } } @@ -277,6 +281,28 @@ namespace pacs_win_uia.Services catch (Exception ex) { return Failure(FormatException(ex)); } } + /// + /// 从命中元素往下找"包含该点的最小元素"。命中测试本身已经正确(它知道虚拟化行等), + /// 这里只是把"容器"收敛成"容器里的那个控件":记事本正文 → Document、XAML Pane → 里面的控件。 + /// 有界(节点数/深度/时间),任何异常都退回原始命中元素。 + /// + // 命中收敛:只在"子元素也包含该点"时下钻,找不到就保持原命中,绝不换成无关元素。 + private static AutomationElement RefineHitAtPoint(AutomationElement hit, Point point) + { + if (hit == null) return null; + try + { + var state = new PointProbeState(1200); + FindDeepestElementAtPoint(hit, point, 0, 16, state); + return state.Best ?? hit; + } + catch (Exception ex) + { + AppLog.Debug("命中收敛失败,沿用原始命中元素:" + ex.GetType().Name); + return hit; + } + } + // 祖先校验:用引用相等或 HWND 相等判断命中元素属于目标窗口,绝不用名称/类名。 private static bool BelongsToWindow(AutomationElement hit, AutomationElement root, IntPtr handle) { diff --git a/src/pacs_win_uia/UI/MainForm.cs b/src/pacs_win_uia/UI/MainForm.cs index 1f42047..2494a76 100644 --- a/src/pacs_win_uia/UI/MainForm.cs +++ b/src/pacs_win_uia/UI/MainForm.cs @@ -1587,10 +1587,11 @@ namespace pacs_win_uia ApplyPickResult(result); - // Only a top-level window was reachable. Browser-based clients start their accessibility - // tree when a client first asks, so a second probe a moment later normally reaches the - // rendered control that the first probe could not see. - if (_currentElement != null && string.Equals(_currentElement.ControlType, "ControlType.Window", StringComparison.Ordinal)) + // 只拿到窗口、或者只拿到"容器"(Pane/Group/Custom)时再探一次: + // 一是浏览器内核/套壳应用的无障碍树是按需生成的(第一次问只给窗口、给容器); + // 二是有些提供者的命中测试先返回容器(Windows 11 记事本正文返回 Pane[NotepadTextBox], + // 里面才是 Document[RichEditD2DPT])。第二次探测更深就把结果换掉。 + if (_currentElement != null && IsContainerControlType(_currentElement.ControlType)) { ScheduleDeeperPickRetry(result, _pickSession); } @@ -1683,6 +1684,20 @@ namespace pacs_win_uia BeginTreeRead(rootHandle, null, rootWindow, "被拾取控件 " + pickedNode); } + // 容器类控件类型:Window/Pane/Group/Custom 都只是"装东西的",拾取到它们通常不是用户想要的叶子控件。 + private static bool IsContainerControlType(string controlType) + { + if (string.IsNullOrEmpty(controlType)) + { + return false; + } + + return string.Equals(controlType, "ControlType.Window", StringComparison.Ordinal) + || string.Equals(controlType, "ControlType.Pane", StringComparison.Ordinal) + || string.Equals(controlType, "ControlType.Group", StringComparison.Ordinal) + || string.Equals(controlType, "ControlType.Custom", StringComparison.Ordinal); + } + /// /// Re-probes the same screen point once, on a background thread, and adopts the result only /// if it reaches something deeper than the window the first probe returned. @@ -1697,6 +1712,7 @@ namespace pacs_win_uia Point screenPoint = firstPick.ScreenPoint; IntPtr handle = firstPick.Window.Handle; string firstRuntimeId = firstPick.AutomationElement == null ? null : firstPick.AutomationElement.RuntimeId; + string firstControlType = firstPick.AutomationElement == null ? null : firstPick.AutomationElement.ControlType; AppLog.Info("拾取只拿到了顶层窗口(" + firstPick.Window + ")," + DeeperPickRetryDelayMs + "ms 后按同一坐标重探一次,看目标是否已激活无障碍树。"); @@ -1728,7 +1744,8 @@ namespace pacs_win_uia && deeper.AutomationElement != null && deeper.AutomationElement.IsAvailable && deeper.AutomationElement.Element != null - && !string.Equals(deeper.AutomationElement.ControlType, "ControlType.Window", StringComparison.Ordinal) + && !IsContainerControlType(deeper.AutomationElement.ControlType) + && !string.Equals(deeper.AutomationElement.ControlType, firstControlType, StringComparison.Ordinal) && !string.Equals(deeper.AutomationElement.RuntimeId, firstRuntimeId, StringComparison.Ordinal); if (!improved) { @@ -2270,6 +2287,8 @@ namespace pacs_win_uia element.EnsureRuntimeId(); // 控件属性 里直接给出 path / 页面特征 / 焦点,省得来回切换去找。 element.ControlPath = DescribeNodePath(node); + // 控件属性里直接给出内容文本(输入框/富文本的内容),与取值键「文本内容」同源。 + element.TextContent = ControlValueReader.TryGetTextContent(element); element.PageFeatures = ControlLocatorBuilder.DescribePageFeatures(node.Window ?? _inspectedWindow); }