同时有2个按钮c ++

时间:2018-07-13 12:22:25

标签: c++ winapi button createwindow

大家! 我试图在C ++应用程序中同时具有2个按钮,而彼此却执行2个不同的操作。 代码:

MSG msg;
HWND m_hwnd = GetConsoleWindow();
HWND hwndButton1 = CreateWindow(TEXT("button"), TEXT("B1"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_FLAT, 100, 100, 100, 30, m_hwnd, NULL, (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE), NULL);
HWND hwndButton2 = CreateWindow(TEXT("button"), TEXT("B2"), WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_FLAT, 200, 100, 100, 30, m_hwnd, NULL, (HINSTANCE)GetWindowLong(m_hwnd, GWL_HINSTANCE), NULL);
ShowWindow(hwndButton2, SW_SHOW);
ShowWindow(hwndButton1, SW_SHOW);
UpdateWindow(hwndButton2);
UpdateWindow(hwndButton1);
while(1)
{
    GetMessage(&msg, NULL, 0, 0);
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    if((HIWORD(WPARAM)) == BN_CLICKED and ((HWND)LPARAM) == hwndButton1)
    {
        printf("1st clicked");
    }
    if((HIWORD(WPARAM)) == BN_CLICKED and ((HWND)LPARAM) == hwndButton2)
    {
        printf("2nd clicked");
    }
}

但是它在'if'中给了我CE:“ expected primary-expression beore')'令牌”(x2,每个'if')。你能帮我吗?

1 个答案:

答案 0 :(得分:3)

WPARAMLPARAM是类型。您应该改用msg.wParammsg.lParam

if((HIWORD(msg.wParam)) == BN_CLICKED and ((HWND)msg.lParam) == hwndButton1)