C 實現QQ自動化——訊息模擬傳送(2)
接著上一篇往下寫
第一步、滑鼠動作定義程式碼如下
[DllImport(“User32”)]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[Flags]
enum MouseEventFlag : uint //設定滑鼠動作的鍵值
{
Move = 0x0001, //發生移動
LeftDown = 0x0002, //滑鼠按下左鍵
LeftUp = 0x0004, //滑鼠鬆開左鍵
RightDown = 0x0008, //滑鼠按下右鍵
RightUp = 0x0010, //滑鼠鬆開右鍵
MiddleDown = 0x0020, //滑鼠按下中鍵
MiddleUp = 0x0040, //滑鼠鬆開中鍵
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800, //滑鼠輪被移動
VirtualDesk = 0x4000, //虛擬桌面
Absolute = 0x8000
}
第二步、定義模擬訊息傳送的方法 ,
void SendMessage(string message, string qqid)
{
try
{
//滑鼠移動到搜尋框
SetCursorPos(Convert。ToInt32(txtX。Text), Convert。ToInt32(txtY。Text));
Thread。Sleep(200);
//滑鼠模擬點選
mouse_event(MouseEventFlag。LeftDown, 0, 0, 0, UIntPtr。Zero);
mouse_event(MouseEventFlag。LeftUp, 0, 0, 0, UIntPtr。Zero);
Thread。Sleep(200);
//鍵盤模擬輸入QQ號碼
SendKeys。SendWait(qqid);
Thread。Sleep(3000);
//鍵盤模擬回車
SendKeys。SendWait(“{ENTER}”);
Thread。Sleep(500);
//鍵盤模擬打字
SendKeys。SendWait(message);
Thread。Sleep(500);
//鍵盤模擬Ctrl+Enter傳送訊息(我QQ傳送訊息的快捷鍵設定是Ctrl+Enter)
SendKeys。SendWait(“^{ENTER}”);
Thread。Sleep(3000);
//鍵盤{Esc模擬關鍵當前視窗
SendKeys。SendWait(“{Esc}”);
}
catch (Exception ex)
{
MessageBox。Show(ex。ToString());
}
}
第三步、把按鈕點選事件改成呼叫訊息傳送的方法,程式碼如下:
private void btnSend_Click(object sender, EventArgs e)
{
SendMessage(“我愛你中國”, “1257473307”);
}
除錯起來看下:
00:32
全部程式碼如下:
using System;
using System。Collections。Generic;
using System。ComponentModel;
using System。Data;
using System。Drawing;
using System。Linq;
using System。Runtime。InteropServices;
using System。Text;
using System。Threading;
using System。Threading。Tasks;
using System。Windows。Forms;
namespace Automate。QQ
{
public partial class Form1 : Form
{
[DllImport(“user32。dll”)]
private static extern int SetCursorPos(int x, int y);
[DllImport(“User32”)]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[Flags]
enum MouseEventFlag : uint //設定滑鼠動作的鍵值
{
Move = 0x0001, //發生移動
LeftDown = 0x0002, //滑鼠按下左鍵
LeftUp = 0x0004, //滑鼠鬆開左鍵
RightDown = 0x0008, //滑鼠按下右鍵
RightUp = 0x0010, //滑鼠鬆開右鍵
MiddleDown = 0x0020, //滑鼠按下中鍵
MiddleUp = 0x0040, //滑鼠鬆開中鍵
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800, //滑鼠輪被移動
VirtualDesk = 0x4000, //虛擬桌面
Absolute = 0x8000
}
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
this。lblPosition。Text = $“x:{Cursor。Position。X},y:{Cursor。Position。Y}”;
}
private void btnSend_Click(object sender, EventArgs e)
{
SendMessage(“我愛你中國”, “1257473307”);
}
private void Form1_Load(object sender, EventArgs e)
{
this。timer1。Enabled = true;
this。timer1。Interval = 10;//timer控制元件的執行頻率
}
void SendMessage(string message, string qqid)
{
try
{
//滑鼠移動到搜尋框f
SetCursorPos(Convert。ToInt32(txtX。Text), Convert。ToInt32(txtY。Text));
Thread。Sleep(200);
//滑鼠模擬點選
mouse_event(MouseEventFlag。LeftDown, 0, 0, 0, UIntPtr。Zero);
mouse_event(MouseEventFlag。LeftUp, 0, 0, 0, UIntPtr。Zero);
Thread。Sleep(200);
//鍵盤模擬輸入QQ號碼
SendKeys。SendWait(qqid);
Thread。Sleep(3000);
//鍵盤模擬回車
SendKeys。SendWait(“{ENTER}”);
Thread。Sleep(500);
//鍵盤模擬打字
SendKeys。SendWait(message);
Thread。Sleep(500);
//鍵盤模擬Ctrl+Enter傳送訊息(我QQ傳送訊息的快捷鍵設定是Ctrl+Enter)
SendKeys。SendWait(“^{ENTER}”);
Thread。Sleep(3000);
//鍵盤{Esc模擬關鍵當前視窗
SendKeys。SendWait(“{Esc}”);
}
catch (Exception ex)
{
MessageBox。Show(ex。ToString());
}
}
}
}