繁体   English   中英

用ui自动化框架调用鼠标(左/中/右键)?

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

使用ms ui自动化框架,我们可以使用InvokePattern调用例如按钮:

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

但是有没有一种方法可以使用自动化框架来模拟例如右键单击?

我从未找到使用UIAutomation在按钮上执行鼠标右键的直接方法,但这是我的解决方法。

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

[返回:MarshalAs(UnmanagedType.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);
        }
    }

暂无
暂无

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

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