BN_CLICKED用于检测菜单和按钮单击或只是单击按钮吗?

时间:2014-11-07 08:03:27

标签: c++ winapi

我应该使用BN_CLICKED来检测菜单和按钮单击还是只按下按钮? 这个示例代码也是正确的:

case WM_COMMAND:
    if (HIWORD(wParam) == BN_CLICKED) // button/menu was clicked
    {
        switch(LOWORD(wParam))
        {
        // Button
        case 1000:
            MessageBox(hWnd, "OK button was clicked", "", 0);
            break;

        // Menu
        case 1001:
            MessageBox(hWnd, "Open menu was clicked", "", 0);
            break;
        }
    }
    break;

1 个答案:

答案 0 :(得分:1)

请勿先检查通知代码。找出首先发送消息的内容。通知代码不保证是唯一的。

The documentation for WM_COMMAND表示如果邮件是通过菜单或加速器发送的,则lParam为0。如果是这样,wParam的高位字是0表示菜单,1表示加速器。首先检查

if (lParam == 0)
    if (HIWORD(wParam) == 0)
        // menu
        switch (LOWORD(wParam)) {
        case /* id of menu option 1 */:
            /* code for menu option 1 */
            break; // or return or whatever
        case /* id of menu option 2 */:
            /* code for menu option 2 */
            break;
        ...
        }
    else
        // accelerator
        switch (LOWORD(wParam)) {
        case /* id of accelerator 1 */:
            /* code for accelerator 1 */
            break;
        case /* id of accelerator 2 */:
            /* code for accelerator 2 */
            break;
        ...
        }
else
    // control
    switch (LOWORD(wParam)) {
    case /* id of button control */:
        switch (HIWORD(wParam)) {
        case BN_CLICKED:
            // clicked action
        ...
        }
        break;
    case /* id of some other control */
        /* likewise */
        break;
    ...
    }