我正在使用C#windows窗体应用程序,它使用System.Windows.Automation类自动化另一个win32应用程序。
我需要阅读或交互一些元素但是UISpy找不到这些字段,它只找到了父容器面板。
例如,下面的代码块应返回许多工具条按钮,但不起作用:
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);
还有另一种方法吗?
答案 0 :(得分:2)
正如您刚刚发现的那样,工具条按钮实际上并不是Windows消息传递世界中的独立控件。菜单项和其他一些控件也是如此。
要使用Windows消息进行单击,您需要将WM直接发送到工具栏,而不是按钮,例如TB_PRESSBUTTON(http://msdn.microsoft.com/en-us/library/windows/desktop/bb787389(v=vs.85).aspx)。
你必须使用针对工具栏的SendMessage
WinAPI函数(你可以像往常一样获得hWnd),TB_PRESSBUTTON作为消息类型,命令标识符作为wParam,1作为lParam。
答案 1 :(得分:0)
您需要使用Win32调用来实现此目的。 GetWindow做到了
帮助信息 - 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);