The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.
这是MSDN上关于EnumWindows函数的解释,意思是枚举所有处于顶层的窗口,但是什么是顶层窗口我并不是十分了解,因为从程序运行的结果我看不出所谓顶层窗口有什么共性,需要再研究研究,程序代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
#include <windows.h>
char om[262144];
BOOL CALLBACK EnumWindowProc(HWND hWnd,LPARAM lParam);
BOOL CALLBACK EnumChildWindowProc(HWND hWnd,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
HWND nphWnd=::FindWindow("notepad",NULL);
if(nphWnd)
{
::EnumWindows(EnumWindowProc,0);//枚举窗口
::EnumChildWindows(nphWnd,EnumChildWindowProc,0);
}
return 0;
}
BOOL CALLBACK EnumWindowProc(HWND hWnd,LPARAM lParam)//枚举窗口的回调函数
{
char temp1[256];
char WinClassName[256];
::GetClassName(hWnd,WinClassName,255);//获得类名
DWORD pID=0;
::GetWindowThreadProcessId(hWnd,(LPDWORD)&pID);//获得进程ID
::GetWindowText(hWnd,temp1,255);//获得窗口标题
::wsprintf(om,"%s%s%s%s%s%s%d",om,"\r\nclassname: ",WinClassName,",wintext: ",temp1,",ID:",pID);
return true;
}
BOOL CALLBACK EnumChildWindowProc(HWND hWnd,LPARAM lParam)
{
char temp1[256];
if(hWnd)
{
::GetClassName(hWnd,temp1,255);
if(!::strcmp(temp1,"Edit"))
{
::SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)om);
return 0;
}
}
return true;
}