winapi BN_CLICKED how to identify which button was clicked?

陌路散爱 提交于 2019-12-17 20:59:43

问题


I'm creating a simple win32 program using c++, although I think i'm only using c in this app. I need to determine which HWND button was pressed on the app. I've searched msdn reference and it only told me HIWORD is the notification code, and LOWORD is the identifier, for the BN_CLICKED message. I've managed to get as far as determining when a button is clicked, but it only applies for all buttons. All my buttons are created in the WM_CREATE message. This is what i managed to whip up so far:

case: WM_CREATE:
    HWND hPlus = CreateWindowEx( 0, L"BUTTON", L"+", WS_CHILD | WS_VISIBLE, 130, 240, 35, 30, hwnd, ( HMENU )IDC_MENU, GetModuleHandle( NULL ), NULL );
    HWND hEquals = CreateWindowEx( 0, L"BUTTON", L"=", WS_CHILD | WS_VISIBLE, 170, 205, 65, 65, hwnd, ( HMENU )IDC_MENU, GetModuleHandle( NULL ), NULL );
break;

case WM_COMMAND:
    switch( HIWORD( wParam ) )
    {
        case BN_CLICKED:
            MessageBox( hwnd, L"OK", "OK", MB_OK );
            break;
    }
    break;

I've tried comparing hEquals to LOWORD( wParam ) but that gave me an error when compiling. I think I also tried comparing it to HIWORD and LOWORD of lParam as well, which also didn't compile. Now I'm clueless for what to do next.


回答1:


Give each button it's own ID, and pass it to CreateWindowEx in the hMenu parameter, which is used for that:

A handle to a menu, or specifies a child-window identifier, depending on the window style.

#define BTN_PLUS  100
#define BTN_EQUAL 101

CreateWindowEx( 0, L"BUTTON", L"+", WS_CHILD | WS_VISIBLE, 130, 240, 35, 30,
                hwnd, ( HMENU )BTN_PLUS, GetModuleHandle( NULL ), NULL );

CreateWindowEx( 0, L"BUTTON", L"=", WS_CHILD | WS_VISIBLE, 170, 205, 65, 65,
               hwnd, ( HMENU )BTN_EQUAL , GetModuleHandle( NULL ), NULL );

Then, in WM_COMMAND, you can test for the ID:

case WM_COMMAND: {
    if ( LOWORD( wParam ) == BTN_PLUS ) {
        [...]
    }
    [...]
    break;
}



回答2:


You just need to look at the lParam it's the button handle:

if ((HWND)lParam == hPlus)
{
    // "plus" clicked ... etc.
}

Though in your code, you'll need to keep the HWND's in global variables to do the comparison.

// somewhere global
HWND hPlus = NULL;
HWND hEquals = NULL;

// in your WndProc ...

case: WM_CREATE:
    hPlus = CreateWindowEx( 0, L"BUTTON", L"+", WS_CHILD | WS_VISIBLE, 130, 240, 35, 30, hwnd, ( HMENU )IDC_MENU, GetModuleHandle( NULL ), NULL );
    hEquals = CreateWindowEx( 0, L"BUTTON", L"=", WS_CHILD | WS_VISIBLE, 170, 205, 65, 65, hwnd, ( HMENU )IDC_MENU, GetModuleHandle( NULL ), NULL );
break;

case WM_COMMAND:
    switch( HIWORD( wParam ) )
    {
        case BN_CLICKED:
            // see which button was clicked
            if ((HWND)lParam == hPlus)
            {
                MessageBox( hwnd, L"hPlus was clicked", "OK", MB_OK );
            }
            break;
    }
    break;

You get the idea, I'm sure....



来源:https://stackoverflow.com/questions/20640330/winapi-bn-clicked-how-to-identify-which-button-was-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!