首頁 > 軟體

C#模擬實現滑鼠自動點選與訊息傳送功能

2022-08-22 18:02:45

一個簡單的實現版本,沒有去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#模擬實現滑鼠自動點選與訊息傳送功能的文章就介紹到這了,更多相關C#滑鼠點選 訊息傳送內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com