简体   繁体   English

c# UI自动化标签

[英]c# UI Automation label

I am using Microsoft's UI Automation library for quite a while now, and I have been able to manipulate textboxes and buttons.我使用微软的 UI 自动化库已经有一段时间了,我已经能够操作文本框和按钮。 However, I have problems with some of the controls.但是,我在某些控件上遇到了问题。

I have a program that I want to automate, which have a picture box and a link label that functions like a button click.我有一个要自动化的程序,它有一个图片框和一个链接标签,其功能类似于单击按钮。 This means that if I clicked on the picture box or the link label, I will trigger an event, and be directed to another page.这意味着如果我点击图片框或链接标签,我将触发一个事件,并被定向到另一个页面。

I have tried using the invoke method, which buttons have, but was unsuccessful.我曾尝试使用按钮具有的 invoke 方法,但没有成功。 Is there any other way to trigger the click event or something similar for a picture box or link label?有没有其他方法可以触发点击事件或类似图片框或链接标签的事件?

Thanks in advance.提前致谢。

Microsoft's UI Automation library: http://msdn.microsoft.com/en-us/library/ms747327.aspx微软的 UI 自动化库: http : //msdn.microsoft.com/en-us/library/ms747327.aspx

<< I have a program that I want to automate, which have a picture box and a link label that functions like a button click >> - this sounds like it is a picture or label control that is just handing mouse down events, but not otherwise letting the system know that it behaves like a button. << 我有一个我想自动化的程序,它有一个图片框和一个链接标签,功能类似于按钮单击 >> - 这听起来像是一个图片或标签控件,它只是传递鼠标按下事件,但不是否则让系统知道它的行为就像一个按钮。 Your best bet in cases like these is to use UIAutomation to get the bounding rectangle, and then use SendInput to move the mouse there and perform a click yourself.在这种情况下,您最好的选择是使用 UIAutomation 获取边界矩形,然后使用 SendInput 将鼠标移动到那里并自己执行单击。

UIAutomation only knows how to deal with standard buttons. UIAutomation 只知道如何处理标准按钮。 Anything that's basically a custom button - eg.任何基本上是自定义按钮的东西 - 例如。 label that handles mouse down - would need to add support for some extra interfaces in order to tell UIAutomation that it can be clicked and to provide support for the Click method itself.处理鼠标按下的标签 - 需要添加对一些额外接口的支持,以便告诉 UIAutomation 它可以被点击并为 Click 方法本身提供支持。

按钮有一个PerformClick()方法。

It's possible to change the behavior of Winforms controls with regard to UI Automation.可以根据 UI 自动化更改 Winforms 控件的行为。 If you want a PictureBox to be seen as a button by UI Automation, and therefore be clickable using UI Automation tools, you can derive from PictureBox and override Automation/Accessible methods, something like this:如果您希望一个 PictureBox 被 UI 自动化视为一个按钮,因此可以使用 UI 自动化工具点击,您可以从 PictureBox 派生并覆盖自动化/可访问方法,如下所示:

Before:前:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var pb = new PictureBox();
        pb.Dock = DockStyle.Fill;
        pb.Click += (s, e) => MessageBox.Show("hello world");
        Controls.Add(pb);
    }
}

This is how Inspect (from the SDK tools) sees it, as a "pane", with no specific action:这就是 Inspect(来自 SDK 工具)将其视为“窗格”的方式,没有具体操作:

在此处输入图片说明

After:后:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var pb = new ButtonPictureBox();
        pb.Dock = DockStyle.Fill;
        pb.Click += (s, e) => MessageBox.Show("hello world");
        Controls.Add(pb);
    }
}

public class ButtonPictureBox : PictureBox
{
    protected override AccessibleObject CreateAccessibilityInstance() => new Accessible(this);

    private class Accessible : ButtonBase.ButtonBaseAccessibleObject
    {
        public Accessible(ButtonPictureBox control)
            : base(control)
        {
        }

        public new ButtonPictureBox Owner => (ButtonPictureBox)base.Owner;
        public override AccessibleRole Role => AccessibleRole.PushButton;
        public override AccessibleStates State => AccessibleStates.Default;
        public override void DoDefaultAction() => Owner.OnClick(EventArgs.Empty);
    }
}

Now, Inspect sees it as a "button" and the Invoke Pattern is available, so if you Invoke it, the Click method will be raised:现在,Inspect 将其视为一个“按钮”并且调用模式可用,因此如果您调用它,将引发 Click 方法:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM