如何使用SetWinEventHook()函数获取活动窗口更改消息 [英] How to use SetWinEventHook() function to get Active Window changed message

查看:186
本文介绍了如何使用SetWinEventHook()函数获取活动窗口更改消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在从事一个项目,该项目需要检测当前的活动窗口并连续获取活动窗口标题。
谁能解释我如何使用 SetWinEventHook() 函数以获取活动 窗口已更改的消息。
[我使用 GetForegroundWindow()函数和计时器来获取活动窗口。由于存在计时器,该方法不太准确。因此,我需要将其与SetWinEventHook()函数一起使用。有人可以解释我该怎么做吗? ]

I have been working on a project which needs to detect current active window and get the active window title continuously. Can anyone explain me how to use SetWinEventHook() function to get Active Window changed message. [ i used GetForegroundWindow() function with a timer to get the active window. That approach is not very accurate because of the timer. So i need to use it with SetWinEventHook() function. can someone explain me how to do that? ]

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 */
}


推荐答案

我找到了解决方案。 EVENT_SYSTEM_FOREGROUND 事件是丢失的一块。即使前景窗口已更改为另一个窗口,系统也会发送此事件。

I have found the solution. EVENT_SYSTEM_FOREGROUND Event is the missing piece. The system sends this event even if the foreground window has changed to another window. We can use this event to get the current 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 */
     }
}

这篇关于如何使用SetWinEventHook()函数获取活动窗口更改消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆