AutomationElement / Context Menus

Deadly 提交于 2019-12-11 11:03:00

问题


Description

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

  • set focus on an AutomationElement
  • SendKeys.SendWait to send a SHIFT+F10
  • see what pops up

What I Am Seeing

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.

Any help would be greatly appreciated.

Example

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);
}

回答1:


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


来源:https://stackoverflow.com/questions/21889806/automationelement-context-menus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!