Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Hi,
I want to automate my window application controls by using windows handle & control's id.
Please give me suggetions...
Posted
Comments
Manfred Rudolf Bihy 11-May-12 7:37am    
There is to little information to give you any solution. What process do you want to automate? What do you want to do with the controls?
Before we can give you any suggstions we need to know what your exact requirements are.
The next important thing is: What have you tried and where are your obstacles?
Dave Kreskowiak 11-May-12 7:48am    
You have to define what you mean by "automate controls". Right now, the best answer anyone can give you is you can get the window handle for the controls from each controls .Handle property.
Sandeep Mewara 11-May-12 9:00am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Sergey Alexandrovich Kryukov 11-May-12 11:50am    
Start with: why would you like to implement control? If you don't explain your ultimate goals, there is no much interest in it, so who would be interested to answer?
--SA

1 solution

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.

C#
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();
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900