Halcom 发表于 2019-11-28 23:39:06

模拟鼠标(mouse_event)

C# 模拟鼠标(mouse_event)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace image_to_label
{
    public class MouseMonitor
    {
      //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; // 绝对坐标   

      //
      
      public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

      
      public static extern int SetCursorPos(int x, int y);

      
      public static extern int GetCursorPos(out Point x);

    }
}相应的鼠标光标坐标为:
            try
            {
                int Mx = 0, My = 0;
                Mx = this.hWindowControl1.Location.X;
                My = this.hWindowControl1.Location.Y + 0 * this.hWindowControl1.Height;
                Point lpPoint = new Point(0, 0);
                MouseMonitor.GetCursorPos(out lpPoint);
                Mx = lpPoint.X;
                My = lpPoint.Y;
                MouseMonitor.SetCursorPos(Mx, My);
                MouseMonitor.mouse_event(MouseMonitor.MOUSEEVENTF_RIGHTDOWN | MouseMonitor.MOUSEEVENTF_RIGHTUP | MouseMonitor.MOUSEEVENTF_MOVE, Mx, My, 0, 0);
            }
            catch { }



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



页: [1]
查看完整版本: 模拟鼠标(mouse_event)