C#模拟实现鼠标自动点击与消息发送功能

作者:Csharp 时间:2023-02-09 08:51:14 

一个简单的实现版本,没有去Hook键鼠等操作,事先录制好操作步骤(将鼠标移动到需要操作的位置,按下热键执行相应动作),点击运行即可。

主要还是用windows api来实现,模拟点击、右击、双击、发送文本等。

代码可能略长一点,下面发下关键代码

主要的思路就是操作热键的时候,将操作类型以及坐标记录到一个List中,然后利用Windows Api循环执行List中的数据

实现功能

模拟鼠标点击、文本输入

开发环境

开发工具: Visual Studio 2013

.NET Framework版本:4.5

实现代码

#region 鼠标操作类型
private const int MOUSEEVENTF_MOVE = 1;//鼠标移动

private const int MOUSEEVENTF_LEFTDOWN = 2;//按下鼠标左键

private const int MOUSEEVENTF_LEFTUP = 4;//抬起鼠标左键

private const int MOUSEEVENTF_RIGHTDOWN = 8;//按下鼠标右键

private const int MOUSEEVENTF_RIGHTUP = 16;//抬起鼠标右键

#endregion

#region Windows Api
/// <summary>
/// 鼠标操作
/// </summary>
/// <param name="dwFlags"></param>
/// <param name="dx"></param>
/// <param name="dy"></param>
/// <param name="cButtons"></param>
/// <param name="dwExtraInfo"></param>
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

/// <summary>
/// 设置鼠标位置
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);

/// <summary>
/// 注册热键
/// </summary>
/// <param name="hWnd"></param>
/// <param name="id"></param>
/// <param name="control"></param>
/// <param name="vk"></param>
/// <returns></returns>
[DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);

/// <summary>
/// 取消热键
/// </summary>
/// <param name="hWnd"></param>
/// <param name="id"></param>
/// <returns></returns>
[DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

#endregion
private List<EventClass> listEvent = new List<EventClass>();
/// <summary>
 /// 注册/取消热键
 /// </summary>
 /// <param name="isReg"></param>
 private void RegistKey(bool isReg)
 {
     if (isReg)
     {
         RegisterHotKey(base.Handle, 30001, MOD_CONTROL, Keys.D1);
         RegisterHotKey(base.Handle, 30002, MOD_CONTROL, Keys.D2);
         RegisterHotKey(base.Handle, 30003, MOD_CONTROL, Keys.D3);
         RegisterHotKey(base.Handle, 30004, MOD_CONTROL, Keys.D4);
         RegisterHotKey(base.Handle, 30005, MOD_CONTROL, Keys.E);
     }
     else
     {
         UnregisterHotKey(base.Handle, 30001);
         UnregisterHotKey(base.Handle, 30002);
         UnregisterHotKey(base.Handle, 30003);
         UnregisterHotKey(base.Handle, 30004);
         UnregisterHotKey(base.Handle, 30005);
     }
 }

//执行点击事件
 private void MouseClick(EventClass eventData)
 {
     SetCursorPos(eventData.X, eventData.Y);
     switch (eventData.clickType)
     {
         case ClickType.leftClick:
             mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
             mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
             break;
         case ClickType.rightClick:
             mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
             mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
             break;
         case ClickType.doubleClick:
             mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
             mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
             Thread.Sleep(100);
             mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
             mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
             break;
     }
 }

//执行设置文本事件
 private void SetText(EventClass eventData)
 {
     SendKeys.SendWait(eventData.Text);
 }

/// <summary>
 /// 录制
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnRecord_Click(object sender, EventArgs e)
 {
     CancelTask = new CancellationTokenSource();
     RegistKey(true);
     EnableControl(false);
     AddLog("正在录制...");
     KeyPress += new KeyPressEventHandler(Click_KeyPress);
 }

/// <summary>
 /// 执行
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnRun_Click(object sender, EventArgs e)
 {
     int interval = string.IsNullOrEmpty(txtInterval.Text) ? 0 : Convert.ToInt32(txtInterval.Text);

int count = string.IsNullOrEmpty(txtCount.Text) ? 1 : Convert.ToInt32(txtCount.Text);

Task.Factory.StartNew(() =>
     {
         for (int i = 0; i < count; i++)
         {
             foreach (EventClass current in listEvent)
             {
                 if (current.clickType == ClickType.SendKeys)
                 {
                     SetText(current);
                 }
                 else
                 {
                     MouseClick(current);
                 }

Thread.Sleep(interval * 1000);
             }

AddLog("第" + (i + 1) + "次执行结束");

try
             {
                 CancelTask.Token.ThrowIfCancellationRequested();
             }
             catch (System.OperationCanceledException ex)
             {
                 AddLog("已手动结束执行");
                 return;
             }
         }
         AddLog("自动执行结束...");
         KeyPress += new KeyPressEventHandler(Click_KeyPress);
     }, CancelTask.Token);
 }

private void Click_KeyPress(object sender, KeyPressEventArgs e)
  {
      string logStr = string.Empty;
      ClickType clickType = ClickType.leftClick;
      string key = e.KeyChar.ToString().ToUpper();
      switch (key)
      {
          case "1":
              clickType = ClickType.leftClick;
              logStr = "点击了鼠标左键";
              break;
          case "2":
              clickType = ClickType.rightClick;
              logStr = "点击了鼠标右键";
              break;
          case "3":
              clickType = ClickType.doubleClick;
              logStr = "双击了鼠标左键";
              break;
          case "4":
              clickType = ClickType.SendKeys;
              logStr = "发送了文本:" + txtValue.Text;
              break;
          default:
              logStr = "按下了" + e.KeyChar + "键,无效!";
              break;
      }

int x = Cursor.Position.X;
      int y = Cursor.Position.Y;
      AddLog("在 (" + x + "," + y + ") 位置," + logStr);

EventClass eventClass = new EventClass();
      eventClass.clickType = clickType;
      eventClass.X = x;
      eventClass.Y = y;
      if (!string.IsNullOrEmpty(txtValue.Text))
      {
          eventClass.Text = txtValue.Text;
      }
      listEvent.Add(eventClass);
  }

实现效果

C#模拟实现鼠标自动点击与消息发送功能

来源:https://blog.csdn.net/qq_27410185/article/details/124525181

标签:C#,鼠标,点击,消息,发送
0
投稿

猜你喜欢

  • idea切换分支的时候,忽略一些无用的修改设置

    2022-01-10 04:13:37
  • 详解c# 线程同步

    2023-11-23 08:54:48
  • 详解SpringBoot中Session超时原理说明

    2022-01-24 06:40:49
  • Eclipse配置Tomcat和JDK步骤图解

    2022-11-23 11:46:54
  • Spring Boot项目如何同时支持HTTP和HTTPS协议的实现

    2023-11-19 19:57:05
  • 深入解读Android的Volley库的功能结构

    2023-02-28 00:57:12
  • Android带进度的圆形进度条

    2023-08-05 21:47:18
  • JavaCV实现照片马赛克效果

    2023-04-27 15:55:14
  • Java如何实现http接口参数和返回值加密

    2023-08-23 12:28:37
  • 详解Java中的三种流程控制语句

    2023-11-04 17:06:34
  • 快速学习c# 枚举

    2022-07-19 18:12:12
  • Java中继承thread类与实现Runnable接口的比较

    2022-06-09 12:57:09
  • C#实现文本转语音功能

    2021-07-07 20:20:54
  • Springboot webscoket自定义定时器

    2023-02-12 05:34:02
  • Mybatis查询时,区分大小写操作

    2021-08-11 14:10:54
  • Java基础学习之字符串知识总结

    2021-06-30 10:23:56
  • java与c#的语法区别详细介绍

    2022-05-02 14:01:10
  • Android广播接实现监听电话状态(电话的状态,拦截)

    2022-12-21 18:50:49
  • C#微信开发之获取接口调用凭据

    2023-10-28 07:57:54
  • Java网络通信中ServerSocket的设计优化方案

    2023-06-14 08:25:53
  • asp之家 软件编程 m.aspxhome.com