使用ms ui自动化框架,我们可以使用InvokePattern调用例如按钮:
InvokePattern invokePattern = ae.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
invokePattern.Invoke();
但有没有办法用自动化框架来模拟右键点击?
答案 0 :(得分:2)
我从未找到使用UIAutomation直接在按钮上执行鼠标右键的方法,但这是我的工作。
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
[返回: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);
}
}
答案 1 :(得分:1)