c#在另一个应用程序中被选中
本文关键字:应用程序 另一个 | 更新日期: 2023-09-27 18:22:31
我正在编写一个应用程序来连接另外两个现有的应用程序。我是C#的新手,所以这对我来说是一个很大的挑战。我目前还没有找到答案的问题是是否选中了复选框。我试着使用UIAutomation,但我不知道如何让它工作。当我使用UISpy选中复选框时,它表明该复选框是一个窗格。经过两天的大量搜索,我找不到如何将复选框的信息作为窗格。我原以为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());
}
非常感谢您的帮助。
我用过这篇文章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;
}
}