C#真的无法解决用SendMessage获取checkbox状态吗

如题所述

关于用C#来进行一些GUI操作,最好的做法还是用.net本身自带Automation库,该库应该是最强大的GUI操作库了,所有的windows控件都可以用这个来操作,用法非常简单,例如你这个获取checkbox状态的,我写了个函数:
bool GetCheckBoxState(string windowName, string controlName)
{
AutomationElement desktop = AutomationElement.RootElement;
AutomationElement window = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, windowName));
AutomationElement checkbox = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, controlName));
return ((TogglePattern)checkbox.GetCurrentPattern(TogglePattern.Pattern)).Current.ToggleState == ToggleState.On;
}
不论你是用MFC,还是C# winform,还是WPF,用以上代码都可以准确获取checkbox的状态,而且该库还有一个配套的工具,叫UISpy,这个工具可以获取控件的各个属性以及该控件所拥有的操作方法,直接照着这个工具所获取的信息就可以写出代码来,十分容易
温馨提示:答案为网友推荐,仅供参考
相似回答