此问题的后续部分:Alt Tab overlay Win32 identificator

我尝试使用Winuser API中的SetWinEventHook函数捕捉alt-tab开关菜单打开(并退出)的时刻。 但是,该挂钩不会捕获任何事件(例如,最小化窗口),因此不会调用HandleWinEvent

以下代码受到MSDN page上提供的代码的极大启发

#ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500
#endif

#ifndef WINVER
    #define WINVER 0x0501
#endif

#include "conio.h"
#include <windows.h>
#include <iostream>


// Global variable.
HWINEVENTHOOK g_hook;

// Prototype
void HandleWinEvent(HWINEVENTHOOK , DWORD , HWND ,
                         LONG , LONG ,
                         DWORD , DWORD );

// Initializes COM and sets up the event hook.
//
void InitializeMSAA()
{
    CoInitialize(NULL);
    g_hook = SetWinEventHook(
        EVENT_MIN ,EVENT_MAX,  // Range of events .
        NULL,                                          // Handle to DLL.
        HandleWinEvent,                                // The callback.
        0, 0,              // Process and thread IDs of interest (0 = all)
        WINEVENT_OUTOFCONTEXT ); // Flags.
}

// Unhooks the event and shuts down COM.
//
void ShutdownMSAA()
{
    UnhookWinEvent(g_hook);
    CoUninitialize();
}

// Callback function that handles events.
//
void HandleWinEvent(HWINEVENTHOOK hook, DWORD event, HWND hwnd,
                         LONG idObject, LONG idChild,
                         DWORD dwEventThread, DWORD dwmsEventTime)
{
    std::cout << std::hex << event ; // desperate attempt to see if any event is caught

    if (event == EVENT_SYSTEM_SWITCHSTART)
    {
        std::cout << "Begin" ;
    }
    else if (event == EVENT_SYSTEM_SWITCHEND)
    {
        std::cout << "End ";
    }
}



int main()
{
    InitializeMSAA();
    while( getch()!= 'q' ){;}
    ShutdownMSAA();
    return 0;
}

构建命令:
g++ -o alttab main.cpp -luser32 -lole32

我正在使用Windows XP和MinGW / GCC编译器4.5版。

最佳答案

从MSDN:调用SetWinEventHook的客户端线程必须具有消息循环才能接收事件。您的主线程等待“q”按钮,而不运行消息循环。

08-16 23:34