C高手请进 ——枚举QQ好友列表
#include <windows.h> #include <stdio.h>
#include<iostream.h>
BOOL CALLBACK EnumChildProc(HWND,LPARAM);//回调函数原型
int num;
static HWND hwnd_find;//用于记录父窗口
HDC hdcScr, hdcMem ;
int main()
{
HFONT hFont;
LOGFONT lfont;
lfont.lfHeight = 450;
lfont.lfWidth = 300;
lfont.lfEscapement = 0;
lfont.lfOrientation = 0;
lfont.lfWeight =1000 ;
lfont.lfItalic = FALSE;
lfont.lfUnderline = 0;
lfont.lfStrikeOut = 0;
lfont.lfCharSet = DEFAULT_CHARSET;
lfont.lfOutPrecision= OUT_DEFAULT_PRECIS;
lfont.lfQuality = DEFAULT_QUALITY;
lfont.lfPitchAndFamily= FF_DECORATIVE;
::SetBkColor (hdcScr,RGB(0,0,255) );
::SetTextColor (hdcScr,RGB(0,255,0));
hFont = CreateFontIndirect(&lfont);
SelectObject (hdcScr,hFont) ;
hwnd_find=FindWindow(NULL,"QQ2011");//寻找游戏窗口
if(!hwnd_find)
MessageBox(hwnd_find,"未发现要找的窗口,请确认","Error",MB_OK);//如果没有找到出现没有找到的提示信息
else
{
cout<<"找到窗口:"<<endl;
char tile[50];
GetWindowText(hwnd_find,tile,MAX_PATH);
cout<<"标题为:"<<tile<<endl;
hdcScr = GetDCEx (hwnd_find, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdcScr) ;
for(int t=0;t<10;t++)
{
::TextOut(hdcScr,0,0,"窗口",4) ;
Sleep(500);
}
EnumChildWindows( hwnd_find,(WNDENUMPROC)EnumChildProc,(LPARAM)NULL);
}
//此处的上一句是重点 (WNDENUMPROC)EnumChildProc可以理解成是一个函数地址,只要还有子窗口
//它就会调用这个函数,也就是下面的回调函数
cout<<"endl";
int io;
cin>>io;
DeleteDC (hdcMem) ;
return 0;
}
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{ cout<<"进入枚举:"<<endl;
if (hwnd!=NULL) //如果子窗口存在
{
cout<<"子窗口存在: "<<endl;
num++;//记录窗口数目的num自增
char chText[MAX_PATH];//用来储存窗口的Text
ZeroMemory(chText,MAX_PATH); // ZeroMemory宏用0来填充一块内存区域
GetWindowText(hwnd,chText,MAX_PATH);//获取窗口的标题
cout<<"标题:"<<chText<<endl;
if(strlen(chText)!=0)//如果窗口标题非空的话
{
// MessageBox(NULL,chText,"找到子窗口",MB_OK);//提示信息
sprintf(chText,"找到标题为: %s 的窗口一个,共计%d个窗口",chText,num);
hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdcScr) ;
for(int t=0;t<10;t++)
{
::TextOut(hdcScr,0,0,"窗口",4) ;
Sleep(500);
}
//格式化chText
}
else
{
sprintf(chText,"找到标题为空的窗口一个,共计%d个窗口",num);
// MessageBox(NULL,chText,"找到子窗口",MB_OK);//提示信息
hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdcScr) ;
for(int t=0;t<10;t++)
{
::TextOut(hdcScr,0,0,"窗口",4) ;
Sleep(500);
}
ReleaseDC (hwnd, hdcScr) ;
return TRUE;
}
cout<<"子窗口已完成枚举"<<endl;
return FALSE;
}
}
//我实现不了,请高手指点