活动窗口上的 Windows 系统事件是否已更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4407631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:46:58  来源:igfitidea点击:

Is there Windows system event on active window changed?

c#.netwindowswinapihook

提问by Vasyl Boroviak

The desktop application I'm developing need to know what windows were active while the application was run. Currently it performs GetForegroundWindow()call (of user32.dll) every 250 msec. The approach is not very accurate.

我正在开发的桌面应用程序需要知道应用程序运行时哪些窗口处于活动状态。目前它每 250 毫秒执行一次GetForegroundWindow()调用(of user32.dll)。方法不是很准确。

Is there any Windows (WINAPI?) event which fires every time the active (focused) window changed? I'd like to subscribe with my callback function.

是否有任何 Windows (WINAPI?) 事件会在每次活动(聚焦)窗口更改时触发?我想订阅我的回调函数。

Thanks.

谢谢。

回答by DReJ

Yes, you can use SetWinEventHookfunction.

是的,您可以使用SetWinEventHook函数。

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)
{
    /* your code here */
}

回答by Piskvor left the building

There's the WM_ACTIVATE message, which is sent to the activated and deactivated windows.

WM_ACTIVATE 消息,它被发送到激活和停用的窗口。