开发者

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开发者_JAVA百科, 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.


Buttons have a PerformClick() method.


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:

c# UI Automation label

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:

c# UI Automation label

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜