在Windows应用程序中自动化控件 [英] Automate controls in windows application

查看:110
本文介绍了在Windows应用程序中自动化控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想通过使用Windows句柄和控件的ID来自动化我的窗口应用程序控件.
请给我建议...

Hi,
I want to automate my window application controls by using windows handle & control''s id.
Please give me suggetions...

推荐答案

我已经使用"System.Windows.Automation.AutomationElement"类与其他应用程序进行交互.不同的应用程序以不同的方式组织组件,但是如果您正在与自己的应用程序进行交互,则遍历这些元素以找到正确的元素应该很容易...

这是我使用的一些代码,用于在其他应用程序中的menuItem上调用click()"invokepattern".此方法假定menuItems位于Window中.某些应用程序可能会将菜单放置在窗格中,也可能放置在其他地方.

I have used the "System.Windows.Automation.AutomationElement" class to interact with other applications. Different applications organize components in different ways, but if you are interacting with your own application, traversing these elements to find the correct one should be fairly easy...

Here is some code that i use, to invoke the click() "invokepattern" on a menuItem in a different application. This method assumes that the menuItems are located in the Window. Some applications may place the menu in a pane, or maybe elsewhere.

private void clickMenuItem(string windowTitle, string menu, string menuItem)
{
    AutomationElement root = AutomationElement.RootElement;
    foreach (AutomationElement window in root.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window)))
    {

        if (window.Current.Name.Contains(windowTitle) && window.Current.IsKeyboardFocusable)
        {
            AutomationElement menuAutomationElement = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Menu));

            //assuming menuBarAutomationElement already exists... these next four lines will open the Menu passed into the method
            PropertyCondition nameCond = new PropertyCondition(AutomationElement.NameProperty, menu);
            AutomationElement selectedMenu = menuAutomationElement.FindFirst(TreeScope.Descendants, nameCond);
            ExpandCollapsePattern fileECPat = selectedMenu.GetCurrentPattern(ExpandCollapsePattern.Pattern) as ExpandCollapsePattern;
            fileECPat.Expand();

            //Select the menuItem option that was passed into the method
            nameCond = new PropertyCondition(AutomationElement.NameProperty, menuItem);
            AutomationElement selectedMenuItem = selectedMenu.FindFirst(TreeScope.Descendants, nameCond);
            InvokePattern closeInvPat = selectedMenuItem.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
            closeInvPat.Invoke();
        }
    }
}


这篇关于在Windows应用程序中自动化控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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