Files
zimoyin ebbf9e1335 选中行/焦点行语法、列表信息键、拾取层级框、控件属性补全
1) 新增「选中行/焦点行」语法,控件监控新增列表信息
   - XPath 快照新增状态属性:@IsSelected / @IsFocused / @IsOffscreen / @IsEnabled
     例如 /Window[1]//DataGrid[@AutomationId='x']/DataItem[@IsSelected='true']
   - IsSelected 随控件树一起从缓存读(SelectionItemPattern);对只在容器上暴露
     SelectionPattern 的提供者(实测 WinForms ListView/DataGridView),树读完后
     由容器反推每个子项的选中状态,@IsSelected 与「选中状态」取值因此可用
   - 新增取值键:列表行数 / 列表列数 / 列表选中项数(Grid → Table → Selection)
   - 控件属性新增「列表概况」(行×列 + 选中项数 + 首项文本)

2) 拾取时显示层级:蓝框=光标所在窗口,黄框=向上遍历的控件链
   - PointPickResult 增加 HierarchyFrames;TargetProbeService 填蓝色窗口层 +
     UiAutomationService.DescribeAncestorFrames 产出的黄色控件链(最多 6 层)
   - 新增 UI/PickHierarchyOverlayForm:跟随锚点窗口、只读 GetWindowRect、
     最小化/关闭即收起、置顶不抢焦点、点击穿透
   - CrosshairPicker 预览改用层级框;只有 Win32/MSAA 信息时退回原来的单框高亮

3) 控件属性按 UIA/Win32 分类补全(没有的就新增)
   - UIA:父元素、RawView/ControlView/ContentView 子元素数、RangeValue、
     Toggle 状态、选中情况、列表概况(全部按需读取,选中节点时才跨进程)
   - Win32:样式 GWL_STYLE、扩展样式 GWL_EXSTYLE、启用、最大化、层级关系
     (父窗口 / 直接子窗口数 / 同类序号)
   - GetWindowStyle 原本读的其实是 GWL_EXSTYLE,已正名为 GetWindowExStyle,
     GWL_STYLE 用新方法读取

实测(跨进程,目标为夹具/记事本):
- 夹具列表选中第 2 行:DataItem[2] IsSelected=True;@IsSelected='true' 唯一命中并
  取到「项目 2」;列表行数=3、列表列数=2、列表选中项数=1
- 拾取夹具列表行:7 层框(1 蓝窗口 + 6 黄控件链),覆盖层实际绘制并截图确认
- 记事本文档:控件属性显示 文本内容="agfa\r\raaaaa"(Name 是「文本编辑器」)

回归 29/29 通过
2026-10-13 19:19:16 +08:00

272 lines
11 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// =====================================================================================
// UiaFixture —— 跨进程验证用的真实 WinForms 夹具(单独的可执行程序)。
//
// 为什么必须是独立进程:本工具靠 UIA 读取“别的进程”的控件树,而 UIA 在**同一进程内**
// 自读自己的控件树会崩溃(见 MEMORY:in-process UIA self-reads segfault)。
// 因此需要一个真正独立的进程暴露一棵已知且稳定的控件树,让工具跨进程去读、去拾取、
// 去按 path 定位——这样测的才是生产路径。夹具自身不做任何 UIA 调用。
//
// 这棵树的控件类型/名称/AutomationId 都固定,且窗口标题与类名在多次运行间保持不变,
// 所以保存的监控条目、悬浮条显示条件可以长期复用它。
//
// 打开方式:tests\run-fixture.cmd(或用 Rider/VS 直接跑本项目)→ 工具里
// 刷新窗口 → 选中「UIA 测试夹具」→ 检查选中窗口;验证清单见 tests\README.md。
// =====================================================================================
using System;
using System.Drawing;
using System.Windows.Forms;
namespace UiaFixture
{
/// <summary>
/// A deliberately ordinary window for exercising the tool: every kind of control the tool has to
/// read (button, text box, checkbox, combo, radio, tree, list, grid, tabs) in one window, with
/// stable names and AutomationIds so a saved path keeps resolving.
///
/// This exists because the PACS/OCS clients either expose nothing (CEF shells) or expose a large
/// tree nobody wants to hand-verify against; here the expected answer is known.
///
/// Run it, then in the tool: 刷新窗口 → select "UIA 测试夹具" → 检查选中窗口. The window title and
/// class stay the same between runs, so saved monitor entries and floating-bar conditions can be
/// built against it.
/// </summary>
public sealed class FixtureForm : Form
{
private readonly Label _status;
private readonly TextBox _input;
private readonly RichTextBox _rich;
private readonly CheckBox _check;
private readonly ComboBox _combo;
private readonly TreeView _tree;
private readonly ListView _list;
private readonly DataGridView _grid;
/// <summary>启动时选中列表的第几行(0-based,-1 表示不选)。给自动化验证用。</summary>
public int SelectRowOnStartup { get; set; }
public FixtureForm()
{
Text = "UIA 测试夹具";
Name = "FixtureForm";
StartPosition = FormStartPosition.Manual;
Location = new Point(60, 60);
Size = new Size(760, 620);
MinimumSize = new Size(600, 480);
var tabs = new TabControl { Dock = DockStyle.Fill, Name = "FixtureTabs" };
// ---- page 1: the everyday controls
var page = new TabPage("控件") { Name = "ControlsPage" };
var panel = new Panel { Dock = DockStyle.Fill, Padding = new Padding(10) };
var button = new Button
{
Text = "测试按钮",
Name = "FixtureButton",
AutoSize = true,
Location = new Point(12, 12)
};
button.Click += delegate { SetStatus("按钮被点击:" + DateTime.Now.ToString("HH:mm:ss")); };
_input = new TextBox
{
Name = "FixtureTextBox",
Text = "可编辑文本",
Location = new Point(140, 14),
Width = 220
};
_input.TextChanged += delegate { SetStatus("文本框内容已改为:" + _input.Text); };
// 多行富文本:内容较长,用来验证"内容在 Name/Value/Text 里"的取值与拾取定位。
_rich = new RichTextBox
{
Name = "FixtureRichText",
Text = "富文本第一行" + Environment.NewLine + "富文本第二行",
Location = new Point(380, 14),
Size = new Size(240, 60)
};
_check = new CheckBox
{
Text = "启用选项",
Name = "FixtureCheckBox",
Location = new Point(12, 50),
AutoSize = true
};
_check.CheckedChanged += delegate { SetStatus("复选框:" + (_check.Checked ? "选中" : "未选中")); };
var radioA = new RadioButton { Text = "选项 A", Name = "FixtureRadioA", Location = new Point(140, 50), AutoSize = true, Checked = true };
var radioB = new RadioButton { Text = "选项 B", Name = "FixtureRadioB", Location = new Point(240, 50), AutoSize = true };
radioA.CheckedChanged += delegate { SetStatus("单选:A"); };
radioB.CheckedChanged += delegate { SetStatus("单选:B"); };
_combo = new ComboBox
{
Name = "FixtureComboBox",
DropDownStyle = ComboBoxStyle.DropDownList,
Location = new Point(12, 80),
Width = 200
};
_combo.Items.AddRange(new object[] { "第一项", "第二项", "第三项" });
_combo.SelectedIndex = 0;
_combo.SelectedIndexChanged += delegate { SetStatus("下拉选择:" + _combo.SelectedItem); };
var listLabel = new Label { Text = "列表(两列)", AutoSize = true, Location = new Point(12, 116) };
_list = new ListView
{
Name = "FixtureListView",
View = View.Details,
FullRowSelect = true,
Location = new Point(12, 138),
Size = new Size(320, 150)
};
_list.Columns.Add("姓名", 120);
_list.Columns.Add("编号", 100);
for (int index = 1; index <= 3; index++)
{
_list.Items.Add(new ListViewItem(new[] { "项目 " + index, "ID" + index }));
}
// 让测试能预先选中某一行(例如验证 XPath 的 @IsSelected / @IsFocused)。
SelectRowOnStartup = 0;
_tree = new TreeView
{
Name = "FixtureTreeView",
Location = new Point(348, 138),
Size = new Size(360, 150)
};
TreeNode root = _tree.Nodes.Add("根节点");
root.Nodes.Add("子节点 1");
root.Nodes.Add("子节点 2");
root.ExpandAll();
_status = new Label
{
Name = "FixtureStatusLabel",
Text = "还没有操作。",
AutoSize = false,
Height = 22,
Location = new Point(12, 296),
Width = 700,
ForeColor = Color.DarkSlateGray
};
panel.Controls.Add(button);
panel.Controls.Add(_input);
panel.Controls.Add(_rich);
panel.Controls.Add(_check);
panel.Controls.Add(radioA);
panel.Controls.Add(radioB);
panel.Controls.Add(_combo);
panel.Controls.Add(listLabel);
panel.Controls.Add(_list);
panel.Controls.Add(_tree);
panel.Controls.Add(_status);
page.Controls.Add(panel);
// ---- page 2: a grid, for the list reader
var gridPage = new TabPage("表格") { Name = "GridPage" };
_grid = new DataGridView
{
Name = "FixtureDataGrid",
Dock = DockStyle.Fill,
AllowUserToAddRows = false,
ReadOnly = true,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
};
_grid.Columns.Add("序号", "序号");
_grid.Columns.Add("内容", "内容");
_grid.Columns.Add("状态", "状态");
for (int index = 1; index <= 5; index++)
{
_grid.Rows.Add(index.ToString(), "第 " + index + " 行内容", index % 2 == 0 ? "已完成" : "待处理");
}
gridPage.Controls.Add(_grid);
// ---- page 3: a nested container, for "the list is inside the picked element"
var nestedPage = new TabPage("嵌套") { Name = "NestedPage" };
var outer = new Panel { Dock = DockStyle.Fill, Padding = new Padding(8), Name = "NestedOuter" };
var middle = new Panel { Dock = DockStyle.Fill, Padding = new Padding(8), Name = "NestedMiddle", BackColor = Color.WhiteSmoke };
var innerList = new ListBox { Dock = DockStyle.Fill, Name = "NestedListBox" };
for (int index = 1; index <= 4; index++)
{
innerList.Items.Add("嵌套列表项 " + index);
}
middle.Controls.Add(innerList);
outer.Controls.Add(middle);
nestedPage.Controls.Add(outer);
tabs.TabPages.Add(page);
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
{
Text = "UIA 测试夹具 - 第二个窗口",
Name = "FixtureSecondWindow",
Owner = this,
StartPosition = FormStartPosition.Manual,
Location = new Point(840, 60),
Size = new Size(360, 220)
};
var secondaryButton = new Button
{
Text = "第二个窗口的按钮",
Name = "FixtureSecondButton",
Location = new Point(20, 24),
AutoSize = true
};
secondaryButton.Click += delegate { SetStatus("第二个窗口的按钮被点击。"); };
secondary.Controls.Add(secondaryButton);
secondary.Shown += delegate { secondary.Location = new Point(Location.X + Width + 20, Location.Y); };
AddOwnedForm(secondary);
}
private void SetStatus(string text)
{
_status.Text = DateTime.Now.ToString("HH:mm:ss") + " " + text;
}
}
internal static class Program
{
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
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);
}
}
}