着上一篇往下写
第一步、鼠标动作定义代码如下
[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");
}
调试起来看下:
全部代码如下:
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());
}
}
}
}