控制鼠标操作包括很多种,如限定鼠标的移动范围、设置鼠标的左右键、控制鼠标的显示和隐藏等。本节中将通过两个具体的示例来介绍有关控制鼠标操作方面的知识。
1.限定鼠标的移动范围
利用API函数ClipCursor和GetWindowRect可以实现限定鼠标移动范围的功能。API函数声明如下:
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
public extern static int ClipCursor(ref RECT lpRect);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public extern static int GetWindowRect(int hwnd, ref RECT lpRect);
示例 控制鼠标移动
本示例通过API函数ClipCursor和GetWindowRect实现了限定鼠标移动范围的功能。
程序主要代码如下。
单击【控制鼠标移动】按钮,鼠标只能在窗体中移动,关键代码如下:
public struct RECT//声明参数的值
{
public int left;
public int top;
public int right;
public int bottom;
}
public void Lock(System.Windows.Forms.Form ObjectForm)
{
RECT _FormRect = new RECT();
GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
ClipCursor(ref _FormRect);
}
单击【恢复移动】按钮,鼠标恢复移动,关键代码如下:
public void UnLock()
{
RECT _ScreenRect = new RECT();
_ScreenRect.top = 0;
_ScreenRect.left = 0;
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
ClipCursor(ref _ScreenRect); }
2.鼠标设置
设置鼠标包括设置鼠标的左右键、显示与隐藏鼠标和设置双击鼠标的时间间隔等。通常使用API函数SwapMouseButton、ShowCursor、SetDoubleClickTime和GetDoubleClickTime对鼠标进行设置。这几个函数的声明如下:
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
public extern static int SwapMouseButton(int bSwap);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
public extern static bool ShowCursor(bool bShow);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
public extern static int SetDoubleClickTime(int wCount);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
public extern static int GetDoubleClickTime();
示例 鼠标设置
本示例通过API函数对鼠标进行设置,通过SwapMouseButton函数实现隐藏鼠标光标, ShowCursor函数实现显示鼠标光标,SetDoubleClickTime函数设置鼠标双击时间,GetDoubleClickTime函数获取鼠标双击时间。
程序主要代码如下。
单击【获取鼠标双击时间】按钮,获取双击时间,并在消息框中显示,关键代码如下:
public string DoubleClickTime_Get()
{
return GetDoubleClickTime().ToString();
}
单击【设置鼠标双击时间】按钮,在文本框中输入设置时间,关键代码如下:
public void DoubleClickTime_Set(int MouseDoubleClickTime)
{
SetDoubleClickTime(MouseDoubleClickTime);
}
单击【隐藏鼠标】按钮,鼠标在窗体上隐藏,关键代码如下:
public void Hide()
{
ShowCursor(false);}
单击【显示鼠标】按钮,鼠标显示,关键代码如下:
public void Show()
{
ShowCursor(true);}
单击【鼠标左键】按钮,鼠标用左键控制鼠标单击事件,关键代码如下:
private void bntLeft_Click(object sender, EventArgs e)
{
this.DefaultLeftButton();}
单击【鼠标右键】按钮,鼠标用右键控制鼠标单击事件,关键代码如下:
private void bntRight_Click(object sender, EventArgs e)
{
this.DefaultRightButton();}
可以通过两个函数操作鼠标:
[DllImport("user32.dll")] static extern bool SetCursorPos(int X, int Y); [DllImport("user32.dll")] static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); [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 }
SetCursorPos使鼠标移动到指定位置;mouse_event使用MouseEventFlag枚举中的Move,也可以使鼠标移动。
mouse_event中使用不同的枚举值可以模拟不同的鼠标事件。
值得注意的是有几点:
1. 我们不能用mouse_event(MouseEventFlag.LeftDown, 10, 10, 0, UIntPtr.Zero);去模拟在(10, 10)处的左键事件,我们需要把这步拆成两步:
第一步:移动鼠标到(10,10)处,用SetCursorPos(10, 10);
第二步:触发左键,用mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
本质上是两步的事件,不能把window API 想的太聪明,认为它会自动跑到(10,10)处,再左键
2. MouseEventFlag的枚举值可以多个一起用,使用 | 操作符
鼠标左键按下和松开两个事件的组合即一次单击:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
两次连续的鼠标左键单击事件 构成一次鼠标双击事件:
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 )
3. MouseEventFlag中有个Absolute枚举,如果没指定Absolute, 则mouse_event的操作是相对于上次鼠标所在的位置;如果指定了Absolute,则是相对于整个屏幕坐标的位置。
这里要注意,指定Absolute,鼠标的坐标会被约束在[0, 65535]之间。0即对应屏幕左,65535即对应屏幕右下角。
MSDN原话如下:
If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner of the display surface, (65535,65535) maps onto the lower-right corner.
所以模拟在(10, 10)处的左键,代码应改为:
mouse_event(MOUSEEVENTF_LEFTDOWN, 10 * 65536 / Screen.PrimaryScreen.Bounds.Width, 10 * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);
如果显示器是一拖二的,想在第二个屏上使用mouse_event,就不能用Screen.PrimaryScreen了

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步