How to use the SetWinEventHook () function to get a message with a modified Active Window

I was working on a project that needs to detect the currently active window and get the title of the active window at all times. Can anyone explain to me how to use SetWinEventHook () to get the Active Window . [I used the GetForegroundWindow () function with a timer to get the active window. This approach is not very accurate due to the timer. Therefore I need to use it with the SetWinEventHook () function. can someone explain me how to do this? ]

hEvent = SetWinEventHook(EVENT_SYSTEM_FOREGROUND , EVENT_SYSTEM_FOREGROUND ,NULL, 
WinEventProcCallback, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);


VOID CALLBACK WinEventProcCallback ( HWINEVENTHOOK hWinEventHook, DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
      /* how to get active window message */
}

      

+3


source to share


1 answer


I found a solution. EVENT_SYSTEM_FOREGROUND The event is the missing piece. The system dispatches this event even if the foreground window has changed to another window. We can use this event to get the currently active window.



VOID CALLBACK WinEventProcCallback ( HWINEVENTHOOK hWinEventHook, DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime)
{
     if (dwEvent == EVENT_SYSTEM_FOREGROUND)
     {
         /* do something */
     }
}

      

+3


source







All Articles