最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#模擬實現鼠標自動點擊與消息發(fā)送功能

 更新時間:2022年08月22日 14:52:53   作者:Csharp 小記  
這篇文章主要為大家詳細介紹了C#如何利用windows api來模擬實現鼠標點擊、右擊、雙擊以及發(fā)送文本功能,文中的示例代碼講解詳細,感興趣的可以了解一下

一個簡單的實現版本,沒有去Hook鍵鼠等操作,事先錄制好操作步驟(將鼠標移動到需要操作的位置,按下熱鍵執(zhí)行相應動作),點擊運行即可。

主要還是用windows api來實現,模擬點擊、右擊、雙擊、發(fā)送文本等。

代碼可能略長一點,下面發(fā)下關鍵代碼

主要的思路就是操作熱鍵的時候,將操作類型以及坐標記錄到一個List中,然后利用Windows Api循環(huán)執(zhí)行List中的數據

實現功能

模擬鼠標點擊、文本輸入

開發(fā)環(huán)境

開發(fā)工具: 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);
      }
  }
 
  //執(zhí)行點擊事件
  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;
      }
  }
 
  //執(zhí)行設置文本事件
  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>
  /// 執(zhí)行
  /// </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) + "次執(zhí)行結束");
 
              try
              {
                  CancelTask.Token.ThrowIfCancellationRequested();
              }
              catch (System.OperationCanceledException ex)
              {
                  AddLog("已手動結束執(zhí)行");
                  return;
              }
          }
          AddLog("自動執(zhí)行結束...");
          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 = "發(fā)送了文本:" + 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#模擬實現鼠標自動點擊與消息發(fā)送功能的文章就介紹到這了,更多相關C#鼠標點擊 消息發(fā)送內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C#運行時相互關系淺析

    C#運行時相互關系淺析

    這篇文章介紹了C#運行時相互關系,包括運行時類型、對象、線程棧和托管堆之間的相互關系,靜態(tài)方法、實例方法和虛方法的區(qū)別等等。
    2015-10-10
  • C#自定義控件指示燈效果

    C#自定義控件指示燈效果

    在C#中實現一個指示燈控件,可以通過GDI+技術繪制,首先使用Pen對象繪制外環(huán),然后用SolidBrush對象填充內圓,通過RectangleF定義繪制和填充的邊界,控件的屬性包括顏色、間隙、外環(huán)寬度等,本文給大家介紹C#自定義控件指示燈效果,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • 從Request.Url中獲取根網址的簡單操作

    從Request.Url中獲取根網址的簡單操作

    這篇文章主要介紹了從Request.Url中獲取根網址的簡單操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • C#實現Winform版計算器

    C#實現Winform版計算器

    這篇文章主要為大家詳細介紹了C#實現Winform版計算器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • C#使用Linq to XML進行XPath查詢的代碼實現

    C#使用Linq to XML進行XPath查詢的代碼實現

    最近在用到HtmlAgliltyPack進行結點查詢時,發(fā)現這里選擇結點使用的是XPath,所以這里總結一下在C#中使用XPath查詢XML的方法,習慣了用Linq,這里也是用的Linq to xml的,需要的朋友可以參考下
    2024-08-08
  • C# winForm實現的氣泡提示窗口功能示例

    C# winForm實現的氣泡提示窗口功能示例

    這篇文章主要介紹了C# winForm實現的氣泡提示窗口功能,涉及C# winForm窗口屬性與設置相關操作技巧,需要的朋友可以參考下
    2018-03-03
  • C#獲取真實IP地址實現方法

    C#獲取真實IP地址實現方法

    這篇文章主要介紹了C#獲取真實IP地址實現方法,對比了C#獲取IP地址的常用方法并實例展示了C#獲取真實IP地址的方法,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • c#利用system.net發(fā)送html格式郵件

    c#利用system.net發(fā)送html格式郵件

    這篇文章主要介紹了c#利用system.net發(fā)送html格式郵件的示例,帶有抄送、密送、附件功能,需要的朋友可以參考下
    2014-02-02
  • ToLua框架下C#與Lua代碼的互調操作

    ToLua框架下C#與Lua代碼的互調操作

    這篇文章主要介紹了ToLua框架下C#與Lua代碼的互調操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • C#編程高并發(fā)的幾種處理方法詳解

    C#編程高并發(fā)的幾種處理方法詳解

    這篇文章主要為大家詳細介紹了C#編程高并發(fā)的幾種處理方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論

连南| 涞源县| 灌云县| 普兰县| 金沙县| 锦州市| 民权县| 盘锦市| 九龙县| 卢氏县| 西平县| 彭山县| 霍邱县| 荆州市| 沈阳市| 陆河县| 大余县| 常山县| 阿鲁科尔沁旗| 本溪| 定西市| 邵阳市| 平乡县| 福鼎市| 隆子县| 庄河市| 静海县| 商南县| 辽中县| 南和县| 安康市| 根河市| 大埔区| 彰化市| 东港市| 隆尧县| 博客| 华阴市| 酉阳| 北宁市| 隆回县|