随笔 - 173  文章 - 1  评论 - 26  阅读 - 43万

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 on   HorseShoe2016  阅读(28361)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示