5

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?

rcarubbi
  • 123
  • 1
  • 10

2 Answers2

3

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.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Thanks for your reply it was real useful, did you know how can I get a value from a specific cell in data grid control? – rcarubbi Jan 08 '14 at 17:52
  • @rcarubbi I'm affraid that a data grid is not a standard windows control, so you're probably going to have to dig deeper into the actual application you're trying to control. Is it in .NET WinForms, MFC, ...? – Luaan Jan 09 '14 at 10:02
  • I Have no idea, I guess it was built with Delphi... it's a pretty old app. – rcarubbi Jan 10 '14 at 13:36
  • @rcarubbi Delphi tried to stick to standard windows messaging as much as possible, but unless you can use WM_GETTEXT or something like that on the grid cell itself, you're probably out of luck. In fact, given the way that data grids work in Delphi, it's possible that the cells value isn't actually even loaded (for example, when connected to a live database cursor, it will only have the values for the visible cells). Or, if it is a wrapper for a windows list view, you can use those windows messages (like LVM_GETITEM). – Luaan Jan 10 '14 at 13:55
0

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); 
realnero
  • 994
  • 6
  • 12
  • The Classes from namespace System.Windows.Automation do this behind the scenes. The First Line from code block that I posted do exactly the same work. var mainWindow = AutomationElement.RootElement.FindChildByNamePart("Back Office Control"); Unfortunally the problem that I'm facing is deeper than just get the window. – rcarubbi Jan 08 '14 at 18:01