在C#开发中,实现鼠标自动移动和点击操作的核心思路是调用Windows系统提供的user32.dll动态链接库中的底层API函数,这些函数可以直接向系统发送鼠标操作的指令,从而模拟真实的鼠标行为。

所需API函数说明
要实现鼠标控制,首先需要引入user32.dll中的两个关键函数:SetCursorPos和mouse_event,前者用于设置鼠标光标的位置,后者用于触发鼠标的相关事件。
我们需要先定义这两个函数的导入声明,代码如下:
using System;
using System.Runtime.InteropServices;
public class MouseSimulator
{
// 设置鼠标光标位置的函数,x和y为目标位置的屏幕坐标
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int x, int y);
// 模拟鼠标事件的函数,dwFlags为事件类型,dx和dy为坐标参数,dwData为额外数据,dwExtraInfo为附加信息
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
// 定义鼠标事件对应的常量
public const int MOUSEEVENTF_LEFTDOWN = 0x02; // 左键按下
public const int MOUSEEVENTF_LEFTUP = 0x04; // 左键抬起
public const int MOUSEEVENTF_RIGHTDOWN = 0x08; // 右键按下
public const int MOUSEEVENTF_RIGHTUP = 0x10; // 右键抬起
public const int MOUSEEVENTF_MIDDLEDOWN = 0x20; // 中键按下
public const int MOUSEEVENTF_MIDDLEUP = 0x40; // 中键抬起
public const int MOUSEEVENTF_MOVE = 0x01; // 鼠标移动
}
鼠标移动实现
鼠标移动只需要调用SetCursorPos函数,传入目标位置的X和Y坐标即可,屏幕坐标的原点在屏幕左上角,X轴向右递增,Y轴向下递增。
以下是一个将鼠标移动到屏幕坐标(500, 300)处的示例:
// 将鼠标移动到屏幕坐标(500, 300)
bool isSuccess = MouseSimulator.SetCursorPos(500, 300);
if (isSuccess)
{
Console.WriteLine("鼠标移动成功");
}
else
{
Console.WriteLine("鼠标移动失败");
}
鼠标点击实现
左键单击
左键单击需要依次触发左键按下和左键抬起两个事件,两个事件之间可以加上短暂的延迟,模拟真实的操作节奏。
// 模拟左键单击 MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // 延迟100毫秒,模拟真实点击间隔 System.Threading.Thread.Sleep(100); MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
右键单击
右键单击的逻辑和左键类似,只是替换为右键对应的事件常量。
// 模拟右键单击 MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); System.Threading.Thread.Sleep(100); MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
双击操作
双击操作就是连续执行两次单击操作,两次单击之间的间隔建议设置在200毫秒以内,符合系统对双击的判定标准。
// 模拟左键双击 // 第一次单击 MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); System.Threading.Thread.Sleep(100); MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // 间隔150毫秒 System.Threading.Thread.Sleep(150); // 第二次单击 MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); System.Threading.Thread.Sleep(100); MouseSimulator.mouse_event(MouseSimulator.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
完整操作示例
以下是一个完整的示例程序,实现先移动鼠标到指定位置,然后执行左键单击的操作:
using System;
using System.Runtime.InteropServices;
using System.Threading;
class Program
{
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
static void Main(string[] args)
{
Console.WriteLine("3秒后开始执行鼠标操作,请将鼠标移到安全位置");
Thread.Sleep(3000);
// 移动鼠标到(500, 300)
SetCursorPos(500, 300);
Thread.Sleep(500);
// 执行左键单击
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Thread.Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Console.WriteLine("鼠标操作执行完成");
}
}
注意事项
- 模拟鼠标操作需要程序有足够的系统权限,部分高权限应用可能无法被模拟操作触发。
- 坐标参数使用的是屏幕绝对坐标,如果程序运行在多屏幕环境下,需要确认坐标对应的屏幕范围。
- 操作的延迟时间可以根据实际需求调整,过短的延迟可能导致操作不被系统正确识别。
- 不要将这类模拟操作用于违反软件使用协议或法律法规的场景。
CSharp鼠标模拟自动化操作user32_dll修改时间:2026-07-18 19:48:31