将点击发送到没有 InvokePattern 的 AutomationElement

Sending clicks to an AutomationElement with no InvokePattern

使用 AutomationElement,有没有什么方法可以将点击发送到 TabItem 而无需移动鼠标并模拟点击? AutomationElement 对我来说仍然是新的-据我了解,除非它支持 InvokePattern (TabItem 不支持),否则您必须走定位控件位置和模拟鼠标的路线。我可以使用该代码(见下文)- 我很好奇这是否是我的选择。

AutomationElement tabControl = GetControl(window, "NOTEBOOK");
AutomationElement tabGeneral = GetControl(tabControl, "FM_STAFF_SUB_P1");

AutomationElementCollection tabs = GetAllTabs(window, tabGeneral);

System.Windows.Point p = tabs[1].GetClickablePoint();

MoveMouse((int)p.X, (int)p.Y);
ClickMouse();

谢谢。

  1. 尝试tab.SetFocus()
  2. 获取所有支持的模式 (tab.GetSupportedPatterns()),然后查看此选项卡实现支持哪些模式。它应该支持 SelectionItemPattern,所以使用:((SelectionItemPattern)tab.GetCurrentPattern(SelectionItemPattern.Pattern)).Select()
  3. 使用 SendKeys 到 window 以导航选项卡(大多数情况下会有一个热键在它们之间导航。如果选项卡被选中,您可以通过在每次导航后检查来组合.
  4. 如果以上都失败了,我想鼠标点击是你唯一的选择。

我在向选项卡项添加热键时遇到了类似的问题。在我的例子中,只需选择选项卡项即可获得焦点,但在动态生成时不会显示选项卡的内容。除非我误解了您的问题,否则此示例将使用 TabItemAutomationPeer 模拟选项卡项点击。

        //get the TabItem
      TabItem tabItem = (TabItem)sender; //or however you are getting it.
    
        //get the TabControl
      TabControl tabControl = UIHelper.FindLogicalParent<TabControl>(tabItem); //or however you are getting it.
      
        //do that magic
      tabItem.IsSelected = true; 
      TabControlAutomationPeer tabControlAutomationPeer = new TabControlAutomationPeer(tabControl);
      TabItemAutomationPeer tabItemAutomationPeer = new TabItemAutomationPeer(tabItem, tabControlAutomationPeer);
      tabItemAutomationPeer.SetFocus(); //works like a click