请选择 进入手机版 | 继续访问电脑版

Hello Mat

 找回密码
 立即注册
查看: 6339|回复: 8

模拟鼠标(mouse_event)

[复制链接]

1278

主题

1504

帖子

90

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22549
发表于 2019-11-28 23:39:06 | 显示全部楼层 |阅读模式
C# 模拟鼠标(mouse_event)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;

  8. namespace image_to_label
  9. {
  10.     public class MouseMonitor
  11.     {
  12.         //public const int WM_LBUTTONDOWN = 513; // 鼠标左键按下  
  13.         //public const int WM_LBUTTONUP = 514; // 鼠标左键抬起  
  14.         //public const int WM_RBUTTONDOWN = 516; // 鼠标右键按下  
  15.         //public const int WM_RBUTTONUP = 517; // 鼠标右键抬起  
  16.         //public const int WM_MBUTTONDOWN = 519; // 鼠标中键按下  
  17.         //public const int WM_MBUTTONUP = 520; // 鼠标中键抬起  

  18.         public const int MOUSEEVENTF_MOVE = 0x0001; // 移动鼠标         
  19.         public const int MOUSEEVENTF_LEFTDOWN = 0x0002; // 鼠标左键按下        
  20.         public const int MOUSEEVENTF_LEFTUP = 0x0004; // 鼠标左键抬起        
  21.         public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // 鼠标右键按下      
  22.         public const int MOUSEEVENTF_RIGHTUP = 0x0010; // 鼠标右键抬起         
  23.         public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // 鼠标中键按下   
  24.         public const int MOUSEEVENTF_MIDDLEUP = 0x0040; // 鼠标中键抬起           
  25.         public const int MOUSEEVENTF_ABSOLUTE = 0x8000; // 绝对坐标   

  26.         //[DllImport("user32.dll", SetLastError = true)]
  27.         [DllImport("user32.dll", SetLastError = true, EntryPoint = "mouse_event")]
  28.         public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

  29.         [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetCursorPos")]
  30.         public static extern int SetCursorPos(int x, int y);

  31.         [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetCursorPos")]
  32.         public static extern int GetCursorPos(out Point x);

  33.     }
  34. }
复制代码
相应的鼠标光标坐标为:
  1.             try
  2.             {
  3.                 int Mx = 0, My = 0;
  4.                 Mx = this.hWindowControl1.Location.X;
  5.                 My = this.hWindowControl1.Location.Y + 0 * this.hWindowControl1.Height;
  6.                 Point lpPoint = new Point(0, 0);
  7.                 MouseMonitor.GetCursorPos(out lpPoint);
  8.                 Mx = lpPoint.X;
  9.                 My = lpPoint.Y;
  10.                 MouseMonitor.SetCursorPos(Mx, My);
  11.                 MouseMonitor.mouse_event(MouseMonitor.MOUSEEVENTF_RIGHTDOWN | MouseMonitor.MOUSEEVENTF_RIGHTUP | MouseMonitor.MOUSEEVENTF_MOVE, Mx, My, 0, 0);
  12.             }
  13.             catch { }
复制代码




参考:
C# 模拟鼠标(mouse_event)
C#调用API:mouse_event 模拟鼠标事件
send_mouse_down_event,send_mouse_up_event等如何使用



算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-3-29 09:58 , Processed in 0.234030 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表