使用WPF的内置UI自动化,将左键单击事件发送到AutomationElement(例如Button)很容易(尽管非常详细):
InvokePattern invokePattern = (InvokePattern) element.GetCurrentPattern(InvokePattern.Pattern);
invokePattern.Invoke();
但是,似乎没有内置方式向同一元素发送右键单击。我已经辞职了,使用P / Invoke来调用SendInput,但是我无法让它工作。使用下面的代码,当我调用RightClick()时,会在光标所在的位置弹出一个上下文菜单,而不是在我希望右键单击的元素上。因此,它似乎忽略了我提交的坐标并仅使用当前光标位置。
public static void RightClick(this AutomationElement element)
{
Point p = element.GetClickablePoint();
NativeStructs.Input input = new NativeStructs.Input
{
type = NativeEnums.SendInputEventType.Mouse,
mouseInput = new NativeStructs.MouseInput
{
dx = (int) p.X,
dy = (int) p.Y,
mouseData = 0,
dwFlags = NativeEnums.MouseEventFlags.Absolute | NativeEnums.MouseEventFlags.RightDown,
time = 0,
dwExtraInfo = IntPtr.Zero,
},
};
NativeMethods.SendInput(1, ref input, Marshal.SizeOf(input));
input.mouseInput.dwFlags = NativeEnums.MouseEventFlags.Absolute | NativeEnums.MouseEventFlags.RightUp;
NativeMethods.SendInput(1, ref input, Marshal.SizeOf(input));
}
internal static class NativeMethods
{
[DllImport("user32.dll", SetLastError = true)]
internal static extern uint SendInput(uint nInputs, ref NativeStructs.Input pInputs, int cbSize);
}
internal static class NativeStructs
{
[StructLayout(LayoutKind.Sequential)]
internal struct Input
{
public NativeEnums.SendInputEventType type;
public MouseInput mouseInput;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MouseInput
{
public int dx;
public int dy;
public uint mouseData;
public NativeEnums.MouseEventFlags dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
}
internal static class NativeEnums
{
internal enum SendInputEventType : int
{
Mouse = 0,
Keyboard = 1,
Hardware = 2,
}
[Flags]
internal enum MouseEventFlags : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
Absolute = 0x8000,
}
}
答案 0 :(得分:1)
根据我的理解,你对于模拟右键单击UIA元素所必需的SendInput是正确的。
关于如何在右键单击之前强制光标移动到元素,您可以尝试将MOUSEEVENTF_MOVE标志添加到dwFlags。
如果仍然不起作用,也许尝试两次调用SendInput - 一次移动鼠标(使用“dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE”),第二次执行右键单击您现在正在执行的操作。
另外,你见过这个项目吗?
http://inputsimulator.codeplex.com/
目前还不确定鼠标输入支持的完整程度,但它可能很有用。
此外,这个问题也许有用:
答案 1 :(得分:1)
MSDN说:
如果指定了MOUSEEVENTF_ABSOLUTE值,则dx和dy包含0到65,535之间的标准化绝对坐标。事件过程将这些坐标映射到显示表面上。坐标(0,0)映射到显示表面的左上角;坐标(65535,65535)映射到右下角。在多监视器系统中,坐标映射到主监视器。
这意味着你不能只使用p.X和p.Y.你需要像这样标准化它,例如:
var virtualScreen = System.Windows.Forms.SystemInformation.VirtualScreen;
Int32 x = Convert.ToInt32((p.X - virtualScreen.Left) * 65536 / virtualScreen.Width);
Int32 y = Convert.ToInt32((p.Y - virtualScreen.Top) * 65536 / virtualScreen.Height);
并且您需要将其与MOUSEEVENTF_VIRTUALDESK
标志一起使用。
答案 2 :(得分:0)
如果您从控件派生,则可以使用以下内容:
var e = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Right);
e.RoutedEvent = Mouse.MouseDownEvent;
this.OnMouseDown(e);
答案 3 :(得分:0)
我检查了所有答案并编制了最终的工作解决方案。
public static void RightClick(this AutomationElement element)
{
Point p = element.GetClickablePoint();
NativeStructs.Input input = new NativeStructs.Input
{
type = NativeEnums.SendInputEventType.Mouse,
mouseInput = new NativeStructs.MouseInput
{
dx = 0,
dy = 0,
mouseData = 0,
dwFlags = NativeEnums.MouseEventFlags.Absolute | NativeEnums.MouseEventFlags.RightDown | NativeEnums.MouseEventFlags.Move,
time = 0,
dwExtraInfo = IntPtr.Zero,
},
};
var primaryScreen = Screen.PrimaryScreen;
input.mouseInput.dx = Convert.ToInt32((p.X - primaryScreen.Bounds.Left) * 65536 / primaryScreen.Bounds.Width);
input.mouseInput.dy = Convert.ToInt32((p.Y - primaryScreen.Bounds.Top) * 65536 / primaryScreen.Bounds.Height);
NativeMethods.SendInput(1, ref input, Marshal.SizeOf(input));
input.mouseInput.dwFlags = NativeEnums.MouseEventFlags.Absolute | NativeEnums.MouseEventFlags.RightUp | NativeEnums.MouseEventFlags.Move;
NativeMethods.SendInput(1, ref input, Marshal.SizeOf(input));
}
internal static class NativeMethods
{
[DllImport("user32.dll", SetLastError = true)]
internal static extern uint SendInput(uint nInputs, ref NativeStructs.Input pInputs, int cbSize);
}
internal static class NativeStructs
{
[StructLayout(LayoutKind.Sequential)]
internal struct Input
{
public NativeEnums.SendInputEventType type;
public MouseInput mouseInput;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MouseInput
{
public int dx;
public int dy;
public uint mouseData;
public NativeEnums.MouseEventFlags dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
}
internal static class NativeEnums
{
internal enum SendInputEventType : int
{
Mouse = 0,
Keyboard = 1,
Hardware = 2,
}
[Flags]
internal enum MouseEventFlags : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
Absolute = 0x8000,
}
}