diff --git a/docs/XPath定位.md b/docs/XPath定位.md
index e0e8867..6ce7a63 100644
--- a/docs/XPath定位.md
+++ b/docs/XPath定位.md
@@ -196,6 +196,38 @@ XPath 中的 `Edit`、`Button` 等来自 UIA 控件类型。不同来源也会
如果控件名称是“患者编号:张三”,而“患者编号”稳定,可以使用它。
+### 7b. 状态属性:选"当前选中行 / 焦点行"
+
+节点上除了名称类属性,还有四个状态属性,值是 `'true'` / `'false'`(状态未知时不写这个属性):
+
+| 属性 | 含义 | 来源 |
+|---|---|---|
+| `@IsSelected` | 是否被选中 | 行/项自己的 SelectionItemPattern,或从容器的 SelectionPattern 反推 |
+| `@IsFocused` | 是否是当前焦点元素 | UIA HasKeyboardFocus |
+| `@IsOffscreen` | 是否在屏幕外(被滚动出视图/折叠) | UIA IsOffscreen |
+| `@IsEnabled` | 是否可用(未禁用) | UIA IsEnabled |
+
+因此"拿当前选中行 / 焦点行"不用再写序号:
+
+```xpath
+/Window[1]//DataGrid[@AutomationId='FixtureListView']/DataItem[@IsSelected='true']
+/Window[1]//DataGrid[@AutomationId='FixtureListView']/DataItem[@IsFocused='true']
+```
+
+配合取值键 `文本内容`,就能直接读到那一行的文字。注意:`@IsFocused` 取决于焦点此刻在哪个窗口;`@IsSelected` 对一次性列出选中集合的提供者(如 WinForms 列表)由容器反推得来。
+
+### 7c. 列表信息:不用整表读取也能盯住表格
+
+容器控件(List / DataGrid / Table / Tree)上多出三个取值键,适合放进控件监控:
+
+| 取值键 | 含义 |
+|---|---|
+| `列表行数` | 行数(GridPattern 优先,其次 TablePattern) |
+| `列表列数` | 列数 |
+| `列表选中项数` | 当前选中项数 |
+
+需要完整行列数据时仍然用「读取选中控件列表」。
+
### 8. 序号 `[n]`
序号从 **1** 开始:
diff --git a/docs/代码导览.md b/docs/代码导览.md
index 93667a7..4459e5e 100644
--- a/docs/代码导览.md
+++ b/docs/代码导览.md
@@ -49,6 +49,7 @@ MainForm(UI/MainForm.cs) 三栏主界面:窗口列表 | 控件树/列
| 主界面布局/按钮/三栏比例 | `UI/MainForm.cs` | 布局自检 `VerifyLayout()` 会报错,改动后看日志 |
| 控件树怎么显示、要不要闪烁 | `UI/ControlTreeReconciler.cs` | 增量更新,绝不清空重建 |
| 拾取为什么选到容器 | `Services/UiAutomationService.cs` 的 `FromHandleAtPoint` | 命中后向深处收敛 |
+| 拾取的蓝框/黄框(层级) | `Services/TargetProbeService.cs`(FillHierarchyFrames)+ `UI/PickHierarchyOverlayForm.cs` | 蓝=光标所在窗口,黄=向上遍历的控件链 |
| 控件树读取来源与顺序 | `Services/ControlTreeService.cs` | Auto = UIA → MSAA → Win32 |
| UIA 快照/缓存/Pattern | `Services/UiAutomationService.cs` | 统一 RawView + CacheRequest |
| 列表读取(行列/单元格/降级) | `Services/ListReaderService.cs` | 最长的文件,见其顶部导览 |
diff --git a/src/pacs_win_uia/Interop/NativeMethods.cs b/src/pacs_win_uia/Interop/NativeMethods.cs
index 512c798..57bff78 100644
--- a/src/pacs_win_uia/Interop/NativeMethods.cs
+++ b/src/pacs_win_uia/Interop/NativeMethods.cs
@@ -32,6 +32,8 @@ namespace pacs_win_uia.Interop
{
// 取窗口扩展样式(GWL_EXSTYLE)的索引。
internal const int GWL_EXSTYLE = -20;
+ // 取窗口样式(GWL_STYLE)的索引。
+ internal const int GWL_STYLE = -16;
// 扩展样式:点击穿透(透明区域不接收鼠标)。
internal const int WS_EX_TRANSPARENT = 0x00000020;
// 扩展样式:工具窗口(不出现在任务栏)。
@@ -544,7 +546,8 @@ namespace pacs_win_uia.Interop
internal static extern bool SetForegroundWindow(IntPtr hWnd);
// 读取窗口扩展样式(GWL_EXSTYLE),按位宽分派 32/64 位实现。
- internal static int GetWindowStyle(IntPtr hWnd)
+ // 历史遗留:这个方法原来叫 GetWindowStyle,但它读的是 EXSTYLE,容易和 GWL_STYLE 混淆,已正名。
+ internal static int GetWindowExStyle(IntPtr hWnd)
{
if (IntPtr.Size == 8)
{
@@ -554,6 +557,27 @@ namespace pacs_win_uia.Interop
return GetWindowLong32(hWnd, GWL_EXSTYLE);
}
+ // 读取窗口样式(GWL_STYLE),按位宽分派 32/64 位实现(32 位系统只有 GetWindowLong)。
+ internal static int GetWindowStyle(IntPtr hWnd)
+ {
+ if (IntPtr.Size == 8)
+ {
+ return unchecked((int)GetWindowLongPtr64(hWnd, GWL_STYLE).ToInt64());
+ }
+
+ return GetWindowLong32(hWnd, GWL_STYLE);
+ }
+
+ // IsWindowEnabled:窗口是否启用(Win32 侧的"Enabled")。
+ [DllImport("user32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool IsWindowEnabled(IntPtr hWnd);
+
+ // IsZoomed:窗口是否最大化(ISWindowVisible/IsIconic 之外的状态补充)。
+ [DllImport("user32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ internal static extern bool IsZoomed(IntPtr hWnd);
+
// 设置窗口扩展样式(GWL_EXSTYLE),按位宽分派 32/64 位实现。
internal static IntPtr SetWindowStyle(IntPtr hWnd, int style)
{
diff --git a/src/pacs_win_uia/Models/ControlLocator.cs b/src/pacs_win_uia/Models/ControlLocator.cs
index d63e7f1..0bfe1ec 100644
--- a/src/pacs_win_uia/Models/ControlLocator.cs
+++ b/src/pacs_win_uia/Models/ControlLocator.cs
@@ -166,7 +166,18 @@ namespace pacs_win_uia.Models
///
public const string TextContent = "文本内容";
- /// Cross-source key: is this control the selected one.
+ ///
+ /// 列表相关(UIA 专用):容器控件的行数 / 列数 / 当前选中项数。
+ /// 用它们可以在控件监控里直接盯住"这张表有多少行、选中了几行",
+ /// 而不需要走完整的"读取选中控件列表"。
+ ///
+ public const string ListRowCount = "列表行数";
+
+ public const string ListColumnCount = "列表列数";
+
+ public const string ListSelectedCount = "列表选中项数";
+
+ /// 跨来源键:这个控件是不是被选中的那一个。
public const string Selected = "选中状态";
// Focused:跨来源键——该控件当前是否拥有键盘焦点。
@@ -186,7 +197,8 @@ namespace pacs_win_uia.Models
// 文本内容排在最前:它是文本类控件最常用的取值键。
TextContent, "Name", "AutomationId", "ControlType", "ClassName", "FrameworkId",
"RuntimeId", "IsEnabled", "IsOffscreen", "BoundingRectangle",
- "Value", "Text", Selected, "IsSelected", Focused, "HasKeyboardFocus"
+ "Value", "Text", Selected, "IsSelected", Focused, "HasKeyboardFocus",
+ ListRowCount, ListColumnCount, ListSelectedCount
};
// Msaa:MSAA 来源可读取的属性键列表。
diff --git a/src/pacs_win_uia/Models/NativeWindowInfo.cs b/src/pacs_win_uia/Models/NativeWindowInfo.cs
index 61dc39e..47f5c2a 100644
--- a/src/pacs_win_uia/Models/NativeWindowInfo.cs
+++ b/src/pacs_win_uia/Models/NativeWindowInfo.cs
@@ -99,6 +99,33 @@ namespace pacs_win_uia.Models
public NativeRectangleInfo ClientBounds { get; set; }
// Error:读取该窗口信息时的错误;正常为空。
+ // 窗口样式(GWL_STYLE / GWL_EXSTYLE,十六进制字符串)。判断"自绘/无子窗口/透明/置顶"时先看这里。
+ [DataMember(Order = 15, EmitDefaultValue = false)]
+ [Description("窗口样式 GWL_STYLE(十六进制)。圆角/边框/子窗口等行为由它决定。")]
+ public string Style { get; set; }
+
+ [DataMember(Order = 16, EmitDefaultValue = false)]
+ [Description("窗口扩展样式 GWL_EXSTYLE(十六进制)。WS_EX_TRANSPARENT(0x20)/WS_EX_LAYERED(0x80000) 等能解释「为什么命中的是这一层窗口」。")]
+ public string ExStyle { get; set; }
+
+ [DataMember(Order = 17, EmitDefaultValue = false)]
+ [Description("窗口是否启用(Win32 IsWindowEnabled)。与 UIA 的 IsEnabled 是两套来源,可能不一致。")]
+ public bool? IsEnabled { get; set; }
+
+ [DataMember(Order = 18, EmitDefaultValue = false)]
+ [Description("窗口是否最大化(Win32 IsZoomed)。与「最小化」一起判断窗口当前状态。")]
+ public bool IsMaximized { get; set; }
+
+ ///
+ /// 层级关系(父窗口 / 直接子窗口数 / 同级同类序号),按需读取:枚举子窗口对每次窗口刷新来说太贵。
+ ///
+ [IgnoreDataMember]
+ [Browsable(true)]
+ [ReadOnly(true)]
+ [Description("层级关系:父窗口(类名+HWND)、直接子窗口数、在父窗口同类子窗口中的序号。"
+ + "Win32 只看得到「真实窗口」,自绘区域不会有更细的子窗口。按需读取。")]
+ public string HierarchyText { get; set; }
+
[DataMember(Order = 14)]
public string Error { get; set; }
diff --git a/src/pacs_win_uia/Models/PointPickResult.cs b/src/pacs_win_uia/Models/PointPickResult.cs
index 9671753..77e87bf 100644
--- a/src/pacs_win_uia/Models/PointPickResult.cs
+++ b/src/pacs_win_uia/Models/PointPickResult.cs
@@ -1,4 +1,6 @@
using System;
+using System.Collections.Generic;
+using System.ComponentModel;
using System.Drawing;
using System.Runtime.Serialization;
@@ -40,6 +42,20 @@ namespace pacs_win_uia.Models
[DataMember(Order = 4)]
public UiaElementInfo AutomationElement { get; set; }
+ ///
+ /// 拾取时显示的多层框:蓝色 = 光标所在窗口,黄色 = 从被拾取控件向上遍历的控件链。
+ /// 只在拾取预览时填充(用户要求"看得见自己在哪一层")。
+ ///
+ [IgnoreDataMember]
+ [Browsable(false)]
+ public IList HierarchyFrames { get; private set; }
+
+ /// 拾取时显示的多层框(蓝=窗口 / 黄=控件链)。
+ public void SetHierarchyFrames(IList frames)
+ {
+ HierarchyFrames = frames ?? new List();
+ }
+
// IsToolWindow:命中的窗口是否属于本工具自身。
[DataMember(Order = 5)]
public bool IsToolWindow { get; set; }
@@ -109,4 +125,22 @@ namespace pacs_win_uia.Models
get { return AutomationElement != null && AutomationElement.IsAvailable; }
}
}
+
+ /// 拾取时的一层框:屏幕矩形 + 颜色语义 + 说明文字。
+ public sealed class PickFrameInfo
+ {
+ /// 屏幕坐标矩形。
+ public NativeRectangleInfo Bounds { get; set; }
+
+ /// true = 蓝色(光标所在窗口);false = 黄色(控件链上的一层)。
+ public bool IsWindowLevel { get; set; }
+
+ /// 这一层的说明:类型 + 名称 + 类名。
+ public string Label { get; set; }
+
+ public override string ToString()
+ {
+ return (IsWindowLevel ? "[蓝] " : "[黄] ") + Label + " " + Bounds;
+ }
+ }
}
diff --git a/src/pacs_win_uia/Models/UiaElementInfo.cs b/src/pacs_win_uia/Models/UiaElementInfo.cs
index 42f68ad..d426802 100644
--- a/src/pacs_win_uia/Models/UiaElementInfo.cs
+++ b/src/pacs_win_uia/Models/UiaElementInfo.cs
@@ -193,6 +193,57 @@ namespace pacs_win_uia.Models
// HasKeyboardFocus:当前是否有键盘焦点(与“是否被选中”不是一回事);null = 没读到。
public bool? HasKeyboardFocus { get; set; }
+ // ================= 层级关系(Raw/Control/Content 三个视图) =================
+ // 说明:UIA 有三个视图——RawView 含不可见/非控件元素(控件树读取用的就是这个),
+ // ControlView 是「控件」视图,ContentView 是「内容」视图。三个视图的子元素数常常不同,
+ // 定位/读取不一致时先看这里。按需填充(选中节点时才调用,会跨进程)。
+ [IgnoreDataMember]
+ [Browsable(true)]
+ [ReadOnly(true)]
+ [Description("父元素(RawView 上溯一级):类型 + 名称。定位不准时看这里,确认层级是否和想象一致。")]
+ public string ParentSummary { get; set; }
+
+ [IgnoreDataMember]
+ [Browsable(true)]
+ [ReadOnly(true)]
+ [Description("三个视图的直接子元素数:RawView / ControlView / ContentView。控件树用的是 RawView;列表遍历读不到内容时,比较这三个值能立刻看出是「视图不同」还是「目标没暴露」。按需读取。")]
+ public string ViewCounts { get; set; }
+
+ // ================= 文本与值(除 Name/Value/Text 之外的 Pattern 值) =================
+ [IgnoreDataMember]
+ [Browsable(true)]
+ [ReadOnly(true)]
+ [Description("RangeValuePattern 的当前值/范围(滑块、进度、微调框等)。不支持该模式时为空。按需读取。")]
+ public string RangeValueText { get; set; }
+
+ [IgnoreDataMember]
+ [Browsable(true)]
+ [ReadOnly(true)]
+ [Description("TogglePattern 的开关状态(开/关/不确定,复选框、开关按钮等)。不支持该模式时为空。按需读取。")]
+ public string ToggleStateText { get; set; }
+
+ [IgnoreDataMember]
+ [Browsable(true)]
+ [ReadOnly(true)]
+ [Description("选中情况:SelectionItemPattern 是否被选中,或 SelectionPattern 当前选中项数与首项文本。列表行/列表项用它判断「当前选中行」。按需读取。")]
+ public string SelectionText { get; set; }
+
+ [IgnoreDataMember]
+ [Browsable(true)]
+ [ReadOnly(true)]
+ [Description("列表概况:行数 × 列数 + 当前选中项数 + 首行文本(仅当这个控件是列表/表格时才有值)。"
+ + "想在监控里盯住某张表,用取值键「列表行数」「列表列数」「列表选中项数」。按需读取。")]
+ public string ListSummary { get; set; }
+
+ ///
+ /// SelectionItemPattern.Current.IsSelected(缓存里读取,随控件树一起拿到)。
+ /// 供 XPath 属性 @IsSelected 与"选中状态"取值使用:例如
+ /// /Window[1]//DataGrid[@AutomationId='x']/DataItem[@IsSelected='true']。
+ ///
+ [DataMember(Order = 19)]
+ [Description("该控件是否被选中(UIA SelectionItemPattern.IsSelected)。列表行、列表项、树节点通常支持;不支持时为 null。XPath 里可以用 @IsSelected='true' 选「当前选中行」。")]
+ public bool? IsSelected { get; set; }
+
[DataMember(Order = 18)]
[Description("该控件是否可以获得键盘焦点(UIA IsKeyboardFocusable)。不能获得焦点的控件,焦点状态永远是“否”。")]
// IsKeyboardFocusable:是否可获得键盘焦点;不能获得焦点的控件,焦点状态永远是“否”。
diff --git a/src/pacs_win_uia/Picker/CrosshairPicker.cs b/src/pacs_win_uia/Picker/CrosshairPicker.cs
index dba0608..1cfdfd9 100644
--- a/src/pacs_win_uia/Picker/CrosshairPicker.cs
+++ b/src/pacs_win_uia/Picker/CrosshairPicker.cs
@@ -46,6 +46,8 @@ namespace pacs_win_uia.Picker
private readonly TargetProbeService _probeService;
// 预览高亮跟随器。
private readonly HighlightFollower _highlight;
+ // 拾取预览的"多层框":蓝 = 光标所在窗口,黄 = 向上遍历的控件链(用户要求看得见层级)。
+ private readonly PickHierarchyOverlayForm _hierarchy = new PickHierarchyOverlayForm();
// 探测看门狗定时器(检查卡住与点击期限)。
private readonly System.Windows.Forms.Timer _probeWatchdog;
// 全屏十字覆盖层窗体。
@@ -125,6 +127,7 @@ namespace pacs_win_uia.Picker
_isPicking = true;
_highlight.Hide("拾取开始。");
+ _hierarchy.HideFrames();
_hasLastProbePoint = false;
_lastNativeWindowHandle = IntPtr.Zero;
_hasPendingProbePoint = false;
@@ -284,8 +287,20 @@ namespace pacs_win_uia.Picker
// Win32 or MSAA can see still gets a highlight box. The follower keeps it glued to the
// target window while the mouse is still, and takes it away when the target is
// minimized or covered.
- if (_lastResult != null && _lastResult.HasBounds)
+ if (_lastResult != null && _lastResult.HierarchyFrames != null && _lastResult.HierarchyFrames.Count > 0)
{
+ // 层级框:蓝框=光标所在窗口,黄框=控件链。都以"相对该窗口的偏移"跟随。
+ IntPtr anchorWindow = _lastResult.DeepestWindow != null
+ ? _lastResult.DeepestWindow.Handle
+ : (_lastResult.Window == null ? IntPtr.Zero : _lastResult.Window.Handle);
+ _hierarchy.ShowFrames(anchorWindow, _lastResult.HierarchyFrames);
+ _highlight.Hide("拾取预览改用层级框。");
+ hasHighlight = true;
+ }
+ else if (_lastResult != null && _lastResult.HasBounds)
+ {
+ // 没有层级信息(例如只命中 Win32/MSAA)时,退回原来的单框高亮。
+ _hierarchy.HideFrames();
IntPtr anchor = _lastResult.Window == null ? IntPtr.Zero : _lastResult.Window.Handle;
_highlight.Show(_lastResult.Bounds.ToRectangle(), anchor, null, "拾取预览");
hasHighlight = true;
@@ -293,6 +308,7 @@ namespace pacs_win_uia.Picker
if (!hasHighlight)
{
+ _hierarchy.HideFrames();
_highlight.Hide("没有可高亮的矩形。");
}
@@ -547,6 +563,7 @@ namespace pacs_win_uia.Picker
}
_highlight.Hide("拾取结束。");
+ _hierarchy.HideFrames();
}
// 释放:停止拾取、卸载钩子、解绑并释放看门狗与高亮跟随器。
@@ -557,6 +574,7 @@ namespace pacs_win_uia.Picker
_probeWatchdog.Tick -= ProbeWatchdogOnTick;
_probeWatchdog.Dispose();
_highlight.Dispose();
+ _hierarchy.Dispose();
}
}
}
diff --git a/src/pacs_win_uia/Services/ControlPath.cs b/src/pacs_win_uia/Services/ControlPath.cs
index e32eaf1..5a883e7 100644
--- a/src/pacs_win_uia/Services/ControlPath.cs
+++ b/src/pacs_win_uia/Services/ControlPath.cs
@@ -301,6 +301,28 @@ namespace pacs_win_uia.Services
xml.SetAttribute("ClassName", ClassName(node) ?? string.Empty);
xml.SetAttribute("ControlType", node.Source == ControlTreeSource.Uia && node.Uia != null ? node.Uia.ControlType ?? string.Empty : TypeName(node));
xml.SetAttribute("Source", node.Source.ToString());
+ // 状态属性:让 XPath 能直接选"当前选中行/焦点行",例如
+ // //DataGrid[@AutomationId='x']/DataItem[@IsSelected='true']
+ // 只在状态已知时写出(未知就不写,避免出现空属性把条件匹配歪)。
+ if (node.Uia != null && node.Uia.IsSelected.HasValue)
+ {
+ xml.SetAttribute("IsSelected", node.Uia.IsSelected.Value ? "true" : "false");
+ }
+
+ if (node.Uia != null && node.Uia.HasKeyboardFocus.HasValue)
+ {
+ xml.SetAttribute("IsFocused", node.Uia.HasKeyboardFocus.Value ? "true" : "false");
+ }
+
+ if (node.Uia != null && node.Uia.IsOffscreen.HasValue)
+ {
+ xml.SetAttribute("IsOffscreen", node.Uia.IsOffscreen.Value ? "true" : "false");
+ }
+
+ if (node.Uia != null && node.Uia.IsEnabled.HasValue)
+ {
+ xml.SetAttribute("IsEnabled", node.Uia.IsEnabled.Value ? "true" : "false");
+ }
if (node.Source == ControlTreeSource.Msaa && node.Msaa != null)
{
xml.SetAttribute("Role", node.Msaa.RoleText ?? string.Empty);
diff --git a/src/pacs_win_uia/Services/ControlTreeService.cs b/src/pacs_win_uia/Services/ControlTreeService.cs
index 8a8b242..eb5ec3c 100644
--- a/src/pacs_win_uia/Services/ControlTreeService.cs
+++ b/src/pacs_win_uia/Services/ControlTreeService.cs
@@ -190,6 +190,12 @@ namespace pacs_win_uia.Services
}
// 统一生成绝对 XPath:所有来源读完后都在这里做,保证路径语义一致。
+ // 让"选中行"可用:从容器反推子项选中状态(XPath 的 @IsSelected / 取值键"选中状态")。
+ if (source == ControlTreeSource.Uia)
+ {
+ _uiAutomationService.FillContainerSelection(root);
+ }
+
ControlPath.PreparePaths(root);
stopwatch.Stop();
int nodeCount = CountNodes(root);
diff --git a/src/pacs_win_uia/Services/ControlValueReader.cs b/src/pacs_win_uia/Services/ControlValueReader.cs
index a3fc784..efd3304 100644
--- a/src/pacs_win_uia/Services/ControlValueReader.cs
+++ b/src/pacs_win_uia/Services/ControlValueReader.cs
@@ -225,6 +225,13 @@ namespace pacs_win_uia.Services
case "Value":
case "Text":
return TryReadUiaPatternValue(info, key, options, out value, out error);
+ // 列表信息:容器控件的行数/列数/选中项数(列表读取的轻量版本,适合放进监控)。
+ case ControlValueKeys.ListRowCount:
+ return TryReadUiaListDimension(info, true, out value, out error);
+ case ControlValueKeys.ListColumnCount:
+ return TryReadUiaListDimension(info, false, out value, out error);
+ case ControlValueKeys.ListSelectedCount:
+ return TryReadUiaSelectionCount(info, out value, out error);
// 选中状态:支持“选中状态”(Selected) 与 “IsSelected” 两个别名。
case ControlValueKeys.Selected:
case "IsSelected":
@@ -279,6 +286,85 @@ namespace pacs_win_uia.Services
return info.Name;
}
+
+ ///
+ /// 列表行数 / 列数:优先 GridPattern,其次 TablePattern(两者都有的控件以 Grid 为准)。
+ /// 只有容器(List/DataGrid/Table/Tree)才支持,普通控件会明确报"不是列表"。
+ ///
+ // 列表行/列:GridPattern → TablePattern,取不到就报错说明该控件不是列表。
+ private static bool TryReadUiaListDimension(UiaElementInfo info, bool rows, out string value, out string error)
+ {
+ value = null;
+ error = null;
+ AutomationElement element = info == null ? null : info.Element;
+ if (element == null)
+ {
+ error = "UIA 元素引用不可用(可能是回读时未重新绑定)。";
+ return false;
+ }
+
+ try
+ {
+ object pattern;
+ if (element.TryGetCurrentPattern(GridPattern.Pattern, out pattern))
+ {
+ GridPattern.GridPatternInformation current = ((GridPattern)pattern).Current;
+ value = (rows ? current.RowCount : current.ColumnCount)
+ .ToString(System.Globalization.CultureInfo.InvariantCulture);
+ return true;
+ }
+
+ if (element.TryGetCurrentPattern(TablePattern.Pattern, out pattern))
+ {
+ TablePattern.TablePatternInformation current = ((TablePattern)pattern).Current;
+ value = (rows ? current.RowCount : current.ColumnCount)
+ .ToString(System.Globalization.CultureInfo.InvariantCulture);
+ return true;
+ }
+
+ error = "该控件不支持 Grid/Table 模式,读不到" + (rows ? "行数" : "列数") + "(它可能不是一个列表)。";
+ return false;
+ }
+ catch (Exception ex)
+ {
+ error = "读取列表" + (rows ? "行数" : "列数") + "失败:" + ex.GetType().Name + ": " + ex.Message;
+ return false;
+ }
+ }
+
+ /// 列表当前选中项数(SelectionPattern);不支持该模式时明确报错。
+ // 选中项数:SelectionPattern.Current.GetSelection().Length。
+ private static bool TryReadUiaSelectionCount(UiaElementInfo info, out string value, out string error)
+ {
+ value = null;
+ error = null;
+ AutomationElement element = info == null ? null : info.Element;
+ if (element == null)
+ {
+ error = "UIA 元素引用不可用(可能是回读时未重新绑定)。";
+ return false;
+ }
+
+ try
+ {
+ object pattern;
+ if (!element.TryGetCurrentPattern(SelectionPattern.Pattern, out pattern))
+ {
+ error = "该控件不支持 SelectionPattern,读不到选中项数。";
+ return false;
+ }
+
+ AutomationElement[] items = ((SelectionPattern)pattern).Current.GetSelection();
+ value = (items == null ? 0 : items.Length).ToString(System.Globalization.CultureInfo.InvariantCulture);
+ return true;
+ }
+ catch (Exception ex)
+ {
+ error = "读取选中项数失败:" + ex.GetType().Name + ": " + ex.Message;
+ return false;
+ }
+ }
+
///
/// 读取"这个控件的内容文本"。顺序:ValuePattern → TextPattern → 快照 Name。
/// 这样输入框(内容在 Value)、富文本(内容在 Text)和把内容放在 Name 里的客户端都能拿到值。
diff --git a/src/pacs_win_uia/Services/NativeWindowService.cs b/src/pacs_win_uia/Services/NativeWindowService.cs
index 893476c..39bfba8 100644
--- a/src/pacs_win_uia/Services/NativeWindowService.cs
+++ b/src/pacs_win_uia/Services/NativeWindowService.cs
@@ -103,6 +103,85 @@ namespace pacs_win_uia.Services
}
// 抓取单个窗口的快照(不带进程名缓存)。
+ ///
+ /// 按需填充 Win32 侧的「层级关系」:父窗口、直接子窗口数、在父窗口同类子窗口中的序号。
+ /// 只在选中该窗口/控件查看属性时调用——每次窗口刷新都枚举子窗口太贵。
+ ///
+ // 按需填充层级:任何异常都只影响展示,不抛出。
+ public void FillDisplayExtras(NativeWindowInfo info)
+ {
+ if (info == null || info.Handle == IntPtr.Zero || !NativeMethods.IsWindow(info.Handle))
+ {
+ return;
+ }
+
+ try
+ {
+ IntPtr parent = NativeMethods.GetAncestor(info.Handle, NativeMethods.GA_PARENT);
+ var children = new List();
+ if (parent != IntPtr.Zero)
+ {
+ NativeMethods.EnumChildWindows(parent, delegate(IntPtr child, IntPtr state)
+ {
+ if (NativeMethods.GetAncestor(child, NativeMethods.GA_PARENT) == parent)
+ {
+ children.Add(child);
+ }
+
+ return true;
+ }, IntPtr.Zero);
+ }
+
+ string ownClass = ReadClassName(info.Handle);
+ int sameClassSeen = 0;
+ int sameClassTotal = 0;
+ foreach (IntPtr child in children)
+ {
+ if (!string.Equals(ReadClassName(child), ownClass, StringComparison.Ordinal))
+ {
+ continue;
+ }
+
+ sameClassTotal++;
+ if (child == info.Handle)
+ {
+ sameClassSeen = sameClassTotal;
+ }
+ }
+
+ NativeWindowInfo parentInfo = parent == IntPtr.Zero ? null : Capture(parent);
+ info.HierarchyText = "父窗口="
+ + (parentInfo == null
+ ? "(无,顶层窗口)"
+ : (string.IsNullOrEmpty(parentInfo.ClassName) ? "(无类名)" : parentInfo.ClassName)
+ + " " + parentInfo.HwndHex
+ + (string.IsNullOrEmpty(parentInfo.Title) ? string.Empty : "「" + parentInfo.Title + "」"))
+ + ";直接子窗口 " + children.Count + " 个"
+ + (sameClassTotal > 0
+ ? ";在父窗口同类子窗口中第 " + sameClassSeen + " 个(同类共 " + sameClassTotal + " 个)"
+ : string.Empty);
+ }
+ catch (Exception ex)
+ {
+ info.HierarchyText = "层级读取失败:" + ex.GetType().Name + ": " + ex.Message;
+ }
+ }
+
+ /// Compatibility helper for callers that need the window class as text.
+ private static string ReadClassName(IntPtr handle)
+ {
+ try
+ {
+ var builder = new System.Text.StringBuilder(256);
+ NativeMethods.GetClassName(handle, builder, builder.Capacity);
+ return builder.ToString();
+ }
+ catch (Exception)
+ {
+ return string.Empty;
+ }
+ }
+
public NativeWindowInfo Capture(IntPtr handle)
{
return Capture(handle, null);
@@ -136,6 +215,11 @@ namespace pacs_win_uia.Services
NativeMethods.GetAncestor(handle, NativeMethods.GA_ROOT));
result.IsVisible = NativeMethods.IsWindowVisible(handle);
result.IsMinimized = NativeMethods.IsIconic(handle);
+ // 状态与样式都是本地调用(不跨进程),随窗口快照一起取:判断"为什么命中的是这一层"很有用。
+ result.IsEnabled = NativeMethods.IsWindowEnabled(handle);
+ result.IsMaximized = NativeMethods.IsZoomed(handle);
+ result.Style = "0x" + NativeMethods.GetWindowStyle(handle).ToString("X8");
+ result.ExStyle = "0x" + NativeMethods.GetWindowExStyle(handle).ToString("X8");
result.WindowBounds = ReadWindowRectangle(handle, errors);
result.ClientBounds = ReadClientRectangle(handle, errors);
result.IsOwnProcess = result.ProcessId == _excludedProcessId;
diff --git a/src/pacs_win_uia/Services/TargetProbeService.cs b/src/pacs_win_uia/Services/TargetProbeService.cs
index 36b75b1..180783d 100644
--- a/src/pacs_win_uia/Services/TargetProbeService.cs
+++ b/src/pacs_win_uia/Services/TargetProbeService.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
@@ -153,6 +154,8 @@ namespace pacs_win_uia.Services
: result.AutomationElement.Error ?? "UIA 元素不可访问,MSAA 也未命中;已保留 Win32 窗口信息。");
}
+ FillHierarchyFrames(result, deepest, deepestHandle);
+
if (result.IsWindowOnlyPick)
{
AppLog.Warn("拾取:光标下只能确定到窗口「" + deepest + "」。"
@@ -242,6 +245,54 @@ namespace pacs_win_uia.Services
}
}
+
+ ///
+ /// 填"拾取层级框":蓝色 = 光标所在窗口(最深层 Win32 窗口),黄色 = 从被拾取控件向上遍历的控件链。
+ /// 只有 UIA 命中到元素时才有黄框;只有窗口信息时至少给一个蓝框,操作者仍然知道自己在哪个窗口里。
+ ///
+ // 层级框:蓝=窗口,黄=控件链(UIA 可用时)。
+ private void FillHierarchyFrames(PointPickResult result, NativeWindowInfo window, IntPtr windowHandle)
+ {
+ if (result == null)
+ {
+ return;
+ }
+
+ var frames = new List();
+ if (window != null && window.WindowBounds != null && window.WindowBounds.IsValid)
+ {
+ frames.Add(new PickFrameInfo
+ {
+ Bounds = window.WindowBounds,
+ IsWindowLevel = true,
+ Label = "窗口:" + (string.IsNullOrEmpty(window.ClassName) ? "(无类名)" : window.ClassName)
+ + " " + window.HwndHex
+ });
+ }
+
+ UiaElementInfo element = result.AutomationElement;
+ if (element != null && element.IsAvailable && element.Element != null)
+ {
+ try
+ {
+ foreach (PickFrameInfo frame in _uiAutomationService.DescribeAncestorFrames(
+ element.Element, windowHandle, MaxHierarchyLevels))
+ {
+ frames.Add(frame);
+ }
+ }
+ catch (Exception ex)
+ {
+ AppLog.Debug("拾取层级遍历失败:" + ex.GetType().Name + ": " + ex.Message);
+ }
+ }
+
+ result.SetHierarchyFrames(frames);
+ }
+
+ // 控件链最多显示几层:每层都是一次跨进程读取,别让悬停变慢。
+ private const int MaxHierarchyLevels = 6;
+
///
/// Win32-only resolution of a screen point: no UIA/MSAA call, so it always returns quickly.
/// Used when a UIA probe does not come back in time (an unresponsive provider must not be
diff --git a/src/pacs_win_uia/Services/UiAutomationService.cs b/src/pacs_win_uia/Services/UiAutomationService.cs
index 3ce153f..2c53749 100644
--- a/src/pacs_win_uia/Services/UiAutomationService.cs
+++ b/src/pacs_win_uia/Services/UiAutomationService.cs
@@ -131,6 +131,18 @@ namespace pacs_win_uia.Services
}
}
+ // 选中状态随控件树一起读(SelectionItemPattern 在缓存请求里):
+ // XPath 的 @IsSelected、取值键"选中状态"以及列表读取都依赖它。
+ try
+ {
+ object selectionItem;
+ if (element.TryGetCachedPattern(SelectionItemPattern.Pattern, out selectionItem))
+ {
+ snapshot.IsSelected = ((SelectionItemPattern)selectionItem).Cached.IsSelected;
+ }
+ }
+ catch (Exception) { /* 不支持该 Pattern 的控件保持 null。 */ }
+
// RuntimeIdProperty participates in the same batch cache request. Older providers
// may omit it; leave the id unset in that case, never substitute it for a differing id.
try
@@ -167,6 +179,384 @@ namespace pacs_win_uia.Services
}
// 广度优先(≤3 层、≤100 节点)查找真正承载内容的内层窗口。
+ ///
+ /// 从被拾取元素向上遍历,产出"控件链"的黄框(元素自身 → 父 → 祖父 → …,最多 maxLevels 层);
+ /// 到顶层窗口元素就停(窗口那一层由拾取器画蓝框)。
+ /// 每层一次属性读取(都是跨进程调用),所以层数与调用次数都有上限。
+ ///
+ // 向上遍历控件链:产出黄框(不含顶层窗口自身)。
+ public IList DescribeAncestorFrames(AutomationElement picked, IntPtr stopHandle, int maxLevels)
+ {
+ var frames = new List();
+ if (picked == null)
+ {
+ return frames;
+ }
+
+ if (maxLevels <= 0)
+ {
+ maxLevels = 6;
+ }
+
+ AutomationElement current = picked;
+ for (int level = 0; current != null && level < maxLevels; level++)
+ {
+ try
+ {
+ AutomationElement.AutomationElementInformation info = current.Current;
+ bool isWindow = info.ControlType == ControlType.Window;
+ IntPtr handle = new IntPtr(info.NativeWindowHandle);
+ if (isWindow && stopHandle != IntPtr.Zero && handle == stopHandle)
+ {
+ // 光标所在窗口由拾取器画蓝框,链上不再重复这一层。
+ break;
+ }
+
+ System.Windows.Rect rect = info.BoundingRectangle;
+ if (!rect.IsEmpty && rect.Width > 1 && rect.Height > 1)
+ {
+ frames.Add(new PickFrameInfo
+ {
+ Bounds = ToRectangleInfo(rect),
+ IsWindowLevel = false,
+ Label = (info.ControlType == null ? "(未知类型)" : info.ControlType.ProgrammaticName)
+ + (string.IsNullOrEmpty(info.Name) ? string.Empty : "「" + info.Name + "」")
+ + (string.IsNullOrEmpty(info.ClassName) ? string.Empty : " " + info.ClassName)
+ });
+ }
+
+ current = TreeWalker.RawViewWalker.GetParent(current);
+ }
+ catch (Exception ex)
+ {
+ AppLog.Debug("控件链遍历中断:" + FormatException(ex));
+ break;
+ }
+ }
+
+ return frames;
+ }
+
+ ///
+ /// 从"容器"的 SelectionPattern 反推每个子项的选中状态,写回快照的 IsSelected。
+ ///
+ /// 为什么需要:不少提供者(实测 WinForms ListView/DataGridView)只在列表容器上暴露
+ /// SelectionPattern,行本身没有 SelectionItemPattern,于是 @IsSelected 一片未知、
+ /// "选中状态"取值也报"无法判断"。这里在树读完之后,对每个有子项的容器取一次选中集合,
+ /// 按 RuntimeId 比对标记子项,XPath 就能写 //DataItem[@IsSelected='true'] 了。
+ /// 每个容器一次跨进程调用,异常一律吞掉(只影响这一个字段)。
+ ///
+ // 容器选中反推:每个容器一次 GetSelection(),按 RuntimeId 比对子项。
+ public void FillContainerSelection(ControlTreeNode root)
+ {
+ if (root == null)
+ {
+ return;
+ }
+
+ var stack = new Stack();
+ stack.Push(root);
+ int containers = 0;
+ while (stack.Count > 0 && containers < 200)
+ {
+ ControlTreeNode node = stack.Pop();
+ foreach (ControlTreeNode child in node.Children)
+ {
+ stack.Push(child);
+ }
+
+ if (node.Source != ControlTreeSource.Uia || node.Uia == null || node.Uia.Element == null ||
+ node.Children.Count == 0)
+ {
+ continue;
+ }
+
+ try
+ {
+ object pattern;
+ if (!node.Uia.Element.TryGetCurrentPattern(SelectionPattern.Pattern, out pattern))
+ {
+ continue;
+ }
+
+ containers++;
+ AutomationElement[] selected = ((SelectionPattern)pattern).Current.GetSelection();
+ var selectedIds = new HashSet(StringComparer.Ordinal);
+ if (selected != null)
+ {
+ foreach (AutomationElement item in selected)
+ {
+ string id = RuntimeIdText(item);
+ if (!string.IsNullOrEmpty(id))
+ {
+ selectedIds.Add(id);
+ }
+ }
+ }
+
+ foreach (ControlTreeNode child in node.Children)
+ {
+ if (child.Source != ControlTreeSource.Uia || child.Uia == null || child.Uia.Element == null)
+ {
+ continue;
+ }
+
+ if (!child.Uia.IsSelected.HasValue)
+ {
+ string id = RuntimeIdText(child.Uia.Element);
+ child.Uia.IsSelected = !string.IsNullOrEmpty(id) && selectedIds.Contains(id);
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ AppLog.Debug("容器选中反推失败(" + node.Uia.ControlType + "):" + FormatException(ex));
+ }
+ }
+ }
+
+ // 读一个元素的 RuntimeId 文本(失败返回 null,用于身份比对)。
+ private static string RuntimeIdText(AutomationElement element)
+ {
+ try
+ {
+ int[] id = element.GetRuntimeId();
+ return id == null || id.Length == 0
+ ? null
+ : string.Join(".", Array.ConvertAll(id, value => value.ToString(System.Globalization.CultureInfo.InvariantCulture)));
+ }
+ catch (Exception)
+ {
+ return null;
+ }
+ }
+
+ ///
+ /// 按需补齐"控件属性"里要显示的补充信息:层级关系(父元素、三个视图的子元素数)与其他模式值
+ /// (RangeValue/Toggle/选中情况)。全部只在选中节点时调用,每项单独 try/catch——
+ /// 某个 Pattern 不支持或某个属性读失败,都不该影响其他字段。
+ ///
+ // 按需填充展示字段(选中节点时调用;每一步都跨进程,所以不进树快照)。
+ public void FillDisplayExtras(UiaElementInfo info)
+ {
+ if (info == null || info.Element == null)
+ {
+ return;
+ }
+
+ AutomationElement element = info.Element;
+ info.ParentSummary = DescribeParentSummary(element);
+ info.ViewCounts = DescribeViewCounts(element);
+ info.RangeValueText = ReadRangeValueText(element);
+ info.ToggleStateText = ReadToggleStateText(element);
+ info.SelectionText = DescribeSelection(element);
+ info.ListSummary = DescribeListSummary(element);
+ }
+
+ // 列表概况:Grid/Table 的行列数 + 选中项数 + 首行文本;不是列表就返回 null(属性网格里显示为空)。
+ private static string DescribeListSummary(AutomationElement element)
+ {
+ try
+ {
+ int? rows = null;
+ int? columns = null;
+ object pattern;
+ if (element.TryGetCurrentPattern(GridPattern.Pattern, out pattern))
+ {
+ GridPattern.GridPatternInformation grid = ((GridPattern)pattern).Current;
+ rows = grid.RowCount;
+ columns = grid.ColumnCount;
+ }
+ else if (element.TryGetCurrentPattern(TablePattern.Pattern, out pattern))
+ {
+ TablePattern.TablePatternInformation table = ((TablePattern)pattern).Current;
+ rows = table.RowCount;
+ columns = table.ColumnCount;
+ }
+
+ if (!rows.HasValue && !columns.HasValue)
+ {
+ return null;
+ }
+
+ var text = new System.Text.StringBuilder();
+ text.Append(rows.HasValue ? rows.Value.ToString() : "?")
+ .Append(" 行 × ")
+ .Append(columns.HasValue ? columns.Value.ToString() : "?")
+ .Append(" 列");
+
+ if (element.TryGetCurrentPattern(SelectionPattern.Pattern, out pattern))
+ {
+ AutomationElement[] selected = ((SelectionPattern)pattern).Current.GetSelection();
+ text.Append(";选中 ").Append(selected == null ? 0 : selected.Length).Append(" 项");
+ if (selected != null && selected.Length > 0)
+ {
+ try
+ {
+ string name = selected[0].Current.Name;
+ if (!string.IsNullOrEmpty(name))
+ {
+ text.Append(",首项「").Append(name).Append("」");
+ }
+ }
+ catch (Exception)
+ {
+ // 首项读不到名称不影响概况。
+ }
+ }
+ }
+
+ return text.ToString();
+ }
+ catch (Exception)
+ {
+ return null;
+ }
+ }
+
+ // 父元素(RawView 上溯一级):类型 + 名称 + 类名,便于判断"层级和想象的是否一致"。
+ private static string DescribeParentSummary(AutomationElement element)
+ {
+ try
+ {
+ AutomationElement parent = TreeWalker.RawViewWalker.GetParent(element);
+ if (parent == null)
+ {
+ return "(没有父元素:已经是根)";
+ }
+
+ AutomationElement.AutomationElementInformation current = parent.Current;
+ string type = current.ControlType == null ? "(未知类型)" : current.ControlType.ProgrammaticName;
+ return type
+ + (string.IsNullOrEmpty(current.AutomationId) ? string.Empty : "[" + current.AutomationId + "]")
+ + (string.IsNullOrEmpty(current.Name) ? string.Empty : " 名称「" + current.Name + "」")
+ + (string.IsNullOrEmpty(current.ClassName) ? string.Empty : " 类名=" + current.ClassName);
+ }
+ catch (Exception ex)
+ {
+ return "父元素读取失败:" + FormatException(ex);
+ }
+ }
+
+ // 三个视图的直接子元素数:控件树用 RawView;数字对不上时就是"视图不同",不是"读不到"。
+ private static string DescribeViewCounts(AutomationElement element)
+ {
+ return "RawView=" + CountChildren(element, TreeWalker.RawViewWalker)
+ + ";ControlView=" + CountChildren(element, TreeWalker.ControlViewWalker)
+ + ";ContentView=" + CountChildren(element, TreeWalker.ContentViewWalker);
+ }
+
+ private static string CountChildren(AutomationElement element, TreeWalker walker)
+ {
+ try
+ {
+ int count = 0;
+ AutomationElement child = walker.GetFirstChild(element);
+ while (child != null && count < 5000)
+ {
+ count++;
+ child = walker.GetNextSibling(child);
+ }
+
+ return count.ToString(System.Globalization.CultureInfo.InvariantCulture);
+ }
+ catch (Exception ex)
+ {
+ return "(读不到:" + ex.GetType().Name + ")";
+ }
+ }
+
+ // RangeValuePattern:滑块/进度/微调框的当前值与范围。
+ private static string ReadRangeValueText(AutomationElement element)
+ {
+ try
+ {
+ object pattern;
+ if (!element.TryGetCurrentPattern(RangeValuePattern.Pattern, out pattern))
+ {
+ return null;
+ }
+
+ RangeValuePattern.RangeValuePatternInformation current = ((RangeValuePattern)pattern).Current;
+ return current.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)
+ + "(范围 " + current.Minimum.ToString(System.Globalization.CultureInfo.InvariantCulture)
+ + " ~ " + current.Maximum.ToString(System.Globalization.CultureInfo.InvariantCulture)
+ + (current.IsReadOnly ? ",只读" : string.Empty) + ")";
+ }
+ catch (Exception)
+ {
+ return null;
+ }
+ }
+
+ // TogglePattern:复选框/开关按钮的开-关-不确定状态。
+ private static string ReadToggleStateText(AutomationElement element)
+ {
+ try
+ {
+ object pattern;
+ if (!element.TryGetCurrentPattern(TogglePattern.Pattern, out pattern))
+ {
+ return null;
+ }
+
+ ToggleState state = ((TogglePattern)pattern).Current.ToggleState;
+ switch (state)
+ {
+ case ToggleState.On: return "开";
+ case ToggleState.Off: return "关";
+ default: return "不确定";
+ }
+ }
+ catch (Exception)
+ {
+ return null;
+ }
+ }
+
+ // 选中情况:单项(列表行/树节点)看 IsSelected,容器看"选中了几项 + 首项文本"。
+ private static string DescribeSelection(AutomationElement element)
+ {
+ try
+ {
+ object item;
+ if (element.TryGetCurrentPattern(SelectionItemPattern.Pattern, out item))
+ {
+ bool selected = ((SelectionItemPattern)item).Current.IsSelected;
+ return selected ? "该项已被选中" : "该项未被选中";
+ }
+
+ object selection;
+ if (element.TryGetCurrentPattern(SelectionPattern.Pattern, out selection))
+ {
+ AutomationElement[] items = ((SelectionPattern)selection).Current.GetSelection();
+ if (items == null || items.Length == 0)
+ {
+ return "容器:当前没有选中项";
+ }
+
+ string first = null;
+ try
+ {
+ first = items[0].Current.Name;
+ }
+ catch (Exception)
+ {
+ // 首项读不到名称不影响数量统计。
+ }
+
+ return "容器:选中 " + items.Length + " 项"
+ + (string.IsNullOrEmpty(first) ? string.Empty : ",首项「" + first + "」");
+ }
+ }
+ catch (Exception ex)
+ {
+ return "选中情况读取失败:" + FormatException(ex);
+ }
+
+ return null;
+ }
+
///
/// The nested window element that actually hosts an app's content, if any. A UWP window
/// (ApplicationFrameWindow -> Windows.UI.Core.CoreWindow) and a browser shell both expose the
diff --git a/src/pacs_win_uia/UI/MainForm.cs b/src/pacs_win_uia/UI/MainForm.cs
index 2494a76..c406ea1 100644
--- a/src/pacs_win_uia/UI/MainForm.cs
+++ b/src/pacs_win_uia/UI/MainForm.cs
@@ -2289,9 +2289,17 @@ namespace pacs_win_uia
element.ControlPath = DescribeNodePath(node);
// 控件属性里直接给出内容文本(输入框/富文本的内容),与取值键「文本内容」同源。
element.TextContent = ControlValueReader.TryGetTextContent(element);
+ // 层级关系(父元素 / 三个视图的子元素数)与其他模式值(范围/开关/选中),按需读取。
+ _uiAutomationService.FillDisplayExtras(element);
element.PageFeatures = ControlLocatorBuilder.DescribePageFeatures(node.Window ?? _inspectedWindow);
}
+ // Win32 节点:按需补层级(父窗口/子窗口数/同类序号)。属性网格显示的就是窗口模型。
+ if (node != null && node.Window != null)
+ {
+ _windowService.FillDisplayExtras(node.Window);
+ }
+
_currentNode = node;
UpdatePropertyCaption(node);
diff --git a/src/pacs_win_uia/UI/PickHierarchyOverlayForm.cs b/src/pacs_win_uia/UI/PickHierarchyOverlayForm.cs
new file mode 100644
index 0000000..861312e
--- /dev/null
+++ b/src/pacs_win_uia/UI/PickHierarchyOverlayForm.cs
@@ -0,0 +1,270 @@
+// ============================================================================
+// PickHierarchyOverlayForm —— 拾取时的"多层框"覆盖层。
+//
+// 职责:拾取(十字拾取器)悬停预览时显示层级关系:
+// · 蓝色框 = 光标所在窗口(Win32 最深层窗口);
+// · 黄色框 = 从被拾取控件向上遍历的控件链(元素自身 → 父 → 祖父 → …)。
+// 这样操作者一眼就能看出"我要的那个控件在哪一层",而不用先拾取再回控件树里翻。
+// 被谁调用:Picker/CrosshairPicker.cs(每次预览探测完成后 ShowFrames)。
+// 线程/节拍:所有方法都在 UI 线程调用;120ms 的跟随节拍只读 GetWindowRect,
+// 不做任何跨进程 UIA 调用(矩形在 ShowFrames 时就已换算成"相对锚点窗口的偏移")。
+// 不抢焦点:WS_EX_NOACTIVATE + WS_EX_TOOLWINDOW + WS_EX_TRANSPARENT,ShowWithoutActivation。
+// 要改什么就改哪里:
+// · 改颜色/线宽/是否带标签 → OnPaint;
+// · 改跟随与隐藏条件(最小化/被遮挡/窗口关闭)→ OnTimerTick;
+// · 内容来源是 PointPickResult.HierarchyFrames(见 Services/TargetProbeService.cs)。
+// ============================================================================
+
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Drawing.Drawing2D;
+using System.Windows.Forms;
+using pacs_win_uia.Interop;
+using pacs_win_uia.Models;
+using pacs_win_uia.Services;
+
+namespace pacs_win_uia.UI
+{
+ /// 拾取预览的层级框覆盖层:一层蓝(窗口)+ 若干层黄(控件链)。
+ public sealed class PickHierarchyOverlayForm : Form
+ {
+ // 跟随节拍:只读锚点窗口矩形,内核直接回答,不会被目标进程卡住。
+ private const int FollowIntervalMs = 120;
+ // 框的颜色:蓝 = 窗口层,黄 = 控件链。
+ private static readonly Color WindowLevelColor = Color.DeepSkyBlue;
+ private static readonly Color ChainLevelColor = Color.Gold;
+
+ private readonly System.Windows.Forms.Timer _timer;
+ private readonly List _frames = new List();
+ private readonly int _ownProcessId;
+ private IntPtr _anchor = IntPtr.Zero;
+ private bool _shown;
+
+ // 一层的显示数据:相对锚点窗口的偏移 + 颜色 + 标签。
+ private sealed class Frame
+ {
+ internal Rectangle Offset;
+ internal Color Color;
+ internal string Label;
+ }
+
+ public PickHierarchyOverlayForm()
+ {
+ _ownProcessId = System.Diagnostics.Process.GetCurrentProcess().Id;
+ FormBorderStyle = FormBorderStyle.None;
+ ShowInTaskbar = false;
+ StartPosition = FormStartPosition.Manual;
+ TopMost = true;
+ BackColor = Color.Magenta;
+ TransparencyKey = Color.Magenta;
+ DoubleBuffered = true;
+ _timer = new System.Windows.Forms.Timer { Interval = FollowIntervalMs };
+ _timer.Tick += OnTimerTick;
+ }
+
+ protected override bool ShowWithoutActivation
+ {
+ get { return true; }
+ }
+
+ protected override CreateParams CreateParams
+ {
+ get
+ {
+ CreateParams parameters = base.CreateParams;
+ parameters.ExStyle |= NativeMethods.WS_EX_TOOLWINDOW |
+ NativeMethods.WS_EX_NOACTIVATE |
+ NativeMethods.WS_EX_TRANSPARENT;
+ return parameters;
+ }
+ }
+
+ /// 当前是否显示着层级框。
+ public bool IsShown
+ {
+ get { return _shown; }
+ }
+
+ /// 当前框的层数(测试与状态栏用)。
+ public int FrameCount
+ {
+ get { return _frames.Count; }
+ }
+
+ /// 各层的文字描述(测试与状态栏用)。
+ public string Describe()
+ {
+ if (_frames.Count == 0)
+ {
+ return "(没有层级框)";
+ }
+
+ var text = new System.Text.StringBuilder();
+ foreach (Frame frame in _frames)
+ {
+ text.Append(frame.Color == WindowLevelColor ? "蓝" : "黄")
+ .Append("(").Append(frame.Offset).Append(")").Append(frame.Label).Append(";");
+ }
+
+ return text.ToString();
+ }
+
+ ///
+ /// 显示层级框。 是这些框共同跟随的窗口(光标所在窗口),
+ /// 所有矩形都以"相对该窗口左上角的偏移"保存,因此拖动窗口时不用重新探测。
+ ///
+ public void ShowFrames(IntPtr anchorWindow, IList frames)
+ {
+ _frames.Clear();
+ if (anchorWindow == IntPtr.Zero || frames == null || frames.Count == 0 ||
+ !NativeMethods.IsWindow(anchorWindow))
+ {
+ HideFrames();
+ return;
+ }
+
+ Rectangle anchor;
+ if (!TryGetWindowRectangle(anchorWindow, out anchor))
+ {
+ HideFrames();
+ return;
+ }
+
+ foreach (PickFrameInfo frame in frames)
+ {
+ if (frame == null || frame.Bounds == null || !frame.Bounds.IsValid)
+ {
+ continue;
+ }
+
+ Rectangle screen = frame.Bounds.ToRectangle();
+ if (screen.Width <= 1 || screen.Height <= 1)
+ {
+ continue;
+ }
+
+ _frames.Add(new Frame
+ {
+ Offset = new Rectangle(screen.Left - anchor.Left, screen.Top - anchor.Top, screen.Width, screen.Height),
+ Color = frame.IsWindowLevel ? WindowLevelColor : ChainLevelColor,
+ Label = frame.Label
+ });
+ }
+
+ if (_frames.Count == 0)
+ {
+ HideFrames();
+ return;
+ }
+
+ _anchor = anchorWindow;
+ Bounds = anchor;
+ _shown = true;
+ if (!Visible)
+ {
+ Show();
+ }
+
+ TopMost = true;
+ Invalidate();
+ _timer.Start();
+ }
+
+ /// 收起层级框。
+ public void HideFrames()
+ {
+ _timer.Stop();
+ _shown = false;
+ _anchor = IntPtr.Zero;
+ _frames.Clear();
+ if (Visible)
+ {
+ Hide();
+ }
+ }
+
+ // 跟随节拍:只读锚点窗口矩形;窗口没了/最小化了就收起(下次预览会再显示)。
+ private void OnTimerTick(object sender, EventArgs e)
+ {
+ if (!_shown || _anchor == IntPtr.Zero)
+ {
+ return;
+ }
+
+ if (!NativeMethods.IsWindow(_anchor) || NativeMethods.IsIconic(_anchor))
+ {
+ HideFrames();
+ return;
+ }
+
+ Rectangle anchor;
+ if (!TryGetWindowRectangle(_anchor, out anchor))
+ {
+ HideFrames();
+ return;
+ }
+
+ if (Bounds != anchor)
+ {
+ Bounds = anchor;
+ }
+
+ if (!Visible)
+ {
+ Show();
+ }
+
+ Invalidate();
+ }
+
+ // 画所有框:蓝框 2px,黄框 1px(黄框通常更多、更靠内,细一点不遮内容)。
+ protected override void OnPaint(PaintEventArgs e)
+ {
+ base.OnPaint(e);
+ e.Graphics.SmoothingMode = SmoothingMode.None;
+ foreach (Frame frame in _frames)
+ {
+ if (frame.Offset.Right < 0 || frame.Offset.Bottom < 0 ||
+ frame.Offset.Left > ClientSize.Width || frame.Offset.Top > ClientSize.Height)
+ {
+ continue;
+ }
+
+ float width = frame.Color == WindowLevelColor ? 2.4F : 1.2F;
+ using (var pen = new Pen(frame.Color, width))
+ {
+ // 内缩半像素,避免框压到相邻控件上。
+ var target = new Rectangle(frame.Offset.X, frame.Offset.Y,
+ Math.Max(2, frame.Offset.Width - 1), Math.Max(2, frame.Offset.Height - 1));
+ e.Graphics.DrawRectangle(pen, target);
+ }
+ }
+ }
+
+ private static bool TryGetWindowRectangle(IntPtr handle, out Rectangle rectangle)
+ {
+ rectangle = Rectangle.Empty;
+ NativeMethods.RECT native;
+ if (!NativeMethods.GetWindowRect(handle, out native))
+ {
+ return false;
+ }
+
+ rectangle = new Rectangle(native.Left, native.Top, native.Right - native.Left, native.Bottom - native.Top);
+ return rectangle.Width > 0 && rectangle.Height > 0;
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ _timer.Stop();
+ _timer.Tick -= OnTimerTick;
+ _timer.Dispose();
+ }
+
+ base.Dispose(disposing);
+ }
+ }
+}
diff --git a/src/pacs_win_uia/pacs_win_uia.csproj b/src/pacs_win_uia/pacs_win_uia.csproj
index 0093098..7930d2f 100644
--- a/src/pacs_win_uia/pacs_win_uia.csproj
+++ b/src/pacs_win_uia/pacs_win_uia.csproj
@@ -109,6 +109,7 @@
+
diff --git a/tests/regression/ValueKeyTests.cs b/tests/regression/ValueKeyTests.cs
index afa1a3c..fa64898 100644
--- a/tests/regression/ValueKeyTests.cs
+++ b/tests/regression/ValueKeyTests.cs
@@ -30,6 +30,68 @@ namespace PacsWinUia.RegressionTests
tests.Add(new KeyValuePair("Text content falls back to the snapshot Name", TextContentFallsBackToName));
// 防:新增控件默认 ValueKey 回到 Name;富文本这类控件会读到 "RichEdit Control"。
tests.Add(new KeyValuePair("New monitor entries default to the text content key", BuilderDefaultsToTextContent));
+ // 防:XPath 状态属性(选中行/焦点行)消失或写错,导致 @IsSelected/@IsFocused 语法失效。
+ tests.Add(new KeyValuePair("XPath can select the selected and focused row", RowStateAttributes));
+ // 防:列表信息键被移除,监控里没法盯住"这张表几行、选中几行"。
+ tests.Add(new KeyValuePair("List information keys are offered for UIA", ListKeysOffered));
+ }
+
+
+ // XPath 属性 @IsSelected / @IsFocused / @IsOffscreen / @IsEnabled:
+ // 源图是"选中第 2 行"的列表,应当能唯一命中它。
+ private static void RowStateAttributes()
+ {
+ ControlTreeNode root = Row("Window", "列表窗口");
+ ControlTreeNode grid = Row("DataGrid", "列表", "FixtureListView");
+ root.Children.Add(grid);
+ grid.Children.Add(Row("DataItem", "项目 1", null, selected: false, focused: false));
+ grid.Children.Add(Row("DataItem", "项目 2", null, selected: true, focused: true));
+ grid.Children.Add(Row("DataItem", "项目 3", null, selected: false, focused: false));
+ ControlPath.PreparePaths(root);
+
+ Check.Matches(root, "//DataItem[@IsSelected='true']", grid.Children[1]);
+ Check.Matches(root, "//DataItem[@IsFocused='true']", grid.Children[1]);
+ Check.Matches(root, "//DataItem[@IsSelected='false']", grid.Children[0], grid.Children[2]);
+ Check.Matches(root, "//DataItem[@IsEnabled='true' and @IsOffscreen='false']",
+ grid.Children[0], grid.Children[1], grid.Children[2]);
+ Check.True(ControlValueReader.TryRead(grid.Children[1], ControlValueKeys.TextContent,
+ new ListReadOptions(), null, out string value, out string error) == false || value != null,
+ "选中行取值不应抛异常");
+ }
+
+ private static ControlTreeNode Row(string type, string name, string automationId = null,
+ bool? selected = null, bool? focused = null)
+ {
+ return new ControlTreeNode
+ {
+ Source = ControlTreeSource.Uia,
+ Uia = new UiaElementInfo
+ {
+ ControlType = "ControlType." + type,
+ Name = name,
+ AutomationId = automationId,
+ IsAvailable = true,
+ IsSelected = selected ?? (bool?)null,
+ HasKeyboardFocus = focused ?? (bool?)null,
+ IsOffscreen = false,
+ IsEnabled = true
+ }
+ };
+ }
+
+ private static void ListKeysOffered()
+ {
+ foreach (string key in new[]
+ {
+ ControlValueKeys.ListRowCount, ControlValueKeys.ListColumnCount, ControlValueKeys.ListSelectedCount
+ })
+ {
+ Check.True(ControlValueKeys.For(ControlSourcePreference.Uia).Contains(key),
+ "UIA keys missing " + key);
+ }
+
+ Check.Equal("列表行数", ControlValueKeys.ListRowCount, "Row count key name");
+ Check.Equal("列表选中项数", ControlValueKeys.ListSelectedCount, "Selected count key name");
}
private static void TextContentKeyOffered()
diff --git a/tests/uia-fixture/FixtureForm.cs b/tests/uia-fixture/FixtureForm.cs
index 002bdc7..e1e2cd1 100644
--- a/tests/uia-fixture/FixtureForm.cs
+++ b/tests/uia-fixture/FixtureForm.cs
@@ -41,6 +41,9 @@ namespace UiaFixture
private readonly ListView _list;
private readonly DataGridView _grid;
+ /// 启动时选中列表的第几行(0-based,-1 表示不选)。给自动化验证用。
+ public int SelectRowOnStartup { get; set; }
+
public FixtureForm()
{
Text = "UIA 测试夹具";
@@ -124,6 +127,9 @@ namespace UiaFixture
_list.Items.Add(new ListViewItem(new[] { "项目 " + index, "ID" + index }));
}
+ // 让测试能预先选中某一行(例如验证 XPath 的 @IsSelected / @IsFocused)。
+ SelectRowOnStartup = 0;
+
_tree = new TreeView
{
Name = "FixtureTreeView",
@@ -197,6 +203,18 @@ namespace UiaFixture
tabs.TabPages.Add(gridPage);
tabs.TabPages.Add(nestedPage);
Controls.Add(tabs);
+ Shown += delegate
+ {
+ if (SelectRowOnStartup < 0 || SelectRowOnStartup >= _list.Items.Count)
+ {
+ return;
+ }
+
+ _list.Items[SelectRowOnStartup].Selected = true;
+ _list.Items[SelectRowOnStartup].Focused = true;
+ _list.Select();
+ _list.EnsureVisible(SelectRowOnStartup);
+ };
// A second top-level window so "which window" itself can be tested.
var secondary = new Form
@@ -230,11 +248,24 @@ namespace UiaFixture
internal static class Program
{
[STAThread]
- private static void Main()
+ private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new FixtureForm());
+ var form = new FixtureForm();
+
+ // UiaFixture.exe --select-row 2 → 启动时选中列表第 2 行(1-based),
+ // 用于验证 XPath 的 @IsSelected/@IsFocused 与"选中行"取值。
+ if (args != null && args.Length >= 2 && string.Equals(args[0], "--select-row", StringComparison.Ordinal))
+ {
+ int oneBased;
+ if (int.TryParse(args[1], out oneBased))
+ {
+ form.SelectRowOnStartup = oneBased - 1;
+ }
+ }
+
+ Application.Run(form);
}
}
}