如何使用 Microsoft uiautomation 检查复选框?
How to check a checkbox with microsoft uiautomation?
我需要自动化 madvr gui 并使用 uiautomation 实现平滑运动。
但是找不到最近的和简单的工作示例。
madvr window 身份:
madvr smooth motion复选框标识:
我的实际代码:
using System;
using System.Diagnostics;
namespace MadAutomation
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enabling the madvr smooth motion feature...");
EnableSmoothMotion();
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
public static void EnableSmoothMotion()
{
// Run madvr configuration.
Process p = new Process();
p.StartInfo.FileName = @"C:\Users\Admin\Desktop\madVR\madHcCtrl.exe";
p.StartInfo.Arguments = "editLocalSettingsDontWait";
p.Start();
// Enable smooth motion checkbox.
}
}
}
这是一个执行此操作的程序。请注意,您必须 使用 NuGet UIAComWrapper 程序包(由 Microsoft 人员编写)而不是 UIAutomation*.dll 提供的标准 out-of-the-box上班。标准 UIA .NET 程序集看不到所有 AutomationElement,不知道所有属性(Aria 等)。
是的,这意味着它们是 bugged/obsolete 并且出于某种原因,Microsoft 并未正式发布带有新 .NET 或 Windows 版本的较新版本...无论如何,UIAComWrapper 是 100% source-compatible drop-in 的替代品,所以这应该不是问题。
如果您想查看 UIAComWrapper 与 UIA 组件返回的内容之间的差异,您将看到的差异与使用 Inspect(官方UI工具)和UISpy(官方UI工具已过时)。它们看起来很相似,但实际上在细节或结构上有很大不同。
public static void EnableSmoothMotion()
{
bool finished = false;
// wait for the settings window to show
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, (sender, e) =>
{
var window = (AutomationElement)sender;
if (window.Current.ClassName != "TFMadVRSettings")
return;
// get the tree element
var tree = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tree));
// get the smooth motion element & select it
var smoothMotion = tree.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "smooth motion"));
((SelectionItemPattern)smoothMotion.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
// get the tab element
var tab = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab));
// get the pane element
var pane = tab.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane));
// get the first checkbox & ensure it's clicked
var cb = pane.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.CheckBox));
TogglePattern tp = (TogglePattern)cb.GetCurrentPattern(TogglePattern.Pattern);
if (tp.Current.ToggleState != ToggleState.On) // not on? click it
{
((InvokePattern)cb.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
}
// NOTE: uncomment the two following line if you want to close the window directly
// get the ok button & push it
//var ok = window.FindFirst(TreeScope.Children, new AndCondition(
// new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
// new PropertyCondition(AutomationElement.NameProperty, "OK")));
//((InvokePattern)ok.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
finished = true;
});
// run the program
Process p = new Process();
p.StartInfo.FileName = @"C:\Users\Admin\Desktop\madVR\madHcCtrl.exe";
p.StartInfo.Arguments = "editLocalSettingsDontWait";
p.Start();
while(!finished)
{
Thread.Sleep(100);
}
Automation.RemoveAllEventHandlers();
}
注意:我在这里使用了事件处理程序而不是process.MainWindowHandle,因为进程的主要window不是设置window,而设置window是不是主要 window.
的 child
我需要自动化 madvr gui 并使用 uiautomation 实现平滑运动。
但是找不到最近的和简单的工作示例。
madvr window 身份:
madvr smooth motion复选框标识:
我的实际代码:
using System;
using System.Diagnostics;
namespace MadAutomation
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enabling the madvr smooth motion feature...");
EnableSmoothMotion();
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
public static void EnableSmoothMotion()
{
// Run madvr configuration.
Process p = new Process();
p.StartInfo.FileName = @"C:\Users\Admin\Desktop\madVR\madHcCtrl.exe";
p.StartInfo.Arguments = "editLocalSettingsDontWait";
p.Start();
// Enable smooth motion checkbox.
}
}
}
这是一个执行此操作的程序。请注意,您必须 使用 NuGet UIAComWrapper 程序包(由 Microsoft 人员编写)而不是 UIAutomation*.dll 提供的标准 out-of-the-box上班。标准 UIA .NET 程序集看不到所有 AutomationElement,不知道所有属性(Aria 等)。
是的,这意味着它们是 bugged/obsolete 并且出于某种原因,Microsoft 并未正式发布带有新 .NET 或 Windows 版本的较新版本...无论如何,UIAComWrapper 是 100% source-compatible drop-in 的替代品,所以这应该不是问题。
如果您想查看 UIAComWrapper 与 UIA 组件返回的内容之间的差异,您将看到的差异与使用 Inspect(官方UI工具)和UISpy(官方UI工具已过时)。它们看起来很相似,但实际上在细节或结构上有很大不同。
public static void EnableSmoothMotion()
{
bool finished = false;
// wait for the settings window to show
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, (sender, e) =>
{
var window = (AutomationElement)sender;
if (window.Current.ClassName != "TFMadVRSettings")
return;
// get the tree element
var tree = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tree));
// get the smooth motion element & select it
var smoothMotion = tree.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "smooth motion"));
((SelectionItemPattern)smoothMotion.GetCurrentPattern(SelectionItemPattern.Pattern)).Select();
// get the tab element
var tab = window.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Tab));
// get the pane element
var pane = tab.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Pane));
// get the first checkbox & ensure it's clicked
var cb = pane.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.CheckBox));
TogglePattern tp = (TogglePattern)cb.GetCurrentPattern(TogglePattern.Pattern);
if (tp.Current.ToggleState != ToggleState.On) // not on? click it
{
((InvokePattern)cb.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
}
// NOTE: uncomment the two following line if you want to close the window directly
// get the ok button & push it
//var ok = window.FindFirst(TreeScope.Children, new AndCondition(
// new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
// new PropertyCondition(AutomationElement.NameProperty, "OK")));
//((InvokePattern)ok.GetCurrentPattern(InvokePattern.Pattern)).Invoke();
finished = true;
});
// run the program
Process p = new Process();
p.StartInfo.FileName = @"C:\Users\Admin\Desktop\madVR\madHcCtrl.exe";
p.StartInfo.Arguments = "editLocalSettingsDontWait";
p.Start();
while(!finished)
{
Thread.Sleep(100);
}
Automation.RemoveAllEventHandlers();
}
注意:我在这里使用了事件处理程序而不是process.MainWindowHandle,因为进程的主要window不是设置window,而设置window是不是主要 window.
的 child