我正在做一个 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);
还有别的办法吗?
最佳答案
正如您刚刚发现的那样,工具条按钮实际上并不是 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。
关于c# - 如何访问 UI Spy 看不到的元素以使用 C# 自动化 win32 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20999466/