C#Windows 10计算器上的自动单击按钮
所以我是C#新手,我想做一个自动化,在Win10计算器上点击几个按钮 我下载了LeanRunner Designer,并使用它的模型管理器获得5号计算器按钮属性,我得到:C#Windows 10计算器上的自动单击按钮,c#,automation,click,calculator,findwindow,C#,Automation,Click,Calculator,Findwindow,所以我是C#新手,我想做一个自动化,在Win10计算器上点击几个按钮 我下载了LeanRunner Designer,并使用它的模型管理器获得5号计算器按钮属性,我得到: Auto.getWindow({ "className": "ApplicationFrameWindow", "title": "Calculator" }).getWindow({ "className": &q
Auto.getWindow({
"className": "ApplicationFrameWindow",
"title": "Calculator"
}).getWindow({
"className": "Windows.UI.Core.CoreWindow",
"title": "Calculator"
}).getGeneric({
"type": "Group",
"automationId": "NumberPad",
"name": "Number pad",
"className": "NamedContainerAutomationPeer"
}).getButton({
"automationId": "num5Button",
"name": "Five",
"className": "Button"
}).click(52, 24);
我把getWindow调成这样那样就行了
int-hwnd=0;
IntPtr hwndChild=IntPtr.Zero;
IntPtr hwndChild2=IntPtr.Zero;
//获取计算器应用程序主窗口的句柄
hwnd=FindWindow(“应用程序框架窗口”、“计算器”);
hwndChild=FindWindowEx((IntPtr)hwnd,IntPtr.Zero,“Windows.UI.Core.CoreWindow”,“Calculator”);
但是现在我需要点击按钮,我被卡住了。
我知道click的最终结果是
SendMessage((int)hwndChild2, BM_CLICK, 0, IntPtr.Zero);
但是我没有找到按钮的位置。
我不想使用SendKeys。
如果我遗漏了什么或说得不清楚,请向您表示感谢和歉意。如上所述,这可以通过UIAutomation完成。 例如:
public static AutomationElement calcUI;
public void FindAndClickButton(string name)
{
System.Windows.Automation.Condition condition1 = new PropertyCondition(AutomationElement.ClassNameProperty, "Button");
System.Windows.Automation.Condition condition2 = new PropertyCondition(AutomationElement.NameProperty, name);
System.Windows.Automation.Condition condition = new AndCondition(condition1, condition2);
AutomationElement button = calcUI.FindFirst(TreeScope.Descendants, condition);
InvokePattern btnPattern = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
btnPattern.Invoke();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var calc = System.Diagnostics.Process.Start("calc");
//wait for the UI to appear
calc.WaitForInputIdle(5000);
System.Threading.Thread.Sleep(2000);
AutomationElement root = AutomationElement.RootElement;
System.Windows.Automation.Condition condition = new PropertyCondition(AutomationElement.NameProperty, "Calculator");
calcUI = root.FindFirst(TreeScope.Children, condition);
if (calcUI != null)
{
// get and click the buttons for the calculation
FindAndClickButton("Five");
FindAndClickButton("Plus");
FindAndClickButton("Nine");
FindAndClickButton("Multiply by");
FindAndClickButton("Three");
FindAndClickButton("Divide by");
FindAndClickButton("Four");
FindAndClickButton("Equals");
//get the result
System.Windows.Automation.Condition conditionResult = new PropertyCondition(AutomationElement.AutomationIdProperty, "CalculatorResults");
var result = calcUI.FindFirst(TreeScope.Descendants, conditionResult);
MessageBox.Show(result.Current.Name);
}
}
别忘了给项目添加引用
UIAutomationClient
UIAutomationTypes
您最好使用Microsoft UI Automation。我会尝试一下,谢谢,但仍在犹豫,如果使用UI Automation,并且按照建议的方式如何使用UI Automation,请不要回头看。按钮具有,您只需调用其
Invoke()
方法即可单击按钮要查找计算器窗口-由于所有这些小程序的主窗口都具有相同的类名,ApplicationFrameWindow
(例如,请参见日历窗口)-使用和条件
查找更具体的子体(在TreeScope.subjections
),该子体包括:。,ControlType
和Name
条件。请注意,这些类中的某些类可能具有本地化名称(如计算器
),因此此元素名称可能没有意义,除非您仅为特定语言编写代码