枚举窗口及其应用
示例代码下载:枚举窗口及其应用(请不要直接使用迅雷下载)
1.下面请看几种枚举窗口的方式
一:利用GetWindow(ParentWnd, GW_CHILD);
void FindAllChildWnd(HWND ParentWnd) { HWND hChild = ::GetWindow(ParentWnd, GW_CHILD); for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT)) { TCHAR WindowText[30]={0}; ::SendMessage(hChild,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText); TCHAR ClassName[30]={0}; ::GetClassName(hChild,ClassName,sizeof(ClassName)/sizeof(TCHAR)); trace2(WindowText,ClassName); FindAllChildWnd(hChild); } } void CDemoDlg::OnButton1() { FindAllChildWnd(::GetForegroundWindow()); }
二:利用回调函数EnumChildProc枚举所有子窗口
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam) { TCHAR WindowText[100]={0}; ::SendMessage(hwnd,WM_GETTEXT,(WPARAM)(sizeof(WindowText)/sizeof(TCHAR)),(LPARAM)WindowText); TCHAR ClassName[100]={0}; ::GetClassName(hwnd,ClassName,sizeof(ClassName)/sizeof(TCHAR)); trace2(WindowText,ClassName); return TRUE ; } void CDemoDlg::OnButton2() { ::EnumChildWindows(::GetForegroundWindow(),EnumChildProc,NULL); }
三:利用回调函数EnumChildProc枚举所有可见的桌面窗口
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM