C#模擬實(shí)現(xiàn)鼠標(biāo)自動(dòng)點(diǎn)擊與消息發(fā)送功能
一個(gè)簡(jiǎn)單的實(shí)現(xiàn)版本,沒有去Hook鍵鼠等操作,事先錄制好操作步驟(將鼠標(biāo)移動(dòng)到需要操作的位置,按下熱鍵執(zhí)行相應(yīng)動(dòng)作),點(diǎn)擊運(yùn)行即可。
主要還是用windows api來實(shí)現(xiàn),模擬點(diǎn)擊、右擊、雙擊、發(fā)送文本等。
代碼可能略長(zhǎng)一點(diǎn),下面發(fā)下關(guān)鍵代碼
主要的思路就是操作熱鍵的時(shí)候,將操作類型以及坐標(biāo)記錄到一個(gè)List中,然后利用Windows Api循環(huán)執(zhí)行List中的數(shù)據(jù)
實(shí)現(xiàn)功能
模擬鼠標(biāo)點(diǎn)擊、文本輸入
開發(fā)環(huán)境
開發(fā)工具: Visual Studio 2013
.NET Framework版本:4.5
實(shí)現(xiàn)代碼
#region 鼠標(biāo)操作類型
private const int MOUSEEVENTF_MOVE = 1;//鼠標(biāo)移動(dòng)
private const int MOUSEEVENTF_LEFTDOWN = 2;//按下鼠標(biāo)左鍵
private const int MOUSEEVENTF_LEFTUP = 4;//抬起鼠標(biāo)左鍵
private const int MOUSEEVENTF_RIGHTDOWN = 8;//按下鼠標(biāo)右鍵
private const int MOUSEEVENTF_RIGHTUP = 16;//抬起鼠標(biāo)右鍵
#endregion
#region Windows Api
/// <summary>
/// 鼠標(biāo)操作
/// </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>
/// 設(shè)置鼠標(biāo)位置
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
/// <summary>
/// 注冊(cè)熱鍵
/// </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);
#endregionprivate List<EventClass> listEvent = new List<EventClass>();
/// <summary>
/// 注冊(cè)/取消熱鍵
/// </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í)行點(diǎn)擊事件
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í)行設(shè)置文本事件
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í)行結(jié)束");
try
{
CancelTask.Token.ThrowIfCancellationRequested();
}
catch (System.OperationCanceledException ex)
{
AddLog("已手動(dòng)結(jié)束執(zhí)行");
return;
}
}
AddLog("自動(dòng)執(zhí)行結(jié)束...");
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 = "點(diǎn)擊了鼠標(biāo)左鍵";
break;
case "2":
clickType = ClickType.rightClick;
logStr = "點(diǎn)擊了鼠標(biāo)右鍵";
break;
case "3":
clickType = ClickType.doubleClick;
logStr = "雙擊了鼠標(biāo)左鍵";
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);
}實(shí)現(xiàn)效果

到此這篇關(guān)于C#模擬實(shí)現(xiàn)鼠標(biāo)自動(dòng)點(diǎn)擊與消息發(fā)送功能的文章就介紹到這了,更多相關(guān)C#鼠標(biāo)點(diǎn)擊 消息發(fā)送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在網(wǎng)頁中根據(jù)url截圖并輸出到網(wǎng)頁的方法,涉及C#網(wǎng)頁瀏覽器及圖片操作的相關(guān)技巧,需要的朋友可以參考下2016-01-01
WPF中不規(guī)則窗體與WindowsFormsHost控件兼容問題的解決方法
這篇文章主要介紹了WPF中不規(guī)則窗體與WindowsFormsHost控件兼容問題的解決方法,對(duì)比以往的解決方案,給出了一個(gè)具有普遍性的技巧,具有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11
WPF利用TextBlock實(shí)現(xiàn)查找結(jié)果高亮顯示效果
在應(yīng)用開發(fā)過程中,經(jīng)常遇到這樣的需求:通過關(guān)鍵字查找數(shù)據(jù),把帶有關(guān)鍵字的數(shù)據(jù)顯示出來,同時(shí)在結(jié)果中高亮顯示關(guān)鍵字,所以本文就來和大家介紹一下如何利用TextBlock實(shí)現(xiàn)查找結(jié)果高亮顯示效果吧2023-08-08
C#實(shí)現(xiàn)自定義線程池實(shí)例代碼
這篇文章介紹了C#實(shí)現(xiàn)自定義線程池的實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
C#實(shí)現(xiàn)只運(yùn)行單個(gè)實(shí)例應(yīng)用程序的方法(使用VB.Net的IsSingleInstance)
這篇文章主要介紹了C#實(shí)現(xiàn)只運(yùn)行單個(gè)實(shí)例應(yīng)用程序的方法,本文使用的是VB.Net的IsSingleInstance方法實(shí)現(xiàn),優(yōu)于Mutex 和 Process 這兩種只運(yùn)行單個(gè)應(yīng)用程序?qū)嵗姆椒?需要的朋友可以參考下2014-07-07

