简体   繁体   中英

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

<< 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 only knows how to deal with standard buttons. 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.

按钮有一个PerformClick()方法。

It's possible to change the behavior of Winforms controls with regard to UI Automation. 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:

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:

在此处输入图片说明

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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