C#采用mouse_event函数实现模拟鼠标功能

本文详细介绍了如何使用C#进行鼠标操作模拟,包括鼠标移动、左键点击、右键点击等常见操作的具体实现方式。文章深入解析了mouse_event函数的使用方法,以及如何通过发送特定消息模拟鼠标事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面我通过代码为大家分享下C#模拟鼠标,具体内容如下:

想必有很多人在项目开发中可能遇见需要做模拟鼠标点击的小功能,很多人会在百度过后采用mouse_event这个函数,不过我并不想讨论如何去使用mouse_event函数怎么去使用,因为那没有多大意义。

?
1
2
3
4
5
6
7
8
static void mouse_event( int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo)
{
  int x = dx, y = dy;
  edit_position(dwFlags, dx, dy, ref x, ref y);
  IntPtr hWndFromPoint = WindowFromPoint(x, y);
  screen_to_client(hWndFromPoint, ref x, ref y);
  send_message(hWndFromPoint, dwFlags, cButtons, x, y);
}

 
上述代码你发现了什么?如果你发现说明你知道了本文到底在写什么东东 说不定你会有一些兴趣看下去,不过想到我如今混那么凄惨 在工地上做干活 不过也还好。

鼠标点击目标时会向鼠标所点击目标窗口投递消息,根据鼠标的按键、状态不同会投递不同的消息,一个完整的“鼠标左键单击”事件过程为“WM_LBUTTONDOWN +

WM_LBUTTONUP”即鼠标“先左键按下 + 后左键抬起”,由于mouse_event可以模拟鼠标点击过程而不是直接性一次完整的鼠标单击过程,所以同样存在“按下、抬起”

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);  
mouse_event在没有提供MOUSEEVENTF_MOVE量时光标不会移动到相对位置,“光标相对位置=光标现行位置+新光标位置”如果提供量“MOUSEEVENTF_ABSOLUTE”绝对位置,则会以“新光标位置”为准而不会添加“光标现行位置”

?
1
2
3
4
5
6
7
8
9
10
static void edit_position( int dwFlags, int dx, int dy, ref int x, ref int y)
{
  Point pos = MousePosition;
  x = x + pos.X;
  y = y + pos.Y;
  if ((dwFlags | MOUSEEVENTF_ABSOLUTE) == dwFlags)
   SetCursorPos(dx, dy);
  if ((dwFlags | MOUSEEVENTF_MOVE) == dwFlags)
   SetCursorPos(x, y);
}

edit_position函数主要用于对MOUSEEVENTF_MOVE于MOUSEEVENTF_ABSOLUTE

相对/绝对光标位置修改的一个支持

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static void send_message(IntPtr hWnd, int dwFlags, int cButtons, int x, int y)
{
  if ((dwFlags | MOUSEEVENTF_LEFTDOWN) == dwFlags)
   SendMessage(hWnd, WM_LBUTTONDOWN, cButtons, MakeDWord(x, y));
  if ((dwFlags | MOUSEEVENTF_LEFTUP) == dwFlags)
   SendMessage(hWnd, WM_LBUTTONUP, cButtons, MakeDWord(x, y));
  if ((dwFlags | MOUSEEVENTF_RIGHTDOWN) == dwFlags)
   SendMessage(hWnd, WM_RBUTTONDOWN, cButtons, MakeDWord(x, y));
  if ((dwFlags | MOUSEEVENTF_RIGHTUP) == dwFlags)
   SendMessage(hWnd, WM_RBUTTONUP, cButtons, MakeDWord(x, y));
  if ((dwFlags | MOUSEEVENTF_MIDDLEDOWN) == dwFlags)
   SendMessage(hWnd, WM_MBUTTONDOWN, cButtons, MakeDWord(x, y));
  if ((dwFlags | MOUSEEVENTF_MIDDLEUP) == dwFlags)
   SendMessage(hWnd, WM_MBUTTONUP, cButtons, MakeDWord(x, y));
}

 

send_message函数主要用于模拟鼠标点击的过程,上面我提到“先左键按下 + 后左键抬起”在上面的代码中你会看的清楚的不得了,如果相反你可以去尝试一番会有什么后果与其说

不如你们自己做更要来的快些。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
static int MakeDWord( int low, int high)
{
  return low + (high * Abs(~ ushort .MaxValue));
}
static int Abs( int value)
{
  return ((value >> 31) ^ value) - (value >> 31);
}
MakeDWord / 合并整数,函数主要是把两个 short 合并为一个 int ,分为low、high两部分
  
  
static bool screen_to_client(IntPtr hwnd, ref int x, ref int y)
{
  bool bRetVal = false ;
  Point lpptPos = new Point(x, y);
  if ((bRetVal = ScreenToClient(hwnd, ref lpptPos)))
  {
   x = lpptPos.X;
   y = lpptPos.Y;
  }
  return bRetVal;
}
screen_to_client函数故名思意,它主要用于把屏幕上的坐标转换到窗口客户上对应坐标
public const int WM_LBUTTONDOWN = 513; // 鼠标左键按下
public const int WM_LBUTTONUP = 514; // 鼠标左键抬起
public const int WM_RBUTTONDOWN = 516; // 鼠标右键按下
public const int WM_RBUTTONUP = 517; // 鼠标右键抬起
public const int WM_MBUTTONDOWN = 519; // 鼠标中键按下
public const int WM_MBUTTONUP = 520; // 鼠标中键抬起
public const int MOUSEEVENTF_MOVE = 0x0001; // 移动鼠标  
public const int MOUSEEVENTF_LEFTDOWN = 0x0002; // 鼠标左键按下 
public const int MOUSEEVENTF_LEFTUP = 0x0004; // 鼠标左键抬起 
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // 鼠标右键按下 
public const int MOUSEEVENTF_RIGHTUP = 0x0010; // 鼠标右键抬起  
public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // 鼠标中键按下
public const int MOUSEEVENTF_MIDDLEUP = 0x0040; // 鼠标中键抬起  
public const int MOUSEEVENTF_ABSOLUTE = 0x8000; // 绝对坐标
 
 
[DllImport( "user32.dll" , SetLastError = true )]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
[DllImport( "user32.dll" , SetLastError = true )]
public static extern IntPtr WindowFromPoint( int xPoint, int yPoint);
[DllImport( "user32.dll" , SetLastError = true )]
public static extern int SetCursorPos( int x, int y);
[DllImport( "user32.dll" , SetLastError = true )]
public static extern bool ScreenToClient(IntPtr hWnd, ref Point lppt);
// [DllImport("user32", SetLastError = true)]
// public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);


鼠标右键单击(静默):

 

复制代码代码如下:

mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0);  

 

鼠标左键双击(静默):

复制代码代码如下:

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0);  

 

鼠标移动(相对位置):

 

复制代码代码如下:

mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0);  

 

鼠标移动(绝对位置):

 

复制代码代码如下:

mouse_event(MOUSEEVENTF_ABSOLUTE, 100, 50, 0, 0);  

 

以上内容比较多请认真学习,希望能够帮助到大家。

转载于:https://www.cnblogs.com/ssslgf/p/10231331.html

WinAPI-Wrapper 模拟鼠标点击 用于模拟鼠标移动、点击、窗口操作等的Windows API包装器类。 API 下面是一些可用的方法的总结。有更多的方法和类,比下面列出的要多,但目的是要大致了解包装器能做什么。要查看关于特定方法的详细信息和参数的详细信息,请查看代码本身,因为它的注释很好。 Mouse.cs public static void LeftClick(); public static void RightClick(); public static void MiddleClick(); public static void LeftDown(); public static void LeftUp(); public static void RightDown(); public static void RightUp(); public static void MiddleDown(); public static void MiddleUp(); public static void Move(int x, int y); public static void LeftDrag(Point point1, Point point2, int interval, int lag); Window.cs public static bool DoesExist(string windowTitle); public static IntPtr Get(string windowTitle); public static IntPtr GetFocused(); public static void SetFocused(IntPtr hWnd); public static bool IsFocused(IntPtr hWnd); public static void Move(IntPtr hWnd, int x, int y); public static void Resize(IntPtr hWnd, int width, int height); public static void Hide(IntPtr hWnd); public static void Show(IntPtr hWnd); public static Rectangle GetDimensions(IntPtr hWnd); public static Size GetSize(IntPtr hWnd); public static Point GetLocation(IntPtr hWnd); public static string GetTitle(IntPtr hWnd); public static void SetTitle(IntPtr hWnd, string title); public static void Maximize(IntPtr hWnd); public static void Minimize(IntPtr hWnd); public static void Normalize(IntPtr hWnd); public static Bitmap Screenshot(IntPtr hWnd); public static void RemoveMenu(IntPtr hWnd); public static void Close(IntPtr hWnd); public static void DisableCloseButton(IntPtr hWnd); public static void DisableMaximizeButton(IntPtr hWnd); public static void DisableMinimizeButton(IntPtr hWnd); public static void EnableMouseTransparency(IntPtr hWnd); public static Point ConvertToWindowCoordinates(IntPtr hWnd, int x, int y); public static Point GetCoordinateRelativeToWindow(IntPtr hWnd); Desktop.cs public static Bitmap Screenshot(); public static void HideTaskBar(); public static void ShowTaskBar(); public static int GetWidth(); public static int GetHeight(); 使用 在windows api文件夹中编译代码会产生一个.dll文件。任何引用这个.dll的ccode都可以使用包装器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值