網站首頁 編程語言 正文
一個簡單的實現版本,沒有去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);
}
實現效果
原文鏈接:https://blog.csdn.net/qq_27410185/article/details/124525181
相關推薦
- 2023-02-05 Flutter實現固定header底部滑動頁效果示例_Android
- 2024-01-06 TCP的三次握手和四次揮手
- 2022-06-28 python神經網絡使用tensorflow構建長短時記憶LSTM_python
- 2022-11-23 關于vba代碼運行時錯誤1004?應用程序定義或對象定義錯誤問題_VBA
- 2022-08-27 深入理解Redis內存淘汰策略_Redis
- 2023-05-29 Python中Merge使用的示例詳解_python
- 2022-04-07 C++中類的默認成員函數詳解_C 語言
- 2022-08-13 深入理解Linux內核select多路復用原理
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支