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

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

问题描述

我创建使用C ++一个简单的Win32程序,虽然我觉得我只是用c在此应用程序。我需要确定哪些HWND按钮是pressed上的应用程序。我搜索的MSDN参考,并只告诉我HIWORD是通知code和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中),但编译时给了我一个错误。我想我也试了一下比较,HIWORD和lParam 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.

推荐答案

您只需要看看 lParam的它的按键手柄:

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

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

虽然在code,你需要保持 HWND 的全局变量做比较。

// 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....

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

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