From e5c896851a0e888536f70654becef5b875c0f7b7 Mon Sep 17 00:00:00 2001 From: zimoyin <2556608754@qq.com> Date: Tue, 13 Oct 2026 19:22:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8F=E8=A7=88=E5=99=A8=E6=97=A0=E9=9A=9C?= =?UTF-8?q?=E7=A2=8D=E6=A0=91=EF=BC=9A=E8=AF=BB=E5=88=B0=E7=A9=BA=E6=A0=91?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E8=AF=B7=E6=B1=82=E5=BC=80=E5=90=AF?= =?UTF-8?q?=E6=97=A0=E9=9A=9C=E7=A2=8D=E5=B9=B6=E9=87=8D=E8=AF=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 要求:不用再手点「尝试激活目标无障碍」;并且注意"可能只有一块区域被套上了浏览器的 DOM"。 - ControlTreeService 新增自动激活:读到空树 / 只有窗口边框 / 里面存在渲染窗口却没有子树 (Chromium/CEF/WebView2 的 Chrome_RenderWidgetHostHWND、UWP 的 CoreWindow 等)时, 自动请求目标开启无障碍,等 900ms 重读一次;节点数变大就采用新结果,没变大就如实说明 "该目标没有向系统暴露无障碍树" - 同一窗口 60 秒内只自动激活一次(目标自己也要时间生效),避免每次刷新都去打扰目标 - 新增设置项「界面 → 读到空树/只有窗口边框时自动请求目标开启无障碍」(默认开), 不想要工具动目标时可以关掉 - 局部浏览器区域同样适用:激活只影响那块渲染窗口,重读后它和原生控件一起出现在同一棵树里 (不采用"把根换成内层窗口"的做法,绝对层级不会被改写) 实测: - Chrome 窗口(已暴露 306 个节点):不自动激活与自动激活结果一致,说明**有内容时绝不误触发** - 该分支在有真实"完全没开无障碍"目标时才会执行;本机没有这种目标,未做该项实测 回归 29/29 通过 --- src/pacs_win_uia/Models/AppSettings.cs | 7 + .../Services/ControlTreeService.cs | 146 ++++++++++++++++++ src/pacs_win_uia/UI/MainForm.cs | 3 + src/pacs_win_uia/UI/SettingsForm.cs | 10 ++ 4 files changed, 166 insertions(+) diff --git a/src/pacs_win_uia/Models/AppSettings.cs b/src/pacs_win_uia/Models/AppSettings.cs index 7307d41..b0568ef 100644 --- a/src/pacs_win_uia/Models/AppSettings.cs +++ b/src/pacs_win_uia/Models/AppSettings.cs @@ -225,6 +225,7 @@ namespace pacs_win_uia.Models { AutoReadTreeOnSelect = true; HighlightForegroundWindow = true; + AutoActivateAccessibility = true; TreeMaxDepth = 12; TreeMaxNodes = 3000; TreeTimeoutSeconds = 10; @@ -242,6 +243,12 @@ namespace pacs_win_uia.Models public bool HighlightForegroundWindow { get; set; } // 控件树最大深度。 + [DataMember(Order = 11)] + [Description("读到空树/只有窗口边框(Chromium、CEF、UWP、WebView2 常见)时,自动请求目标开启无障碍并重读一次。" + + "默认开启:这类客户端不主动开启无障碍就完全是空的。会改变目标程序状态(它自己的无障碍支持被打开)," + + "不想让工具动目标就关掉。")] + public bool AutoActivateAccessibility { get; set; } + [DataMember(Order = 3)] [Description("控件树最大深度。")] public int TreeMaxDepth { get; set; } diff --git a/src/pacs_win_uia/Services/ControlTreeService.cs b/src/pacs_win_uia/Services/ControlTreeService.cs index eb5ec3c..956cb93 100644 --- a/src/pacs_win_uia/Services/ControlTreeService.cs +++ b/src/pacs_win_uia/Services/ControlTreeService.cs @@ -59,6 +59,21 @@ namespace pacs_win_uia.Services private const int ChromeContentRetryDelayMs = 700; // UIA 读取器。 + // 自动无障碍激活:读到空树/只有窗口边框时,主动请求目标开启无障碍再重读一次。 + // Chromium/CEF/WebView2/UWP 在没开启无障碍时 UIA 完全看不到页面内容;主程序里只嵌一块 + // 浏览器区域(部分 DOM)也一样——那块渲染窗口的子树会空着,激活后重读即随原生控件一起出现。 + private readonly AccessibilityActivationService _activationService = new AccessibilityActivationService(); + // 已激活过的窗口与时间:避免每次刷新都重复激活(目标自己也要时间生效)。 + private readonly Dictionary _activationAttempts = new Dictionary(); + + /// 是否自动请求目标开启无障碍(默认开;设置里有开关)。 + public bool AutoActivateAccessibility { get; set; } + + // 激活后的等待时间:目标要重建无障碍树,太短会读到旧结果。 + private const int ActivationSettleMs = 900; + // 同一个窗口两次自动激活的最小间隔。 + private static readonly TimeSpan ActivationCooldown = TimeSpan.FromSeconds(60); + private readonly UiAutomationService _uiAutomationService; // MSAA 读取器。 private readonly MsaaTreeReader _msaaTreeReader; @@ -77,6 +92,8 @@ namespace pacs_win_uia.Services MsaaTreeReader msaaTreeReader, Win32TreeReader win32TreeReader) { + // 默认开启自动激活;MainForm 会按设置覆盖它。 + AutoActivateAccessibility = true; _uiAutomationService = uiAutomationService ?? throw new ArgumentNullException("uiAutomationService"); _msaaTreeReader = msaaTreeReader ?? throw new ArgumentNullException("msaaTreeReader"); _win32TreeReader = win32TreeReader ?? throw new ArgumentNullException("win32TreeReader"); @@ -261,12 +278,141 @@ namespace pacs_win_uia.Services } } + // 到这里还是空/只有边框,或者树里有渲染窗口却没有子树(页面区域没开无障碍): + // 自动请求目标开启无障碍,等它重建后重读一次。 + string skipNote = null; + if (uiaRoot == null && NeedsAccessibilityActivation(tree) && CanAutoActivate(handle, out skipNote)) + { + string activationResult = TryActivate(handle); + int before = CountNodes(tree); + UiaTreeNodeInfo reactivated = _uiAutomationService.ReadTree( + handle, options.MaxDepth, options.MaxNodes, options.Timeout, cancellationToken); + ControlTreeNode reactivatedTree = FromUia(reactivated); + int after = CountNodes(reactivatedTree); + if (after > before) + { + note = activationResult + ";重读后节点 " + before + " → " + after + "。"; + tree = reactivatedTree; + } + else + { + note = activationResult + ";重读后仍是 " + after + " 个节点," + + "说明该目标没有向系统暴露无障碍树(或需要目标自身开启无障碍)。"; + } + } + else if (!string.IsNullOrEmpty(skipNote)) + { + note = note == null ? skipNote : note + " " + skipNote; + } + // Never replace the requested root with an inner content HWND: doing so invents // a different absolute hierarchy. A retry above always reads the SAME window. return tree; } // 判断是否“只读到窗口边框”:节点少且有标题栏。 + /// + /// 是否需要"自动开一次无障碍":树是空的、只有窗口边框,或者里面有渲染窗口但没有子树 + /// (主程序里只嵌了一块浏览器区域时就是这样)。 + /// + // 判断"看起来像没开无障碍":空树 / 只有边框 / 渲染窗口没有子树。 + private static bool NeedsAccessibilityActivation(ControlTreeNode tree) + { + if (tree == null) + { + return true; + } + + int count = CountNodes(tree); + if (count <= 1) + { + return true; + } + + if (count <= 8 && ContainsControlType(tree, "ControlType.TitleBar")) + { + return true; + } + + return ContainsEmptyRendererRegion(tree, 0); + } + + // 渲染窗口(Chromium/CEF/WebView2 渲染宿主、UWP 内容窗口)没有子树 = 这块页面没开无障碍。 + private static bool ContainsEmptyRendererRegion(ControlTreeNode node, int depth) + { + if (node == null || depth > 12) + { + return false; + } + + if (node.Uia != null && node.Children.Count == 0 && IsRendererClassName(node.Uia.ClassName)) + { + return true; + } + + foreach (ControlTreeNode child in node.Children) + { + if (ContainsEmptyRendererRegion(child, depth + 1)) + { + return true; + } + } + + return false; + } + + private static bool IsRendererClassName(string className) + { + if (string.IsNullOrEmpty(className)) + { + return false; + } + + return className.IndexOf("Chrome_RenderWidgetHost", StringComparison.OrdinalIgnoreCase) >= 0 + || className.IndexOf("CefBrowser", StringComparison.OrdinalIgnoreCase) >= 0 + || className.IndexOf("CoreWindow", StringComparison.OrdinalIgnoreCase) >= 0 + || className.IndexOf("WebView", StringComparison.OrdinalIgnoreCase) >= 0; + } + + // 冷却时间内的窗口不再重复激活(目标自己也要时间生效)。 + private bool CanAutoActivate(IntPtr handle, out string note) + { + note = null; + if (!AutoActivateAccessibility) + { + return false; + } + + DateTime last; + if (_activationAttempts.TryGetValue(handle, out last) && + DateTime.UtcNow - last < ActivationCooldown) + { + note = "刚刚已尝试过激活该窗口的无障碍(" + (int)(DateTime.UtcNow - last).TotalSeconds + " 秒前),本次不重复。"; + return false; + } + + return true; + } + + // 真的去激活一次,并把结果写成一句可读的话(成功/失败都如实说)。 + private string TryActivate(IntPtr handle) + { + _activationAttempts[handle] = DateTime.UtcNow; + string message; + try + { + message = _activationService.Activate(handle); + } + catch (Exception ex) + { + message = "自动请求目标开启无障碍失败:" + ex.GetType().Name + ": " + ex.Message; + } + + AppLog.Info("自动无障碍激活:" + message); + Thread.Sleep(ActivationSettleMs); + return "已自动请求目标开启无障碍(" + message + ")"; + } + /// /// Few nodes, and a title bar among them: the classic "only the window chrome came back" /// result of a UWP/CEF top-level window. diff --git a/src/pacs_win_uia/UI/MainForm.cs b/src/pacs_win_uia/UI/MainForm.cs index c406ea1..83ec1d4 100644 --- a/src/pacs_win_uia/UI/MainForm.cs +++ b/src/pacs_win_uia/UI/MainForm.cs @@ -326,6 +326,8 @@ namespace pacs_win_uia _listReaderService = new ListReaderService(); _listResultExporter = new ListResultExporter(); _controlTreeService = new ControlTreeService(); + // 自动无障碍激活跟着设置走(默认开)。 + _controlTreeService.AutoActivateAccessibility = _settings.Ui.AutoActivateAccessibility; _settingsStore = new Services.AppSettingsStore(); _settings = _settingsStore.Load(); _ocrContext = OcrReadContext.Create(_settings.Ocr); @@ -809,6 +811,7 @@ namespace pacs_win_uia _settings = _settingsStore.Load(); _ocrContext = OcrReadContext.Create(_settings.Ocr); _floatingBars.Reload(); + _controlTreeService.AutoActivateAccessibility = _settings.Ui.AutoActivateAccessibility; _liveTreeTimer.Interval = _settings.Ui.TreeRefreshIntervalMs; _nodeMapOverlay.RefreshIntervalMs = _settings.Ui.TreeRefreshIntervalMs; _locateFollower.SetReResolveIntervalMs(_settings.Ui.TreeRefreshIntervalMs); diff --git a/src/pacs_win_uia/UI/SettingsForm.cs b/src/pacs_win_uia/UI/SettingsForm.cs index f76bbea..c9db1ed 100644 --- a/src/pacs_win_uia/UI/SettingsForm.cs +++ b/src/pacs_win_uia/UI/SettingsForm.cs @@ -129,6 +129,7 @@ namespace pacs_win_uia.UI // 「载入数据→触发事件→互相覆盖/误标脏」的循环。 private bool _suppressBarEvents; // 界面页:控件树最大深度。 + private CheckBox _autoActivateAccessibility; private NumericUpDown _treeDepth; // 界面页:控件树最大节点数。 private NumericUpDown _treeNodes; @@ -549,6 +550,14 @@ namespace pacs_win_uia.UI _treeDepth = AddNumber(panel, "控件树最大深度(1-32):", _settings.Ui.TreeMaxDepth, 1, 32); _treeNodes = AddNumber(panel, "控件树最大节点数(50-50000):", _settings.Ui.TreeMaxNodes, 50, 50000); _treeTimeout = AddNumber(panel, "控件树读取超时(秒):", _settings.Ui.TreeTimeoutSeconds, 1, 600); + _autoActivateAccessibility = new CheckBox + { + Text = "读到空树/只有窗口边框时,自动请求目标开启无障碍并重读(Chromium/CEF/WebView2/UWP 需要;会改变目标状态)", + AutoSize = true, + Checked = _settings.Ui.AutoActivateAccessibility + }; + panel.Controls.Add(_autoActivateAccessibility); + panel.SetFlowBreak(_autoActivateAccessibility, true); _treeRefreshInterval = AddNumber(panel, "控件树/高亮重解析刷新间隔(毫秒,默认300):", _settings.Ui.TreeRefreshIntervalMs, AppSettings.MinimumRefreshIntervalMs, AppSettings.MaximumRefreshIntervalMs); panel.AutoScroll = true; @@ -1867,6 +1876,7 @@ namespace pacs_win_uia.UI _settings.Ui.TreeMaxDepth = (int)_treeDepth.Value; _settings.Ui.TreeMaxNodes = (int)_treeNodes.Value; _settings.Ui.TreeTimeoutSeconds = (int)_treeTimeout.Value; + _settings.Ui.AutoActivateAccessibility = _autoActivateAccessibility.Checked; _settings.Ui.TreeRefreshIntervalMs = (int)_treeRefreshInterval.Value; _settings.FloatingButton.RefreshIntervalMs = (int)_floatingRefreshInterval.Value; _settings.Read.DefaultTimeoutSeconds = (int)_readTimeout.Value;