c# winform 控制鼠标,让鼠标在窗体的指定坐标上单击
1个回答
展开全部
在你的窗体代码中添加如下API定义代码(引人System.Runtime.InteropServices命名空间(using System.Runtime.InteropServices;)):
[Flags]
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
在你的按钮单击事件中(Button_Click)加入如下代码:
Cursor.Position = new Point(0 /*x坐标*/, 0 /*y坐标*/); //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown,0,0,0,UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp,0,0,0,UIntPtr.Zero);
//这里是鼠标点击的代码(左键)
[Flags]
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
在你的按钮单击事件中(Button_Click)加入如下代码:
Cursor.Position = new Point(0 /*x坐标*/, 0 /*y坐标*/); //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown,0,0,0,UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp,0,0,0,UIntPtr.Zero);
//这里是鼠标点击的代码(左键)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询