设置WindowsHookEx以确定何时停用窗口



我有一个全局WH_CBT 32 位钩子,我用它来确定何时使用 HCBT_ACTIVATE 激活窗口。

如何确定窗口何时停用?有CBTACTIVATESTRUCThWndActive ,但这有时是0x0的,切换到 64 位窗口时它不起作用。

没有HCBT_DEACTIVATE.

As@Remy Lebeau提到的,你可以使用WM_ACTIVATE消息。激活或停用窗口时都会发送此消息。设置一个WH_CALLWNDPROC钩子来捕获已停用的消息,它将在系统将消息发送到目标窗口过程之前获取消息。有关更多详细信息:

将 DLL 中的函数用于非本地挂钩:

#include <Windows.h>
#include <stdio.h>
LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) {
    HWND hwnd;
    //...
    DWORD threadID = GetWindowThreadProcessId(hwnd, NULL);
    HINSTANCE hinstDLL = LoadLibrary(TEXT("..\Debug\ProcHookDLL.dll"));
    void (*AttachHookProc)(DWORD);
    AttachHookProc = (void (*)(DWORD)) GetProcAddress(hinstDLL, "AttachHook");
    AttachHookProc(threadID);
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    //...
};

下面是 DLL 的代码:

#include <Windows.h>
#include <stdio.h>
HMODULE thisModule;
HHOOK hook;
LRESULT CALLBACK LaunchListener(int nCode, WPARAM wParam, LPARAM lParam);
#ifdef __cplusplus      // If used by C++ code, 
    extern "C" {        // we need to export the C interface
#endif
    __declspec(dllexport) void AttachHook(DWORD threadID) {
        hook = SetWindowsHookEx(WH_CALLWNDPROC, LaunchListener, thisModule, threadID);
    }
#ifdef __cplusplus
}
#endif
    LRESULT CALLBACK LaunchListener(int nCode, WPARAM wParam, LPARAM lParam) {
        // process event here
        if (nCode >= 0) {
            CWPSTRUCT* cwp = (CWPSTRUCT*)lParam;
            if (cwp->message == WM_ACTIVATE) {
                if (LOWORD(cwp->wParam) == WA_INACTIVE)
                {
                    //the window being deactivated
                }
                else
                {
                    //the window being activated
                }
            }
        }
        return CallNextHookEx(NULL, nCode, wParam, lParam);
    }

最新更新