none
MFC里如何获取当前点击的按钮的ID RRS feed

  • 问题

  • 我把多个按钮的Clicked事件绑定到了同一个函数中,然后想使用GetDlgItemText来获取当前点击的按钮的TEXT属性,请问该如何实现?
    2011年3月30日 2:56

答案

  • // 注意button的ID号码要连续
    afx_msg void OnButtonClicked(UINT nID);

    ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTONX, OnButtonClicked)

    void CXXXDlg::OnButtonClicked(UINT nID)
    {
     int nIndex = nID - IDC_BUTTON1;
     CString str;
     GetDlgItemText(nID, str);
     CString strText;
     strText.Format(_T("%d - %s"), nIndex, str);
     AfxMessageBox(strText);
    }

     


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2011年3月30日 3:15
    版主

全部回复

  • // 注意button的ID号码要连续
    afx_msg void OnButtonClicked(UINT nID);

    ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTONX, OnButtonClicked)

    void CXXXDlg::OnButtonClicked(UINT nID)
    {
     int nIndex = nID - IDC_BUTTON1;
     CString str;
     GetDlgItemText(nID, str);
     CString strText;
     strText.Format(_T("%d - %s"), nIndex, str);
     AfxMessageBox(strText);
    }

     


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2011年3月30日 3:15
    版主
  • WM_COMMAND MESSAGE

    Sent when the user selects a command item from a menu, when a control sends a notification message to its parent window, or when an accelerator keystroke is translated.

    BOOL XXX::OnCommand(WPARAM wParam, LPARAM lParam)

    {

    UINT CtrlID = LOWORD(wParam);

    if( HIWORD(wParam) == BN_CLICKED && CtrlID >= 10000 && CtrlID <= 20000 )
    {
            // 通过控件编号,获取指定控件文本

    }

    }


    To Be An Excellent Coder!
    2011年3月30日 3:26