Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. BN_CLICKED

BN_CLICKED

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionlearning
3 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lucy 0
    wrote on last edited by
    #1

    I created a new class based on CButton -- CMyButton. I add a message notification handler in CMyButton.cpp for =BN_CLICKED to change an protected member variable of CMyButton.

    void CMyButton::OnClicked()
    {
    m_nState += 1;
    }

    Using resource editor, I put an instance of CMyButton to my dialog template. I want my dialog to handle the BN_CLICKED also, do some other stuff.

    void CMyDlg::OnMyBtn()
    {
    MessageBox("Button pressed.");
    }

    The problem is: CMyDlg::OnMyBtn is never called, even though I add the style of "Notify" to the button instance in the resource editor. What am I missing here? TIA!

    G 1 Reply Last reply
    0
    • L lucy 0

      I created a new class based on CButton -- CMyButton. I add a message notification handler in CMyButton.cpp for =BN_CLICKED to change an protected member variable of CMyButton.

      void CMyButton::OnClicked()
      {
      m_nState += 1;
      }

      Using resource editor, I put an instance of CMyButton to my dialog template. I want my dialog to handle the BN_CLICKED also, do some other stuff.

      void CMyDlg::OnMyBtn()
      {
      MessageBox("Button pressed.");
      }

      The problem is: CMyDlg::OnMyBtn is never called, even though I add the style of "Notify" to the button instance in the resource editor. What am I missing here? TIA!

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      You need an entry in the message map for CMyDlg that 'connects' your button to the handler.

      BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
      //{{AFX_MSG_MAP(CMyDlg)
      ON_BN_CLICKED(IDC_Control_ID, OnMyBtn)
      //}}AFX_MSG_MAP
      END_MESSAGE_MAP()

      IDC_Control_ID is the resource ID for the button that you assigned when you created it. To get a feel for this, use the wizard to create a simple dialog application. In the resource editor, drop a button on the dialog. In VC6, right-click on the button you just added and select Events. In VS.NET, right-click and select Add Event Handler. This lets you add code to the dialog class to handle events from the button. Take a look at the code generated by the wizard. It adds code in the following places:

      • A declaration is added to MyDlg.h for the button handler. This will be a line of the form afx_msg void OnMyButton();
      • An entry is added to the message map like the example above.
      • A definition of the handler is added to MyDlg.cpp, similar to the following:

      void MyDlg::OnMyButton()
      {
      // handle the button
      }


      Software Zen: delete this;

      L 1 Reply Last reply
      0
      • G Gary R Wheeler

        You need an entry in the message map for CMyDlg that 'connects' your button to the handler.

        BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
        //{{AFX_MSG_MAP(CMyDlg)
        ON_BN_CLICKED(IDC_Control_ID, OnMyBtn)
        //}}AFX_MSG_MAP
        END_MESSAGE_MAP()

        IDC_Control_ID is the resource ID for the button that you assigned when you created it. To get a feel for this, use the wizard to create a simple dialog application. In the resource editor, drop a button on the dialog. In VC6, right-click on the button you just added and select Events. In VS.NET, right-click and select Add Event Handler. This lets you add code to the dialog class to handle events from the button. Take a look at the code generated by the wizard. It adds code in the following places:

        • A declaration is added to MyDlg.h for the button handler. This will be a line of the form afx_msg void OnMyButton();
        • An entry is added to the message map like the example above.
        • A definition of the handler is added to MyDlg.cpp, similar to the following:

        void MyDlg::OnMyButton()
        {
        // handle the button
        }


        Software Zen: delete this;

        L Offline
        L Offline
        lucy 0
        wrote on last edited by
        #3

        yes, I already had it. It seemed to me that the BN_CLICKED is processed by the button itself and the message never get to the dialog. I add the following to CMyButton::OnClicked:

        GetParent()->PostMessage(WM\_COMMAND,
            MAKEWPARAM(this->GetDlgCtrlID(), BN\_CLICKED),
            LPARAM(this->m\_hWnd));
        

        but it didn't help. :confused::( Any hint?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups