How to access elements that UI Spy doesn't see to automate a win32 application with C#

3.2k views Asked by At

I'm doing a C# windows form application that automate another win32 application using System.Windows.Automation classes.

There are some elements that I need to read or interact but UISpy don't find these fields, it only found the parent container panel.

For example, this code block below should return many toolstrip buttons but doesn't work:

var mainWindow = AutomationElement.RootElement.FindChildByNamePart("Back Office Control");
var mainWindowChildren = mainWindow.FindAll(TreeScope.Children, Condition.TrueCondition);
var toolBarPanel = mainWindowChildren[1];
var toolBarItens = toolBarPanel.FindAll(TreeScope.Children, Condition.TrueCondition);

There is another way to do this?

2

There are 2 answers

4
Luaan On BEST ANSWER

As you've just found out, toolstrip buttons aren't actually separate controls in the windows messaging world. This is also true of menu items and some other controls.

To cause a click using a windows message, you need to send a WM directly to the toolbar, not the button, for example TB_PRESSBUTTON (http://msdn.microsoft.com/en-us/library/windows/desktop/bb787389(v=vs.85).aspx).

You have to use the SendMessage WinAPI function, targeted at the toolbar (you can get the hWnd as usual), with TB_PRESSBUTTON as message type, the command identifier as wParam and 1 as lParam.

1
realnero On

You need to use Win32 calls to achieve this. GetWindow does it

Help info - http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx

[DllImport("user32.dll")] public static extern int GetWindow(int hwnd,int wCmd);