我正在编写一个应用程序来连接其他两个现有的应用程序。 我是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());
}
非常感谢任何帮助。
答案 0 :(得分:0)
我以这篇文章 https://bytes.com/topic/net/answers/637107-how-find-out-if-check-box-checked 为灵感:
using Accessibility;
[DllImport("oleacc.dll", PreserveSig = false)]
[return: MarshalAs(UnmanagedType.Interface)]
public static extern object AccessibleObjectFromWindow(IntPtr hwnd, uint dwId, ref Guid riid);
public static Nullable<bool> isCheckBoxChecked(IntPtr checkBoxHandle)
{
const UInt32 OBJID_CLIENT = 0xFFFFFFFC;
const int UNCHECKED = 1048576;
const int CHECKED = 1048592;
Guid uid = new Guid("618736e0-3c3d-11cf-810c-00aa00389b71");
IAccessible accObj = (IAccessible)AccessibleObjectFromWindow(checkBoxHandle, OBJID_CLIENT, ref uid);
object o2 = accObj.get_accState(0);
if ((int)o2 == UNCHECKED)
{
return false;
}
else if ((int)o2 == CHECKED)
{
return true;
}
else
{
return null;
}
}