C ++ winApi无法处理子窗口事件

时间:2019-08-20 20:00:43

标签: c++ winapi childwindow

所以,我有两个功能。第一个用于处理父事件,第二个用于处理子事件。只是一个简单的方法。

// 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函数中创建,注册类并初始化这些窗口:

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条目。也许我用错误的名称注册了课程?不,我复制粘贴了它们。我用谷歌搜索,但没有找到答案。请帮忙。

1 个答案:

答案 0 :(得分:3)

来自MSDN WM_COMMAND

  

lParam代表控制窗口的句柄,如果是菜单或   加速器为0。

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