前些时在网上看了可以自动登陆QQ的程序后,自己也写了个,纯粹是为了熟悉一下这些知识。
代码如下:
// MyToolDlg.h : header file
//#if !defined(AFX_MYTOOLDLG_H__DE821896_C3DB_4441_8D10_5E707B35EA37__INCLUDED_)
#define AFX_MYTOOLDLG_H__DE821896_C3DB_4441_8D10_5E707B35EA37__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
// CMyToolDlg dialog
#include \"vector\"
using namespace std;
struct LogIn
{
int index; //索引
char *QQUser; //QQ号码
char *QQPass; //QQ密码
char *QQName; //QQ昵称
};class CMyToolDlg : public CDialog
{
// Construction
public:
HWND MyFindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName,HANDLE hProcess);
BOOL CustomFindFile();
void PushLogInButton(CWnd* pWnd,UINT ButtonID);
void InputQQPassword(CWnd* pWnd,LogIn li);
void InputQQNumber(CWnd* pWnd,LogIn li);
void InitUserInfo();
BOOL MyCreateProcess(LPPROCESS_INFORMATION lpPrcInfo);
CMyToolDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
//{{AFX_DATA(CMyToolDlg)
enum { IDD = IDD_MYTOOL_DIALOG };
int m_nIndex;
//}}AFX_DATA// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyToolDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL// Implementation
protected:
HICON m_hIcon;
CString m_szFile;
vector<LogIn> m_vecLi;CBrush m_brush;
CBitmap m_bmp;LPSTR m_lpWarnStr;
LPSTR m_lpMsgBoxName;PROCESS_INFORMATION m_prcInfo;
// Generated message map functions
//{{AFX_MSG(CMyToolDlg)
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnQq();
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
afx_msg void OnQqgame();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYTOOLDLG_H__DE821896_C3DB_4441_8D10_5E707B35EA37__INCLUDED_)
// MyToolDlg.cpp : implementation file
//#include \"stdafx.h\"
#include \"MyTool.h\"
#include \"MyToolDlg.h\"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMyToolDlg dialogCMyToolDlg::CMyToolDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyToolDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyToolDlg)
m_nIndex = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// m_brush.CreateSolidBrush(RGB(255,255,255));
m_bmp.LoadBitmap(IDB_BITMAP);
m_brush.CreatePatternBrush(&m_bmp);
m_lpMsgBoxName=\"指定文件路径\";
}
void CMyToolDlg::InitUserInfo()
{
LogIn LI[3]=
{
0, \"123\", \"123\", \"拾樓桦^@^oо\",
1, \"123\", \"123\", \"?(りベ動了\",
2, \"123\", \"123\", \"C++之旅\"
};
CComboBox*pComb=(CComboBox*)GetDlgItem(IDC_CCOM_QQNO);
ASSERT(pComb);pComb->DeleteString(0);
int num=sizeof(LI)/sizeof(LogIn);
for (int i=0;i<num;i++)
{
m_vecLi.push_back(LI[i]);
pComb->AddString(LI[i].QQName);
}
}
void CMyToolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyToolDlg)
DDX_CBIndex(pDX, IDC_CCOM_QQNO, m_nIndex);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CMyToolDlg, CDialog)
//{{AFX_MSG_MAP(CMyToolDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_QQ, OnQq)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(ID_QQGAME, OnQqgame)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyToolDlg message handlersBOOL CMyToolDlg::OnInitDialog()
{
CDialog::OnInitDialog();// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
InitUserInfo();
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CMyToolDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyToolDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
HBRUSH CMyToolDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC herepDC->SetBkMode(TRANSPARENT);
hbr=m_brush;
// TODO: Return a different brush if the default is not desired
return hbr;
}
BOOL CMyToolDlg::MyCreateProcess(LPPROCESS_INFORMATION lpPrcInfo)
{
//--Method one--//STARTUPINFO si;
memset(lpPrcInfo,sizeof(PROCESS_INFORMATION),0);
memset(&si, sizeof(si),0);
si.cb = sizeof(si);
si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = NULL;
si.cbReserved2=0;
si.lpReserved2 = NULL;
si.dwFlags = STARTF_FORCEOFFFEEDBACK;
si.dwX = 100;
si.dwY = 100;
si.dwXSize = 320;
si.dwYSize = 240;
return CreateProcess(m_szFile,NULL,NULL, NULL, NULL,0,NULL,NULL,&si,lpPrcInfo);//--Method two--//
/* CString path=\"C:\\Program Files\\Tencent\\QQ\\\";
ShellExecute(NULL,\"open\",_T(\"QQ.exe\"), NULL,path,SW_SHOW);*/
}
void CMyToolDlg::InputQQNumber(CWnd *pWnd,LogIn li)
{
ASSERT(pWnd);
CComboBox *pCob = (CComboBox *)pWnd->GetDlgItem(138);
pCob->SetEditSel(0, -1);
pCob->Clear();
int userLen=strlen(li.QQUser);
char cNo;
for(int i=0;i<userLen;i++)
{
cNo=li.QQUser[i];
pCob->SendMessage(WM_CHAR, (WPARAM)cNo, 0);
}
}void CMyToolDlg::InputQQPassword(CWnd *pWnd,LogIn li)
{
ASSERT(pWnd);
CWnd *pNextWnd = pWnd->GetNextWindow(GW_CHILD);
pNextWnd = pNextWnd->GetNextWindow(GW_HWNDNEXT);
for(;pNextWnd!=NULL;pNextWnd=pNextWnd->GetNextWindow(GW_HWNDNEXT))
{
if (pNextWnd)
{
CEdit* pPwd=(CEdit*)pNextWnd->GetDlgItem(16038);
if (pPwd)
{
pPwd->SetSel(0, -1);
pPwd->Clear();
int passLen=strlen(li.QQPass);
char cKey;
for(int i=0;i<passLen;i++)
{
cKey=li.QQPass[i];
pPwd->SendMessage(WM_CHAR, (WPARAM)cKey,0);
}
break;
}
}
}
}void CMyToolDlg::PushLogInButton(CWnd *pWnd,UINT ButtonID)
{
ASSERT(pWnd);
CButton *pBtnLogin = (CButton *)pWnd->GetDlgItem(ButtonID);
if (pBtnLogin)
{
pBtnLogin->PostMessage(WM_LBUTTONDOWN, 0, 0);
pBtnLogin->PostMessage(WM_LBUTTONUP, 0, 0);
}
}void CMyToolDlg::OnQq()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CWnd*pWnd=NULL;
m_szFile=CString(\"C:\\Program Files\\Tencent\\QQ\\CoralQQ.exe\");
m_lpWarnStr=\"没有找到该执行文件!\n要自己指定可执行文件路径请按 ‘是’,退出请按 ‘否’\";
HWND hwnd=::FindWindow(_T(\"#32770\"),_T(\"QQ用户登录\"));
while(!hwnd)
{
BOOL found=FALSE;
while(!found)
{
if(found=MyCreateProcess(&m_prcInfo))
break;
if(!CustomFindFile())
goto loop; //结束并退出程序
}
hwnd=MyFindWindow(_T(\"#32770\"),_T(\"QQ用户登录\"),m_prcInfo.hProcess);
if (hwnd)
ShowWindow(SW_HIDE);
}
pWnd=CWnd::FromHandle(hwnd);
// HIDE QQ_LOGIN FORM
pWnd->ShowWindow(SW_HIDE);
pWnd->MoveWindow(CRect(0,0,0,0));
ASSERT(pWnd);
InputQQNumber(pWnd,m_vecLi[m_nIndex]);
InputQQPassword(pWnd,m_vecLi[m_nIndex]);
PushLogInButton(pWnd,16032);
hwnd=NULL;
while(1)
{
static int i=0;
hwnd=::FindWindow(_T(\"#32770\"),_T(\"请选择上网环境\"));
if(hwnd)
{
pWnd=CWnd::FromHandle(hwnd);
pWnd->MoveWindow(CRect(0,0,0,0));
PushLogInButton(pWnd,1);
break;
}
if(i==100)break;
Sleep(1);
i++;
}
loop:
OnOK();
}void CMyToolDlg::OnQqgame()
{
// TODO: Add your control notification handler code here
}BOOL CMyToolDlg::CustomFindFile()
{
if(IDNO==MessageBox(m_lpWarnStr,m_lpMsgBoxName,MB_YESNO))
return FALSE;
char *szFile=\"可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||\";
CFileDialog fileDlg(TRUE,szFile,\"QQ.exe\",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFile,this);if (IDCANCEL==fileDlg.DoModal())
return FALSE;m_szFile=fileDlg.GetPathName();
return TRUE;
}HWND CMyToolDlg::MyFindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName,HANDLE hProcess)
{
HWND hwnd=NULL;
for(int i=0;!hwnd;i++)
{
Sleep(100);
hwnd=::FindWindow(lpClassName/*_T(\"#32770\"),_T(\"QQ用户登录\")*/,lpWindowName);
if(i==10)
{
m_lpWarnStr=\"指定执行的文件错误,请查询后重新输入新的路径!\n点 ‘是’重新指定路径,点‘否’退出!\";
m_szFile=\"\";
TerminateProcess(hProcess,0);
return 0;
}
}
return hwnd;
}
[此贴子已经被作者于2006-10-14 19:15:17编辑过]