【发布时间】:2016-02-21 14:31:03
【问题描述】:
我正在编写一个应用程序来连接其他两个现有应用程序。 我是 C# 新手,所以这对我来说是一个很大的挑战。 我目前尚未找到答案的问题是是否选中了复选框。 我试图使用 UIAutomation,但我不知道如何让它工作。 当我使用 UISpy 检查复选框时,它表明该复选框是一个窗格。 经过 2 多天的大量搜索后,我无法找到如何将复选框的信息作为窗格获取。 我在想 pInvoke 可以解决问题,但我也没有运气。 这是我尝试过的:
var ischecked = NativeMethods.SendMessage(variables.allCNumbers[29].Hwnd,BM_GETSTATE, IntPtr.Zero, IntPtr.Zero);
MessageBox.Show(variables.allCNumbers[29].Hwnd.ToString()); // This has a value
MessageBox.Show(ischecked.ToString()); // This always shows 0 whether the checkbox is checked or not
这是我尝试过的 UIAutomation:
AutomationElement rootElement = AutomationElement.RootElement;
Automation.Condition condition = new PropertyCondition(AutomationElement.ClassNameProperty,"TMainForm_ihm" );
AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
AutomationElement workspace = appElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Workspace"));
AutomationElement card = workspace.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Card"));
AutomationElement pagecontrol = card.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "TRzPageControl"));
AutomationElement cardnumber = pagecontrol.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Card number"));
if(cardnumber != null)
{
Automation.Condition usecardCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, "25232366");
AutomationElement usecard = cardnumber.FindFirst(TreeScope.Children, usecardCondition);
MessageBox.Show("usecard: " + usecard.Current.ControlType.ProgrammaticName); // This returns "ControlType.Pane"
//TogglePattern tp1 = usecard.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern; <- This throws an error: An unhandled exception of type 'System.InvalidOperationException' occurred in UIAutomationClient.dll Additional information: Unsupported Pattern.
//MessageBox.Show(tp1.Current.ToggleState.ToString());
}
非常感谢任何帮助。
【问题讨论】:
-
没有合适的 API 可用吗?如果我能在我的代码库中找到它,我会觉得这段代码很有趣。
-
没有可用的 API。就像我说的那样,我对这一切都很陌生。我敢肯定“有趣”并不完全是一种恭维,但我也不认为这是一种侮辱。我真的很想学习正确的做事方式,但在这一点上,我只是想让事情顺利进行。在过去的几周里,我学到了很多东西(我在 9 月份才开始使用 C#),并且自从发现我错了之后,我重写了很多东西。
-
这并不是侮辱。通常情况下,数据是通过 API 间接提供的(应用程序被扩展,或者另一个应用程序被用来检索这些数据)。这样,UI 仍然可以更改原始应用程序,但您的应用程序仍然可以工作。我不确定这个其他应用程序是如何工作的,所以恐怕我无法根据您提供的信息向您提供更多详细信息。
-
试试 Winapi。 .. 检查此链接:bytes.com/topic/net/answers/…
-
Windows 控件不支持自动化框架,所以试试我上面发给你的链接
标签: c# user32 microsoft-ui-automation