首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用microsoft uiautomation检查复选框?

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

Stack Overflow用户
提问于 2015-06-17 01:10:49
回答 1查看 3.4K关注 0票数 4

我需要自动化的madvr和使顺利的运动与uiautomation。

但却找不到最近和容易操作的样本。

madvr窗口标识:

madvr平滑运动复选框标识:

我的实际代码:

代码语言:javascript
运行
复制
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.
        }
    }
}
EN

Stack Overflow用户

回答已采纳

发布于 2015-06-17 06:44:55

这是一个做这个的程序。注意,您必须使用NuGet UIAComWrapper包(由微软人员编写),而不是使用开箱即用的标准UIAutomation*.dll。标准的UIA .NET程序集并不能看到所有的AutomationElement,也不知道所有的属性(Aria等等)。

是的,这意味着他们是bugged/obsolete,出于某种原因,微软没有正式发布新的.NET或Windows发布版本.无论如何,UIAComWrapper是一个100%的源代码兼容的插入替换,所以这不应该是一个问题。

如果您希望看到UIAComWrapper与UISpy返回的内容之间的差异,那么如果您分别使用(官方UIA工具)和UISpy (过时的官方UI工具),则会看到相同的差异。它们看起来很相似,但实际上在细节或结构上有很大的不同。

代码语言:javascript
运行
复制
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,因为进程的主窗口不是设置窗口,并且设置窗口不是主窗口的子窗口。

票数 7
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30880765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档