我需要在C#中发送鼠标信号,以便其他应用程序注册它们。确切地说,我需要模拟鼠标按钮和鼠标的移动。有没有办法在C#中做到这一点? (Windows)中
答案 0 :(得分:3)
mouse's cursor position是一个可设置的属性 - 您可以使用它在任何地方移动鼠标。
答案 1 :(得分:3)
答案 2 :(得分:1)
就按钮而言,您可以执行以下操作
[DllImport("user32.dll")]
public static extern uint SendInput(uint nInputs, ref Input pInputs, int cbSize);
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
public static void DoMouseClick()
{
var input =
new Input
{
type = 0,
mouseinput =
new Mouseinput
{
dx = Cursor.Position.X,
dy = Cursor.Position.Y,
dwFlags = MOUSEEVENTF_LEFTDOWN
}
};
SendInput(1, ref input, 28);
}
[StructLayout(LayoutKind.Explicit, Size = 28)]
public struct Input
{
[FieldOffset(0)]
public uint type;
[FieldOffset(4)]
public Mouseinput mouseinput;
};
[StructLayout(LayoutKind.Explicit, Size = 28)]
public struct Mouseinput
{
[FieldOffset(0)]
public int dx;
[FieldOffset(4)]
public int dy;
[FieldOffset(8)]
public uint mouseData;
[FieldOffset(12)]
public uint dwFlags;
[FieldOffset(16)]
public uint time;
[FieldOffset(20)]
public uint dwExtraInfo;
}