Windows API 编程----EnumWindows()函数的用法

1.

函数原型:

BOOL WINAPI EnumWindows(
_In_ WNDENUMPROC lpEnumFunc,
_In_ LPARAM lParam
);

lpEnumFunc: 应用程序定义的回调函数的指针

lParam:         传递给回调函数的应用程序定义的值

MSDN中对EnumWindows的解释:

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.

即:

枚举屏幕上的所有的顶层窗口,轮流地将这些窗口的句柄传递给一个应用程序定义的回调函数。EnumWindows会一直进行下去,直到枚举完所有的顶层窗口,或者回调函数返回了FALSE.

 

2.

EnumWindowsProc()函数的定义:

BOOL CALLBACK EnumWindowsProc(
_In_ HWND hwnd,
_In_ LPARAM lParam
);

hwnd: 在枚举每个顶层窗口而调用该函数的过程中传递给该函数的顶层窗口的句柄

lParam: 即EnumWindows()函数的第二个参数。

Return Value:

返回TRUE,则EnumWindows()函数在系统中继续调用EnumWindowsProc()函数;返回FALSE,则停止枚举。

MSDN解释:

An application-defined callback function used with the EnumWindows or EnumDesktopWindows function. It receives top-level window handles. The WNDENUMPROC type defines a pointer to this callback function. EnumWindowsProc is a placeholder for the application-defined function name

即:

应用程序定义的函数,用于EnumWindows()或EnumDesktopWindos()函数。接收顶层窗口句柄。WNDENUMPROC类型定义了指向这种回调函数的指针。EnumWindowsProc是一个应用程序定义的函数名称的占位符。

 

应用举例:

//这段代码的功能就是枚举当前所有的顶层窗口句柄,并且指定了一个窗口句柄,如果枚举到的顶层窗口句柄和传递给EnumWindowsProc函数的窗口句柄不同,则使枚举到的顶层窗口失能

//
自定义结构体 typedef struct _DLONG { LONG wParam; LONG lParam; }stLONG; //回调函数的定义 BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) { stLONG *pDlong = (stLONG*)lParam; BOOL bEnable = (BOOL)pDlong->lParam; if (hwnd != (HWND)pDlong->wParam)   EnableWindow(hwnd, bEnable);//如果当前所枚举到的顶层窗口句柄和主调函数(EnumWindos函数)所传递来的额外信息做指定的顶层窗口句柄不同,则进行相关使能或使失能操作 return TRUE; } ...... stLONG dlong; //传递给EnumWindowsProc的额外信息 dlong.lParam = (LPARAM)FALSE; dlong.wParam = (WPARAM)hWnd; EnumWindows(EnumWindowsProc, (LONG)&dlong);

 

posted @ 2016-06-08 18:32  HorseShoe2016  阅读(26588)  评论(0编辑  收藏  举报