dontcare
dontcare

Reputation: 975

mouse ( left / middle / right click ) invoke with ui automation framework?

With the ms ui automation framework we have the possibility to invoke e.g buttons with the InvokePattern:

InvokePattern invokePattern = ae.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern.Invoke();

but is there a way to perform with the automation framework to simulate e.g a right click ?

Upvotes: 0

Views: 2344

Answers (2)

RonR
RonR

Reputation: 21

I never found a direct way of using UIAutomation to perform a right mouse click on a button, but this is my work around.

[DllImport("user32.dll", EntryPoint = "SetCursorPos")]

[return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetCursorPos(int X, int Y);

    Rect BoundingRect = (Rect)aeButton.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty);
      Mouse.MouseRightClick(BoundingRect);


    public void MouseRightClick(System.Windows.Rect BoundingRectangle)
    {
        int X = (int)BoundingRectangle.Left + ((int)(BoundingRectangle.Right - BoundingRectangle.Left) / 2);
        int Y = (int)BoundingRectangle.Top + ((int)(BoundingRectangle.Bottom - BoundingRectangle.Top) / 2);
         MouseClick(MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP,X, Y);
    }
    protected void MouseClick(uint ButtonDown, uint ButtonUp, int X, int Y)
    {
        try
        {
            SetCursorPos(X, Y);
            //Call the imported function with the cursor's current position
            mouse_event(ButtonDown | ButtonUp, X, Y, 0, 0);
        }
        catch (Exception e)
        {
            throw new InvalidOperationException("MouseClick", e);
        }
    }

Upvotes: 2

zamN
zamN

Reputation: 236

Maybe this: http://bytes.com/topic/c-sharp/answers/268148-c-equivalent-javas-robot-class

is what you are looking for?

Upvotes: 1

Related Questions