WM_CHAR 在主循环中使用 IsDialogMessage() 时不起作用

WM_CHAR doesn't work when IsDialogMessage() is used in the main loop

我在我的主循环中插入了 IsDialogMessage(),以便选项卡控件中的选项卡键工作(在我的真实代码中),但 WM_CHAR 消息停止工作。 WM_KEYDOWN 仍在工作,但我计划处理 WM_CHAR 中的原始字符和 WM_KEYDOWN 中的组合键,因此我将同时处理键盘输入。为什么它停止工作?我错过了什么?

完整代码如下:

#pragma comment(lib, "user32.lib")

#define WIN32_LEAN_AND_MEAN
#define UNICODE
#define _UNICODE

#include <windows.h>
#include <assert.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HWND hMainWindow;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PWSTR lpCmdLine, int nCmdShow) {

    MSG  msg = {0};
    WNDCLASSW wc = {0};
    wc.lpszClassName = L"window1";
    wc.hInstance     = hInstance;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursor(0, IDC_ARROW);

  
    if(!RegisterClass(&wc)) {
        assert(!"register window failed!");
    }

    hMainWindow = CreateWindow(wc.lpszClassName, L"dear window",
                  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                  100, 100, 330, 270, 0, 0, hInstance, 0);

    while (GetMessage(&msg, NULL, 0, 0))
    {
 
        if (!IsDialogMessage(hMainWindow, &msg))
        { 
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, 
    WPARAM wParam, LPARAM lParam) {

    switch(msg) {

        case WM_CREATE:
      
            CreateWindowW(L"Static", L"this is label1", 
                WS_CHILD | WS_VISIBLE | SS_LEFT,
                20, 20, 300, 230, 
                hwnd, (HMENU) 1, NULL, NULL);
            break;

        case WM_DESTROY:

            PostQuitMessage(0);
            break;

        case WM_CHAR:
        {
            static wchar_t buffer[2] = {0};

            buffer[0] = wParam;
            MessageBox(0, buffer, L"you typed", MB_OK);
        }
        break;
    }

    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

WM_CHAR 消息被发送到具有当前键盘焦点的 window。您正在尝试在对话框 window 的 WndProc 中捕获 WM_CHAR 消息,但是如果您的对话框 window 上有任何可聚焦的子控件,那么对话框 window 的 WndProc将不会收到 WM_CHAR 消息,它们将被发送到具有焦点的子控件的 WndProc。因此,您必须子类化子控件才能捕获 WM_CHAR 消息。

否则,只需在消息循环中处理键盘消息,然后再将它们传递给 IsDialogMessage()/TranslateMessage()