c++winapichildwindow

C++ winApi cannot handle child window events


So, I have two functions. First for handling parent events and second for handling child ones. Just a trivial approach.

// 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;
}

And of course I create, register classes, and init these windows in my WinMain function:

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;
}

But I can't handle this click event within WndProc1. And within WndProc, too. My debugger just bypasses my WM_COMMAND case entry. Maybe I register classes with wrong names? No, I copy-pasted them. I googled but found no answers. Please help.


Solution

  • From MSDN WM_COMMAND.

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

    You should use the LOWORD(wparam) to get the command id.