我尝试使用C# .NET中的user32 dll中的SendInput()模拟(全局)鼠标点击。我已经尝试过使用SendInput()进行键盘输入,它可以正常工作。但由于某些原因,它无法用于鼠标输入。
以下是我的代码:
为了简单起见,我没有放置KEYBDINPUT和HARDWAREINPUT(结构体)的定义,因为MOUSEINPUT在这里是唯一相关的。
以下是我的代码:
为了简单起见,我没有放置KEYBDINPUT和HARDWAREINPUT(结构体)的定义,因为MOUSEINPUT在这里是唯一相关的。
[DllImport("user32.dll")]
internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
//main calling method
public static void CLICK()
{
INPUT []i = new INPUT[1];
i[0].type = 0;
i[0].U.mi.time = 0;
i[0].U.mi.dwFlags = MOUSEEVENTF.LEFTDOWN | MOUSEEVENTF.ABSOLUTE;
i[0].U.mi.dwExtraInfo = UIntPtr.Zero;
i[0].U.mi.dx = 1;
i[0].U.mi.dy = 1;
SendInput(1, i, INPUT.Size);
}
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
internal uint type;
internal InputUnion U;
internal static int Size
{
get { return Marshal.SizeOf(typeof(INPUT)); }
}
}
[StructLayout(LayoutKind.Explicit)]
internal struct InputUnion
{
[FieldOffset(0)]
internal MOUSEINPUT mi;
[FieldOffset(0)]
internal KEYBDINPUT ki;
[FieldOffset(0)]
internal HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT
{
internal long dx;
internal long dy;
internal int mouseData;
internal MOUSEEVENTF dwFlags;
internal uint time;
internal UIntPtr dwExtraInfo;
}
[Flags]
internal enum MOUSEEVENTF : uint
{
ABSOLUTE = 0x8000,
HWHEEL = 0x01000,
MOVE = 0x0001,
MOVE_NOCOALESCE = 0x2000,
LEFTDOWN = 0x0002,
LEFTUP = 0x0004,
RIGHTDOWN = 0x0008,
RIGHTUP = 0x0010,
MIDDLEDOWN = 0x0020,
MIDDLEUP = 0x0040,
VIRTUALDESK = 0x4000,
WHEEL = 0x0800,
XDOWN = 0x0080,
XUP = 0x0100
}
此外,我尝试了不同的x、y点和其他dwFlags(例如LEFTUP、MOVE等),但目前仍未见效。