在《QQ群发软件开发一》中我们建立了QQ帐号与QQ主程序对应关系。现在我们要实现自动发送功能必须解决以下几个问题:
1、通过这个主程序句柄调出对话框 。
2 、取得QQ对话框句柄,对其进行操作。
先说第一部份取话框, 取对话框分为取QQ好友对话框与QQ群对话框以及群里的陌生人的对话框这三种情况 。
取QQ好友的对话框
// trytimes尝试次数,lastfirend上次取到的好友呢称,friendName这次取到的呢称。
public bool GetNextFriend(int trytimes, string lastfirend, out string friendName)
{
friendName = "";
try
{
if (this.IsActive())
{
for (int timeindex = 0; timeindex < trytimes; timeindex++)
{
WinAPI.ShowWindow(this._QQHandleID.handle, WinConst.SW_SHOW);
Thread.Sleep(new System.Random().Next(8) + 100);
WinAPI.BringWindowToTop(this._QQHandleID.handle);
//Thread.Sleep(new System.Random().Next(8) + 300);
Rect winrect = new Rect();
WinAPI.GetWindowRect(this._QQHandleID.handle, out winrect);
ushort xpos = ushort.Parse(((winrect.Right - winrect.Left) - 10).ToString());
ushort ypos = ushort.Parse(((winrect.Bottom - winrect.Top) - 80).ToString());
//点击滚动条,使用程序取到焦点
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_LBUTTONDOWN, 0, this.MAKELONG(xpos, ypos));
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_LBUTTONUP, 0, this.MAKELONG(xpos, ypos))
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYDOWN, 40, 0);
Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYUP, 40, 0);
Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYDOWN, 13, 0);
Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYUP, 13, 0);
//取到对话框列表
List<winHandle> chatboxlist = this.GetChatInputBoxs();
if (chatboxlist.Count == 0)
{
Thread.Sleep(500 + new System.Random().Next(500));
chatboxlist = this.GetChatInputBoxs();
if (chatboxlist.Count == 0)
{
if (timeindex == 4)
BaseLib.Encry.Writelog(System.DateTime.Now.ToString("yyyyMMddhhmmss") + " ERROE HAVE NOT GET CHATBOX");
continue;
}
}
foreach (winHandle chatbox in chatboxlist)
{
if (chatbox.WinText != this.lastfriendname)
{
friendName = chatbox.WinText;
break;
}
}
// 关闭除了 friendName 以外的其他对话框
CloseAllChatBox(friendName);
if (friendName == lastfriendname)
{
continue;
}
if (friendName == "")
{
continue;
}
lastfriendname = friendName;
return true;
}
}
return false;
}
catch
{
return false;
}
}
取QQ群对话框
public bool GetNextGroup(int trytimes, string lastgroup, out string groupname)
{
groupname = "";
if (this.IsActive())
{
for (int timeindex = 0; timeindex < trytimes; timeindex++)
{
bool ISwantDownKey = false;
WinAPI.ShowWindow(this._QQHandleID.handle, WinConst.SW_SHOW);
Thread.Sleep(new System.Random().Next(8) + 100);
WinAPI.BringWindowToTop(this._QQHandleID.handle);
this.posy = this.posy + 5;
Rect winrect = new Rect();
WinAPI.GetWindowRect(this._QQHandleID.handle, out winrect);
if ((winrect.Bottom - winrect.Top - bottomeHight) < this.posy)
{
this.posy = winrect.Bottom - winrect.Top - bottomeHight;
ISwantDownKey = true;
}
this.posx = (winrect.Right - winrect.Left) / 2;
if (ISwantDownKey)
{
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYDOWN, 40, 0);
Thread.Sleep(new System.Random().Next(10) + 10);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYUP, 40, 0);
}
Thread.Sleep(new System.Random().Next(8) + 30);
//找到QQ群位置
ushort xpos = ushort.Parse(this.posx.ToString());
ushort ypos = ushort.Parse(this.posy.ToString());
//找到QQ群位置,在QQ群的位置点击(选定对应的群)
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_LBUTTONDOWN, 0, this.MAKELONG(xpos, ypos));
//Thread.Sleep(new System.Random().Next(8) + 25);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_LBUTTONUP, 0, this.MAKELONG(xpos, ypos));
// 在调出之前可以看下句柄的个数(calssname = CON_QQ_MENUTXT);
// 选定之后按下ENTER, 调出QQ群面板
Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYDOWN, 13, 0);
//Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYUP, 13, 0);
//等QQ输入面板弹出
Thread.Sleep(100 + new System.Random().Next(50));
List<winHandle> chatboxlist = this.GetChatInputBoxs();
winHandle chatinputbox = null;
if (chatboxlist.Count == 0)
{
Thread.Sleep(500);
chatboxlist = this.GetChatInputBoxs();
if (chatboxlist.Count == 0)
continue;
}
//这边查看是否有新窗体打开了
foreach (winHandle chatbox in chatboxlist)
{
if (chatbox.WinText != this.lastgroupname)
{
groupname = chatbox.WinText;
chatinputbox = chatbox;
break;
}
}
CloseAllChatBox(groupname);
if (groupname == this.lastgroupname)
{
if (!ISwantDownKey)
{
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYDOWN, 40, 0);
Thread.Sleep(new System.Random().Next(10) + 10);
WinAPI.SendMessage(this._QQHandleID.handle, WinConst.WM_KEYUP, 40, 0);
}
groupname = "";
continue;
}
if (groupname == "")
{
continue;
}
this.posy = this.posy + this.posyindex;
this.lastgroupname = groupname;
this._groupsendtime = 0;
return true;
}
}
return false;
}
取QQ群成员对话框(先取得QQ群对话框,再在QQ群对话框内取得QQ成员对话框)
// trytimes尝试次数,groupname当前打开的群对话框的名称,lastfriend上次取到群成员名称,这次取的名称,isGroupOpened
群对话框是否打开了
public bool GetNextGroupFriend(int trytimes, string groupname, string lastfriend, out string friendname, out bool isGroupOpened)
{
friendname = "";
isGroupopened = true;
try
{
for (int i = 0; i < trytimes; i++)
{
if (CloseAllChatBox(groupname))
{
//取当前所有的对话框与groupname进行比对
List<winHandle> chatlist = this.GetChatInputBoxs();
winHandle groupchatinput = null;
if (chatlist.Count > 0)
{
foreach (winHandle chat in chatlist)
{
if (chat.WinText == groupname)
{
isGroupopened = true;
groupchatinput = chat;
break;
}
}
if (!isGroupopened)
{
continue;
}
}
else
{
isGroupopened = false;
continue;
}
WinAPI.BringWindowToTop(groupchatinput.handle);
this._ChatHandleID = groupchatinput;
WinAPI.SendMessage(groupchatinput.handle, WinConst.WM_SETFOCUS, 0, 0);
Rect winrect = new Rect();
WinAPI.GetWindowRect(groupchatinput.handle, out winrect);
ushort posx = ushort.Parse((winrect.Right - winrect.Left - 60).ToString());
ushort posy = ushort.Parse((winrect.Bottom - winrect.Top - 20).ToString());
WinAPI.SendMessage(groupchatinput.handle, WinConst.WM_LBUTTONDOWN, 0, this.MAKELONG(posx, posy));
Thread.Sleep(new System.Random().Next(8) + 15);
WinAPI.SendMessage(groupchatinput.handle, WinConst.WM_LBUTTONUP, 0, this.MAKELONG(posx, posy));
Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(groupchatinput.handle, WinConst.WM_KEYDOWN, 40, 0);
Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(groupchatinput.handle, WinConst.WM_KEYUP, 40, 0);
Thread.Sleep(20 + new System.Random().Next(20));
WinAPI.SendMessage(groupchatinput.handle, WinConst.WM_KEYDOWN, 13, 0);
Thread.Sleep(new System.Random().Next(10) + 20);
WinAPI.SendMessage(groupchatinput.handle, WinConst.WM_KEYUP, 13, 0);
Thread.Sleep(500);
List<winHandle> chatboxlist = this.GetChatInputBoxs();
if (chatboxlist.Count < 2)
{
Thread.Sleep(500);
chatboxlist = this.GetChatInputBoxs();
if (chatboxlist.Count < 2)
{
isGroupopened = false;
continue;
}
}
winHandle groupfriendchat = null;
foreach (winHandle chat in chatboxlist)
{
if (chat.handle != groupchatinput.handle)
{
groupfriendchat = chat;
this._ChatHandleID = chat;
break;
}
}
if (groupfriendchat != null)
{
friendname = groupfriendchat.WinText;
}
else
{
continue;
}
//CloseAllChatBox(groupname);
if (friendname == this.lastfriendname)
{
friendname = "";
continue;
}
this.lastfriendname = friendname;
isGroupopened = true;
_IsNeedGetNextGroup = false;
return true;
}
}
this._IsNeedGetNextGroup = true;
return false;
}
catch
{
return false;
}
}
以上三段是分别是取QQ好友对话框、取QQ群对话框、取QQ群成员对话框的代码。在后面的部分《QQ群发软件开发三》中我还会与大家分享在取到这些对话框之后,怎么样进行输入内容并发送的操作。对以上代码有兴趣的同学也可以私信我一起讨论。