C++ winApi 无法处理子 window 事件

C++ winApi cannot handle child window events

所以,我有两个功能。首先用于处理父事件,其次用于处理子事件。只是一个微不足道的方法。

// Parent
LONG WINAPI WndProc(HWND hwnd, UINT Message,
    WPARAM wparam, LPARAM lparam) {
    // задаются пользователем, по идее.

    HDC hdc;
    PAINTSTRUCT ps;
    switch (Message) {
        case WM_LBUTTONDOWN:
        {
            ShowWindow(hChildWnd, SW_SHOW | SW_SHOWNORMAL);
            UpdateWindow(hChildWnd);

        }
        break;
        case WM_COMMAND:
            switch (lparam) {
                case 300:
                    showNotification(L"Hey"); // not working???
                    break;
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, Message, wparam, lparam);
    }
    return 0;
}

// Child
LONG WINAPI WndProc1(HWND hwnd, UINT Message,
    WPARAM wparam, LPARAM lparam) {
    switch (Message) {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_COMMAND:
            switch (lparam) {
                case 300:
                    showNotification(L"Hey"); // not working???
                    break;
            }
            break;
        default:
            return DefWindowProc(hwnd, Message, wparam, lparam);
    }
    return 0;
}

当然,我创建、注册 类,并在我的 WinMain 函数中初始化这些 windows:

int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {

    MSG msg;

    // parent
    WNDCLASS wc;
    memset(&wc, 0, sizeof(WNDCLASS));
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hbrBackground = CreateSolidBrush(0x00FFFFFF);
    wc.lpszClassName = L"My Class";
    RegisterClass(&wc);

    // Child
    WNDCLASS wcChild1;
    memset(&wcChild1, 0, sizeof(WNDCLASS));
    wcChild1.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wcChild1.lpfnWndProc = (WNDPROC)WndProc1;
    wcChild1.hInstance = hInstance;
    wcChild1.hbrBackground = CreateSolidBrush(0x00FFFFFF);
    wcChild1.lpszClassName = L"My Dialog Class";
    RegisterClass(&wcChild1);


    hWnd = CreateWindowW(L"My Class", L"График функции loga(x)",
        WS_OVERLAPPEDWINDOW,
        00, 00, 1366, 768, NULL, NULL,
        hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    hChildWnd = CreateWindowW(
        L"My Dialog Class", L"Диалог",
        WS_OVERLAPPED | WS_CAPTION,
        10,
        10,
        90,
        170,
        NULL,
        NULL,
        hInstance,
        NULL);


    HWND hButton1 = CreateWindow(_T("button"), _T("OK"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
        10,//x
        90,//y
        80,//width
        30,//height
        hChildWnd,
        (HMENU)300,//id кнопки
        hInstance, NULL);


    ShowWindow(hChildWnd, SW_HIDE);
    UpdateWindow(hChildWnd);



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

但是我无法在WndProc1内处理这个点击事件。在 WndProc 之内。我的调试器只是绕过了我的 WM_COMMAND case 条目。也许我用错误的名字注册了 类?不,我复制粘贴了它们。我用谷歌搜索但没有找到答案。请帮忙。

来自 MSDN WM_COMMAND.

lParam represents a handle to a control window and in case of menu or accelerator its 0.

您应该使用 LOWORD(wparam) 来获取命令 ID。