winapi BN_CLICKED 如何识别点击了哪个按钮? [英] winapi BN_CLICKED how to identify which button was clicked?

查看:30
本文介绍了winapi BN_CLICKED 如何识别点击了哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 c++ 创建一个简单的 win32 程序,尽管我认为我只在这个应用程序中使用了 c.我需要确定在应用程序上按下了哪个 HWND 按钮.我搜索了 msdn 参考,它只告诉我 HIWORD 是通知代码,LOWORD 是 BN_CLICKED 消息的标识符.我已经设法确定何时单击按钮,但它仅适用于所有按钮.我所有的按钮都是在 WM_CREATE 消息中创建的.到目前为止,这是我设法提出的:

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;

我尝试将 hEquals 与 LOWORD( wParam ) 进行比较,但是在编译时出现错误.我想我也尝试将它与 lParam 的 HIWORD 和 LOWORD 进行比较,它们也没有编译.现在我不知道下一步该做什么.

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.

推荐答案

为每个按钮赋予自己的 ID,并将其传递给 CreateWindowEx 在 hMenu 参数中,用于:

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

然后,在 WM_COMMAND 中,您可以测试身份证:

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

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

这篇关于winapi BN_CLICKED 如何识别点击了哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆