#include "winuser.h"
#include "stdio.h"
#include "windows.h"
#include "afxmt.h"
#include "winbase.h"
#include <string>
using namespace std;
// 控件ID:聊天窗口“发送”按钮
#define CONTROL_ID_CHAT_BUTTON_SEND 1918
// 控件ID:聊天窗口“关闭”按钮
#define CONTROL_ID_CHAT_BUTTON_CLOSE 1030
//存储qq号码和呢称
typedef struct qq_number qq_number;
struct qq_number
{
char qqNumber[10]; //qq号码
char qqName[10]; //呢称
};
qq_number myQQNumber[50];
HWND h_g; //聊天窗体句柄
HWND h_Edit; //聊天文本框句柄
//枚举进程,根据标题获取聊天窗体
BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam)
{
HWND hChatFrame; //聊天窗体句柄
char str[100]; //存储枚举的窗体标题
//获取聊天窗体的标题
::GetWindowText(hwnd,str,sizeof(str));
LPCTSTR pstr = str;
CString tmpStr = pstr;
//获取聊天窗体的句柄
hChatFrame = FindWindow("#32770",pstr);
//获取输入框的父窗口的父窗口句柄
HWND temp = ::FindWindowEx(hwnd, 0, _T("Tencent_QQToolBar"), _T(""));
//获取输入框的父窗口的句柄
HWND temp1 = ::FindWindowEx(hwnd, temp, _T("AfxWnd42"), _T(""));
//获取输入框的句柄
HWND hwnd_edit = ::FindWindowEx(temp1, 0, _T("RICHEDIT20A"), _T(""));
if ((tmpStr.Find("交谈中") != -1))
{
h_g = hChatFrame; //获取聊天窗体的句柄
h_Edit = hwnd_edit; //获取聊天文本框的句柄
return TRUE;
}
return TRUE;
}
//把好友的号码存储到结构体中
void CQq2008Test_0913Dlg::readTxtFile()
{
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();
//创建文件对话框
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*||"));
if (dlg.DoModal() == IDOK)
{
//获得文件路径
CString strPathName = dlg.GetPathName();
CStdioFile file;
//打开文件
if (!file.Open(strPathName, CFile::modeRead))
{
::AfxMessageBox(_T("文件打开失败。"));
return;
}
//读文件
CString strText = _T(""); //按行存储文本文件中的内容
CString qqNo = _T(""); //qq号码
int i = 0;
while (file.ReadString(strText))
{
//获取QQ号码
strText.TrimLeft();
strText.TrimRight();
pListBox->AddString(strText);
qqNo = strText.Left(strText.FindOneOf("-"));
//存储到结构体
strcpy(myQQNumber[i].qqNumber,qqNo);
i++;
}
//关闭文件
file.Close();
}
}
//在线程中群发消息
void CQq2008Test_0913Dlg::OnButton2()
{
CString strChatMsg;
GetDlgItemText(IDC_EDIT1,strChatMsg);
string strMsg(strChatMsg.GetBuffer(256));
strChatMsg.ReleaseBuffer();
//初始化结构体
memset(&myQQNumber,0,50*sizeof(qq_number));
readTxtFile(); //获取要发送消息的qq号码
//取出结构体中的qq号码
for (int k = 0; k <50; k++)
{
if (myQQNumber[k].qqNumber[0]!='/0')
{
CString tmpNumber = myQQNumber[k].qqNumber; //QQ号码
CString lpURL;
//打开聊天对话框
lpURL.Format("http://wpasig.qq.com/msgrd?v=1&uin=%s&site=microsoft&menu=yes",tmpNumber);
//根据号码打开聊天窗口
ShellExecute(this->m_hWnd, "open", lpURL, NULL, NULL, SW_SHOWNORMAL);
Sleep(1500); //等待聊天对话框的打开
//枚举窗体
EnumWindows(lpEnumFunc,NULL);
//粘贴消息
if (h_Edit != NULL)
{
//打开剪切板
if(!::OpenClipboard(m_hWnd))
{
AfxMessageBox("打开剪切板,出现异常!");
}
//清空剪切板原有数据
if(!EmptyClipboard())
{
AfxMessageBox("清空剪切板,出现异常!");
}
//根据用户输入数据分配内存,并得到一个内存对像句柄
//string strMsg="Hello World !"; //要发送的内容
//string strMsg(strChatMsg.GetBuffer(256));
//strChatMsg.ReleaseBuffer();
HANDLE hClip=GlobalAlloc(GMEM_MOVEABLE,strMsg.length()+1);
//将上面得到的句柄转换为指针
char* pBuf=(char*)GlobalLock(hClip);
//将数据拷贝到内存当中
strcpy(pBuf,strMsg.c_str());
GlobalUnlock(hClip);
//将内存中的数据置于剪切板
SetClipboardData(CF_TEXT,hClip);
//关闭剪切板
CloseClipboard();
::PostMessage(h_Edit,WM_COMMAND,0xE125,0) ;
}
//发送消息
if (h_g != NULL)
{
::PostMessage(h_g,WM_COMMAND,(WPARAM)CONTROL_ID_CHAT_BUTTON_SEND, LPARAM(NULL)) ;
Sleep(500); //0.5秒后关闭聊天对话框
::PostMessage(h_g,WM_COMMAND,(WPARAM)CONTROL_ID_CHAT_BUTTON_CLOSE, LPARAM(NULL)) ;
}
} //if (myQQNumber[k].qqNumber!='/0')
} //for (int k = 0; k <50; k++)
//AfxMessageBox("发送结束");
}
#include "stdio.h"
#include "windows.h"
#include "afxmt.h"
#include "winbase.h"
#include <string>
using namespace std;
// 控件ID:聊天窗口“发送”按钮
#define CONTROL_ID_CHAT_BUTTON_SEND 1918
// 控件ID:聊天窗口“关闭”按钮
#define CONTROL_ID_CHAT_BUTTON_CLOSE 1030
//存储qq号码和呢称
typedef struct qq_number qq_number;
struct qq_number
{
char qqNumber[10]; //qq号码
char qqName[10]; //呢称
};
qq_number myQQNumber[50];
HWND h_g; //聊天窗体句柄
HWND h_Edit; //聊天文本框句柄
//枚举进程,根据标题获取聊天窗体
BOOL CALLBACK lpEnumFunc(HWND hwnd, LPARAM lParam)
{
HWND hChatFrame; //聊天窗体句柄
char str[100]; //存储枚举的窗体标题
//获取聊天窗体的标题
::GetWindowText(hwnd,str,sizeof(str));
LPCTSTR pstr = str;
CString tmpStr = pstr;
//获取聊天窗体的句柄
hChatFrame = FindWindow("#32770",pstr);
//获取输入框的父窗口的父窗口句柄
HWND temp = ::FindWindowEx(hwnd, 0, _T("Tencent_QQToolBar"), _T(""));
//获取输入框的父窗口的句柄
HWND temp1 = ::FindWindowEx(hwnd, temp, _T("AfxWnd42"), _T(""));
//获取输入框的句柄
HWND hwnd_edit = ::FindWindowEx(temp1, 0, _T("RICHEDIT20A"), _T(""));
if ((tmpStr.Find("交谈中") != -1))
{
h_g = hChatFrame; //获取聊天窗体的句柄
h_Edit = hwnd_edit; //获取聊天文本框的句柄
return TRUE;
}
return TRUE;
}
//把好友的号码存储到结构体中
void CQq2008Test_0913Dlg::readTxtFile()
{
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();
//创建文件对话框
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, _T("文本文件(*.*)|*.*||"));
if (dlg.DoModal() == IDOK)
{
//获得文件路径
CString strPathName = dlg.GetPathName();
CStdioFile file;
//打开文件
if (!file.Open(strPathName, CFile::modeRead))
{
::AfxMessageBox(_T("文件打开失败。"));
return;
}
//读文件
CString strText = _T(""); //按行存储文本文件中的内容
CString qqNo = _T(""); //qq号码
int i = 0;
while (file.ReadString(strText))
{
//获取QQ号码
strText.TrimLeft();
strText.TrimRight();
pListBox->AddString(strText);
qqNo = strText.Left(strText.FindOneOf("-"));
//存储到结构体
strcpy(myQQNumber[i].qqNumber,qqNo);
i++;
}
//关闭文件
file.Close();
}
}
//在线程中群发消息
void CQq2008Test_0913Dlg::OnButton2()
{
CString strChatMsg;
GetDlgItemText(IDC_EDIT1,strChatMsg);
string strMsg(strChatMsg.GetBuffer(256));
strChatMsg.ReleaseBuffer();
//初始化结构体
memset(&myQQNumber,0,50*sizeof(qq_number));
readTxtFile(); //获取要发送消息的qq号码
//取出结构体中的qq号码
for (int k = 0; k <50; k++)
{
if (myQQNumber[k].qqNumber[0]!='/0')
{
CString tmpNumber = myQQNumber[k].qqNumber; //QQ号码
CString lpURL;
//打开聊天对话框
lpURL.Format("http://wpasig.qq.com/msgrd?v=1&uin=%s&site=microsoft&menu=yes",tmpNumber);
//根据号码打开聊天窗口
ShellExecute(this->m_hWnd, "open", lpURL, NULL, NULL, SW_SHOWNORMAL);
Sleep(1500); //等待聊天对话框的打开
//枚举窗体
EnumWindows(lpEnumFunc,NULL);
//粘贴消息
if (h_Edit != NULL)
{
//打开剪切板
if(!::OpenClipboard(m_hWnd))
{
AfxMessageBox("打开剪切板,出现异常!");
}
//清空剪切板原有数据
if(!EmptyClipboard())
{
AfxMessageBox("清空剪切板,出现异常!");
}
//根据用户输入数据分配内存,并得到一个内存对像句柄
//string strMsg="Hello World !"; //要发送的内容
//string strMsg(strChatMsg.GetBuffer(256));
//strChatMsg.ReleaseBuffer();
HANDLE hClip=GlobalAlloc(GMEM_MOVEABLE,strMsg.length()+1);
//将上面得到的句柄转换为指针
char* pBuf=(char*)GlobalLock(hClip);
//将数据拷贝到内存当中
strcpy(pBuf,strMsg.c_str());
GlobalUnlock(hClip);
//将内存中的数据置于剪切板
SetClipboardData(CF_TEXT,hClip);
//关闭剪切板
CloseClipboard();
::PostMessage(h_Edit,WM_COMMAND,0xE125,0) ;
}
//发送消息
if (h_g != NULL)
{
::PostMessage(h_g,WM_COMMAND,(WPARAM)CONTROL_ID_CHAT_BUTTON_SEND, LPARAM(NULL)) ;
Sleep(500); //0.5秒后关闭聊天对话框
::PostMessage(h_g,WM_COMMAND,(WPARAM)CONTROL_ID_CHAT_BUTTON_CLOSE, LPARAM(NULL)) ;
}
} //if (myQQNumber[k].qqNumber!='/0')
} //for (int k = 0; k <50; k++)
//AfxMessageBox("发送结束");
}
文本文件内容:
123456-aaa
456789-bbb