locked
MFC Button Click RRS feed

  • Question

  • Hi,

    I've created a button (Cbutton.Create) in a window which is created using the CFrameWnd. I don't know how to perform an action when the button is clicked.

    So, Guide me how to perform onclick event in a button.

    Thanks
    Pandi
    Monday, June 22, 2009 5:46 AM

Answers

  • Add this function to .h file (well the name can change ;))

    void OnMyButtonClick();

    And then to CPP file

    BEGIN_MESSAGE_MAP( CMyClass, CBaseWndClass )
     ON_BN_CLICKED( IDC_MY_BUTTON_ID, OnMyButtonClick)
    END_MESSAGE_MAP()

    void CMyClass::OnMyButtonClick()
    {
        MessageBox( "You clicked on me ;) ");
    }

    ON_BN_CLICKED macro is the message map entry for trapping a button click event, which in turn calls OnMyButtonClick function.

    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    • Marked as answer by Nancy Shao Monday, June 29, 2009 9:36 AM
    Monday, June 22, 2009 5:53 AM

All replies

  • Add this function to .h file (well the name can change ;))

    void OnMyButtonClick();

    And then to CPP file

    BEGIN_MESSAGE_MAP( CMyClass, CBaseWndClass )
     ON_BN_CLICKED( IDC_MY_BUTTON_ID, OnMyButtonClick)
    END_MESSAGE_MAP()

    void CMyClass::OnMyButtonClick()
    {
        MessageBox( "You clicked on me ;) ");
    }

    ON_BN_CLICKED macro is the message map entry for trapping a button click event, which in turn calls OnMyButtonClick function.

    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    • Marked as answer by Nancy Shao Monday, June 29, 2009 9:36 AM
    Monday, June 22, 2009 5:53 AM
  • I've used only afxwin.h header file only in my application. No any header files alone. So, where should I add that function(void OnMyButtonClick();) which you've mentioned.



    Pandi
    Monday, June 22, 2009 6:39 AM
  • Hello Pandi,

    You need to buy the book I suggested, you can learn fast that way. :)

    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Monday, June 22, 2009 8:52 AM
  • I've completed those task. I'm moving with Checkbox, Radio buttons..
    Pandi
    Monday, June 22, 2009 8:54 AM
  • Show the code you used to create the button in the CMainFrame (derived from CFrameWnd).  Nibu is right, you need to add a ON_MESSAGE line in the message map of CFrameWnd, and add the function to both the Mainfrm.h and Mainfrm.cpp.  When in doubt, simply add a button to the About dialog and handle the click.  See the code that is added for that, and add similar code to CMainFrame.
     
    -- David
     
     
    "Karunagara" wrote in message news:ddd656e0-4eee-47d 8-83ae-8fb6da54f265...
    I've used only afxwin.h header file only in my application. No any header files alone. So, where should I add that function(void OnMyButtonClick();) which you've mentioned.



    Pandi
    Monday, June 22, 2009 6:06 PM