如何使用microsoft uiautomation检查复选框?

时间:2015-06-17 01:10:50

标签: c# ui-automation microsoft-ui-automation

我需要自动化madvr gui并通过uutoutomation实现平滑运动 但是找不到最近的简单工作样品。

madvr窗口识别:

madvr平滑动作复选框识别:

我的实际代码:

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.
        }
    }
}

1 个答案:

答案 0 :(得分:5)

这是一个执行它的程序。请注意,必须使用NuGet UIAComWrapper包(由Microsoft人编写),而不是开箱即用的标准UIAutomation * .dll才能使用。标准的UIA .NET程序集没有看到所有的AutomationElement,也不知道所有属性(Aria等)。

是的,这意味着它们被窃听/过时,并且出于某种原因,Microsoft没有正式发布新版本的.NET或Windows版本......无论如何,UIAComWrapper是100%来源兼容的直接替换,所以这应该不是问题。

如果你想看看UIAComWrapper与UIA asssemblies返回的内容之间的差异,那么如果你使用 Inspect (官方UIA工具)vs UISpy <,你会看到相同的差异/ strong>(过时的官方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,因为进程的主窗口不是设置窗口,并且设置窗口不是主窗口的子窗口。