简体   繁体   中英

Get message from other window

I try to get some messages from other windows. I need to find window by name, I used FindWindowW() . How can I get active window and get message from its? When I add hwnd in GetMessage(), it doesn't work.

//WndProc
        case WM_KEYDOWN:
            OutputDebugStringW(L"Key down");

            break;

//main.c
        //WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;

        hwnd = FindWindowW(NULL, L"Sublime Text");

        while (GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;

Only the thread that creates a window can directly receive and dispatch messages for that window. GetMessage() retrieves messages from the calling thread's message queue, so it can only be used with windows that are owned by the calling thread.

Since you are trying to catch messages for a window that is not yours, you will have to use SetWindowsHookEx() or SetWinEventHook() to install a hook callback into that window's owning thread, and then that callback can intercept the desired messages/events for that window. You can use GetWindowThreadProcessId() to get the IDs of the Process and Thread that own the window.

If you use SetWindowsHookEx() and are trying to hook a window in another process, your callback must reside in a DLL so it can be injected into that process. You do not need to do that with SetWinEventHook() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM