如何从另一个窗口/程序发送/接收消息?

use*_*592 1 c++ windows winapi messages

我可以选择我想要的任何可见窗口并获取其主句柄,但我无法处理发送或接收消息。GetMessage() 函数总是返回 0。如果我想在属于另一个窗口的文本框当前处于活动状态时发送有关击键的消息怎么办?

MSG msg;
WPARAM wParam;
LPARAM lParam;
UINT message;
while(TRUE)
{
    GetMessage(&msg, rHwnd, 0, 0); // get message from another window
    TranslateMessage(&msg);
    wParam = msg.wParam;
    lParam = msg.lParam;
    message = msg.message;
    switch(message) // check whether an user clicked the 't' key
    {
        case WM_CHAR:
            switch(wParam)
            {
                case 't':
                    MessageBox(NULL, "t", "", 0);
                    break;
            }
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 5

向窗口发送消息很容易 - 使用PostMessage()或SendMessage...()为此(尽管为了模拟键盘输入,您应该SendInput()改用)。但是,GetMessage()只能为调用线程拥有的窗口检索消息,不能为另一个线程/进程拥有的窗口检索消息。如果您需要处理用于其他应用程序的消息,则必须通过SetWindowsHookEx().