大漠插件就不过多介绍了,不知道的请查下百度。主要是讲解C#怎么调用大漠插件。
大漠插件提供了COM版本,C#直接点击引用,添加即可。然后注册下大漠插件到系统文件夹,注册代码如下:
static string AutoRegCom(string strCmd)
{
string rInfo;
try
{
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("cmd.exe");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.CreateNoWindow = true;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcessStartInfo.Arguments = "/c " + strCmd;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
rInfo = myStreamReader.ReadToEnd();
myProcess.Close();
rInfo = strCmd + "\r\n" + rInfo;
return rInfo;
}
catch (Exception ex)
{
return ex.Message;
}
}
调用方法:
//注册大漠
var regResult = AutoRegCom("regsvr32 -s dm.dll");
实例化大漠模块:
private static dmsoft _dmsoft = new dmsoft();
查找微信和QQ的聊天窗口,根据窗口类名来进行模糊查找,然后得到窗口句柄,根据窗口句柄得到窗口的标题、类名信息(QQ聊天窗口的类名是:TXGuiFoundation,微信的聊天窗口类名是:ChatWnd):
var list = new List<int>();
list.AddRange(_dmsoft.EnumWindow(, "", "TXGuiFoundation", ).Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => Convert.ToInt32(x)));
list.AddRange(_dmsoft.EnumWindow(, "", "ChatWnd", ).Split(',').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => Convert.ToInt32(x)));
windInfoBindingSource.DataSource = list.Select(x => new WindInfo()
{
Handle = x,
ClassName = _dmsoft.GetWindowClass(x),
Title = _dmsoft.GetWindowTitle(x),
}).ToList();
拿到窗口句柄剩下的事情就好办了。往窗口里面投递文本消息->模拟鼠标按键Enter发送消息。
foreach (var windInfo in w.Where(x => x.Enable))
{
_dmsoft.BindWindowEx(windInfo.Handle, "gdi", "windows", "windows", "", );
Thread.Sleep();
_dmsoft.SetWindowState(windInfo.Handle, );
_dmsoft.SetWindowState(windInfo.Handle, );
Thread.Sleep();
_dmsoft.SendString2(windInfo.Handle, content);
Thread.Sleep();
_dmsoft.KeyDown();
_dmsoft.KeyUp();
_dmsoft.SetWindowState(windInfo.Handle, );
_dmsoft.SetWindowState(windInfo.Handle, );
_dmsoft.UnBindWindow();
}
1、通过 BindWindowEx 方法绑定窗口
2、通过 SetWindowState 设置窗口为正常模拟并且设置窗口为激活状态(为了获取到输入焦点)
3、SendString发送文本到窗口,因为目前窗口已经获取到输入焦点,所以直接投递文本即可
4、模拟按键 Enter ,Enter键在大漠模块里面的虚拟键代码是13,模拟进行按下和弹起的操作,因为大漠模块里面默认设置了按下和弹起操作的间隔时间,所以这里我们不需要再次设置。
5、通过 SetWindowState 设置窗口为取消激活状态,并且设置窗口最小化
6、解绑窗口