如何用EnumWindows函数获得窗口句柄
编号:QA004354
建立日期: 2001年8月12日 最后修改日期:2001年8月12日
所属类别:
Li Min:
操作系统: win 2000
编程工具: vc6.0
问题: 在桌面上有若干个窗口,想用EnumWindows函数获得这些窗口句柄。请问该如何使用。
水平: 中级
回答:
下面是来自微软的例子,这个枚举所有的窗口,然后向窗口发送关闭的消息。
#include <windows.h>
BOOL CALLBACK EnumWindowsProc(
HWND hwnd,
DWORD lParam
);
//
// EnumWindowsProc must be called from a Windows
// application on Windows 95.
//
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//
// Close all open applications.
//
EnumWindows(EnumWindowsProc, 0);
// Now do a regular logoff.
ExitWindowsEx(EWX_LOGOFF , 0);
}
BOOL CALLBACK EnumWindowsProc(
HWND hwnd,
DWORD lParam
)
{
DWORD pid = 0;
LRESULT lResult;
HANDLE hProcess;
DWORD dwResult;
lResult = SendMessageTimeout(
hwnd,
WM_QUERYENDSESSION,
0,
ENDSESSION_LOGOFF,
SMTO_ABORTIFHUNG,
2000,
&dwResult);
if( lResult )
{
//
// Application will terminate nicely, so let it.
//
lResult = SendMessageTimeout(
hwnd,
WM_ENDSESSION,
TRUE,
ENDSESSION_LOGOFF,
SMTO_ABORTIFHUNG,
2000,
&dwResult);
}
else // You have to take more forceful measures.
{
//
// Get the ProcessId for this window.
//
GetWindowThreadProcessId( hwnd, &pid );
//
// Open the process with all access.
//
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
//
// Terminate the process.
//
TerminateProcess(hProcess, 0);
}
//
// Continue the enumeration.
//
return TRUE;
}
此问题由李海回答。
| |
|
|
| |
|
|