这是一个执行此操作的程序。请注意,您必须使用 NuGet UIAComWrapper 包(由 Microsoft 人员编写),而不是提供开箱即用的标准 UIAutomation*.dll 以使其工作。标准的 UIA .NET 程序集看不到所有的 AutomationElement,不知道所有的属性(Aria 等)。
是的,这意味着它们是bugged/obsolete,并且由于某种原因,Microsoft 没有正式发布带有新的 .NET 或 Windows 版本的更新...无论如何 UIAComWrapper 是 100% 来源- 兼容的直接替换,所以这应该不是问题。
如果您想查看 UIAComWrapper 与 UIA 程序集返回的内容之间的差异,您会看到使用 Inspect(官方 UIA 工具)与 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 因为进程的主窗口不是设置窗口,设置窗口不是主窗口的子窗口。