C# UI自动化和菜单项
我正在尝试使用.NETUI自动化。 我有一个我知道是用.NET编写的第三方应用程序,但我没有它的源代码。 我正在启动应用程序,使用 Process.Start(“exe路径”); 以及获取进程ID 然后按搜索主应用程序窗口C# UI自动化和菜单项,c#,.net,ui-automation,C#,.net,Ui Automation,我正在尝试使用.NETUI自动化。 我有一个我知道是用.NET编写的第三方应用程序,但我没有它的源代码。 我正在启动应用程序,使用 Process.Start(“exe路径”); 以及获取进程ID 然后按搜索主应用程序窗口 this.MainWindow = AutomationElement.RootElement.FindFirst (TreeScope.Children, new AndCondition(
this.MainWindow = AutomationElement.RootElement.FindFirst
(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ProcessIdProperty, this.ProcessId),
new PropertyCondition(AutomationElement.NameProperty, InitialWindowName)
));
这是工作发现
但在主窗口中,有一个菜单栏,其中有一个常见的“文件,编辑,…”
因此,下一步,我选择菜单栏并用展开文件菜单
var menuBar = this.MainWindow.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "menu bar"));
var fileMenu = menuBar.FindAll(TreeScope.Children, Condition.TrueCondition)[0];
var expandPattern = fileMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
if (expandPattern.Current.ExpandCollapseState != ExpandCollapseState.Expanded)
expandPattern.Expand();
Thread.Sleep(3000);
由于“文件”菜单选项是菜单栏中的第一个选项,因此这是在扩展“文件”菜单选项
现在,我想调用“文件”菜单列表中的“打印”菜单项
“打印”菜单项的名称为“打印文档Ctrl+p”
所以我寻找
var printMenuItem = this.MainWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty,"Print Document Ctrl+P"));
但是没有成功。
我尝试了不同的方法,比如获取所有项目的子代,并循环遍历名称,以查找它们是否有“打印”内容,但没有成功,如下图所示
var list = this.MainWindow.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.LocalizedControlTypeProperty,"menu item"));
for (int i = 0; i < list.count; i++)
{
if (list[0].Current.Name.IndexOf("Print") > -1)...
var list=this.MainWindow.FindAll(TreeScope.subjects,新属性条件(AutomationElement.LocalizedControlTypeProperty,“菜单项”);
对于(int i=0;i-1)。。。
我试过了,找到了打印菜单
var printMenu = fileMenu.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Print"));