AutomationElement/上下文菜单 [英] AutomationElement / Context Menus

查看:432
本文介绍了AutomationElement/上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UI自动化与上下文菜单进行交互.基本上,我正在尝试:

I am trying to be able to interact with context menus using UI Automation. Basically, I am trying to:

  • 将重点放在AutomationElement
  • SendKeys.SendWait发送SHIFT+F10
  • 看看弹出的内容
  • set focus on an AutomationElement
  • SendKeys.SendWait to send a SHIFT+F10
  • see what pops up

我看到的是,即使UISpy看到了,弹出上下文菜单时,AutomationElement.FindAll(TreeScope.Descendants, Condition.TrueCondition)似乎也没有反映出来.

What I am seeing is that the AutomationElement.FindAll(TreeScope.Descendants, Condition.TrueCondition) does not seem to reflect when the context menu is popped, even though UISpy sees it.

任何帮助将不胜感激.

这是我已经在 LINQPad 中运行的示例应用程序:

Here is an example application that I have been running in LINQPad:

void Main()
{
  var notepad = FindNotepad();
  Console.WriteLine("Pre-Context: {0}", Descendants(notepad).Count);
  TypeInto(notepad, "(+{F10})");
  Thread.Sleep(1000);

  Console.WriteLine("With Context: {0}", Descendants(notepad).Count);
  TypeInto(notepad, "{ESC}");
  Console.WriteLine("Post-Context: {0}", Descendants(notepad).Count);
}

AutomationElement FindNotepad(string title = "Untitled - Notepad")
{
  var notepadName = new PropertyCondition(AutomationElement.NameProperty, title);
  return AutomationElement.RootElement.FindFirst(TreeScope.Children, notepadName);
}

void TypeInto(AutomationElement element, string keys)
{
  element.SetFocus();
  SendKeys.SendWait(keys);
}

AutomationElementCollection Descendants(AutomationElement element)
{
  return element.FindAll(TreeScope.Subtree, Condition.TrueCondition);
}

推荐答案

在Shift-F10上打开的上下文菜单实际上是根元素(桌面)的子级,因此,如果您使用Descendants(AutomationElement.RootElement).Count而不是Descendants(notepad).Count会看到区别. 例如

The context menu that opened on Shift-F10 is actually a child of the root element (desktop) so if you use Descendants(AutomationElement.RootElement).Count instead of Descendants(notepad).Count you will see the difference. e.g.

Pre-Context: 2019
With Context: 2036
Post-Context: 2019

这篇关于AutomationElement/上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆