winapi BN_CLICKED how to identify which button was clicked?

advertisements

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.


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