使用 UI Automation 在没有 InvokePattern 或 clickablePoint 的情况下单击按钮调用
Invoke on click on a button using UI Automation with no InvokePattern or clickablePoint
我正在尝试将点击消息发送到(或调用)另一个应用程序中的按钮。
我使用了UISpy.exe并且可以找到我需要的元素。
但它没有 ID,没有可点击点,也没有调用模式。
我尝试了以下代码:
var processStartInfo = new ProcessStartInfo(@"tdesktop\Program.exe");
var proc = Process.Start(processStartInfo);
Thread.Sleep(3000);
AutomationElement mainWin = AutomationElement.RootElement.FindChildByProcessId(proc.Id);
List<AutomationElement> elmList= GetChildren(mainWin);
//MessageBox.Show(elmList.Count.ToString());
if (elmList.Count == 7)
{
List<AutomationElement> menubar= GetChildren(elmList[6]);
AutomationElement elementNode = menubar[1];
double x = elementNode.GetClickablePoint().X;
double y = elementNode.GetClickablePoint().Y;
win32 w = new win32();
w.move_left_click((UInt32)x, (UInt32)y);
}
它在 elementNode.GetClickablePoint().X 中抛出一个异常,即 Autumation 元素没有可点击的点。
我也试过 TryGetInvokePattern() 但仍然抛出执行它没有 InvokePattern。
我使用 VS2012 和 .net 4.5
有什么办法可以做到吗?
菜单栏不公开 InvokePattern (see UI Automation Support for the MenuBar Control Type). However, a menu item can be Invoked (see UI Automation Support for the MenuItem Control Type).
以下代码说明了如何生成菜单项列表:
AutomationElementCollection items = menubar.FindAll(
TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem));
如前所述,我强烈建议将 Inspect SDK 工具指向您感兴趣的 UI。该工具 (inspect.exe) 可以在 [=32] 等地方找到=].
通过使用该工具,您可以看到感兴趣的 UI 元素是如何以编程方式公开的。例如,它是作为单个 UIA 元素公开的,还是更大的 UIA 元素的一部分,代表一组 UI 元素的视觉显示,以及什么 UI它支持A模式吗?作为测试,我只是将 Inspect 指向 Paint 中的箭头形状。结果如下所示。
所以我可以看出箭头是作为单个 UIA 元素以编程方式公开的,并且它支持调用模式。这意味着我可以通过 UIA 以编程方式调用按钮。 (而且我可以从 Inspect 的 Action 菜单中调用元素的模式方法,这非常方便。)如果 UIA 元素不支持任何允许我以编程方式控制它的模式,我可以找到它的 BoundingRectangle 属性 到 UIA,并在中间模拟鼠标单击以调用它。 (我假设当我模拟鼠标点击时按钮没有被遮挡。)
但是如果我查看屏幕上直观显示的另一组元素,使用 Inspect 我可以了解到整个集合作为单个 UIA 元素通过 UIA 公开。因此,在下面显示的 Inspect 图像中,我可以了解到我无法以编程方式调用该组中的特定颜色。
所以在那种情况下,我可能不得不假设我知道 UI 元素在该组中可视化显示的大小和布局,并在我认为合适的某个点模拟鼠标单击基于我想要调用的颜色。
通过使用 Inspect,我可以很好地了解我的选择。理想情况下,屏幕上可视化显示的单个元素将通过 UIA 作为单个元素公开,我可以通过任何相关模式(例如,Invoke、Toggle、SelectionItem 等)进行控制。但是,如果不支持有用的模式,那么我可以考虑根据公开的任何 ClickablePoint 或 BoundingRectangle 数据模拟鼠标点击。
谢谢,
家伙
我正在尝试将点击消息发送到(或调用)另一个应用程序中的按钮。
我使用了UISpy.exe并且可以找到我需要的元素。
但它没有 ID,没有可点击点,也没有调用模式。
我尝试了以下代码:
var processStartInfo = new ProcessStartInfo(@"tdesktop\Program.exe");
var proc = Process.Start(processStartInfo);
Thread.Sleep(3000);
AutomationElement mainWin = AutomationElement.RootElement.FindChildByProcessId(proc.Id);
List<AutomationElement> elmList= GetChildren(mainWin);
//MessageBox.Show(elmList.Count.ToString());
if (elmList.Count == 7)
{
List<AutomationElement> menubar= GetChildren(elmList[6]);
AutomationElement elementNode = menubar[1];
double x = elementNode.GetClickablePoint().X;
double y = elementNode.GetClickablePoint().Y;
win32 w = new win32();
w.move_left_click((UInt32)x, (UInt32)y);
}
它在 elementNode.GetClickablePoint().X 中抛出一个异常,即 Autumation 元素没有可点击的点。
我也试过 TryGetInvokePattern() 但仍然抛出执行它没有 InvokePattern。
我使用 VS2012 和 .net 4.5
有什么办法可以做到吗?
菜单栏不公开 InvokePattern (see UI Automation Support for the MenuBar Control Type). However, a menu item can be Invoked (see UI Automation Support for the MenuItem Control Type).
以下代码说明了如何生成菜单项列表:
AutomationElementCollection items = menubar.FindAll(
TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem));
如前所述,我强烈建议将 Inspect SDK 工具指向您感兴趣的 UI。该工具 (inspect.exe) 可以在 [=32] 等地方找到=].
通过使用该工具,您可以看到感兴趣的 UI 元素是如何以编程方式公开的。例如,它是作为单个 UIA 元素公开的,还是更大的 UIA 元素的一部分,代表一组 UI 元素的视觉显示,以及什么 UI它支持A模式吗?作为测试,我只是将 Inspect 指向 Paint 中的箭头形状。结果如下所示。
所以我可以看出箭头是作为单个 UIA 元素以编程方式公开的,并且它支持调用模式。这意味着我可以通过 UIA 以编程方式调用按钮。 (而且我可以从 Inspect 的 Action 菜单中调用元素的模式方法,这非常方便。)如果 UIA 元素不支持任何允许我以编程方式控制它的模式,我可以找到它的 BoundingRectangle 属性 到 UIA,并在中间模拟鼠标单击以调用它。 (我假设当我模拟鼠标点击时按钮没有被遮挡。)
但是如果我查看屏幕上直观显示的另一组元素,使用 Inspect 我可以了解到整个集合作为单个 UIA 元素通过 UIA 公开。因此,在下面显示的 Inspect 图像中,我可以了解到我无法以编程方式调用该组中的特定颜色。
所以在那种情况下,我可能不得不假设我知道 UI 元素在该组中可视化显示的大小和布局,并在我认为合适的某个点模拟鼠标单击基于我想要调用的颜色。
通过使用 Inspect,我可以很好地了解我的选择。理想情况下,屏幕上可视化显示的单个元素将通过 UIA 作为单个元素公开,我可以通过任何相关模式(例如,Invoke、Toggle、SelectionItem 等)进行控制。但是,如果不支持有用的模式,那么我可以考虑根据公开的任何 ClickablePoint 或 BoundingRectangle 数据模拟鼠标点击。
谢谢,
家伙