Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 625
rcarubbi Avatar asked Oct 20 '22 17:10

rcarubbi


1 Answers

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.

like image 134
Luaan Avatar answered Oct 31 '22 21:10

Luaan