Command Buttons in MFC Dialog Boxes – MFC Tutorial Part 7

The next step after learning Dialog box creation in MFC, are the controls. This article deals with the Command Button control. We’ll look at how to use the CButton class in case of the command button controls. This article assumes the user has familiarity with the basic Windows controls. This MFC Tutorial only concentrates on how to manipulate them using MFC.

Before using the controls, we need to have our base application with a dialog box for this MFC Tutorial. This dialog box will be the container to hold the controls. At least from now on (for easier learning), the Codersource MFC Tutorial articles will start using the wizard to create the base application.

To Create the Application:

  • Open the Microsoft Visual Studio 6.00. Click Menu–> File–> New and Select MFC AppWizard(exe) option.
  • In the next MFC AppWizard screen – Step 1, Choose Dialog based application.
  • Click Finish after selecting this. All the files needed for this MFC Tutorial will be created.
  • If the project is built and run, it will open a dialog box, with two command buttons and one static display control.
  • Close the application and come back to the Microsoft Visual studio.

The application created in the above steps of this MFC Tutorial will be used to add and use the controls.

The wizard has already created two command buttons with captions OK and Cancel. The wizard has created two automatic handlers for each one of the buttons. When we click them, it just closes the dialog. Delete these two command buttons and the static control. We’ll create our own command buttons in our MFC Tutorial.

Adding the command Button:

  • Open the dialog box. There must be a controls tool box appearing on the sides. Click on the command button control and Click on the dialog box, where you want to place the button.
  • The button is placed in the dialog box and its caption is set to “Button1” by default.
  • Click the mouse on the Button and Press Enter key.
  • A Property sheet will appear, showing all the properties of the button.
  • Change the ID to IDC_MYBUTTON and change the caption to “My Button”.
  • Now Press the Ctrl+W keys to invoke the class wizard.
  • Ensure that you are in the Message Maps tab.
  • Ensure that the Top right side combo (class name) contains the CMFC_Tutorial_7 class. If you have named your application with a different name, it should have “yourapplicationdlg” selected.
  • Select the IDC_MYBUTTON on the left hand side and BN_CLICKED on the right handside of the dialog. Then click “Add Function”. Say ok to the dialog which appears eventually.
  • If you look at the list box down, it will have Onmybutton ON_IDC_MYBUTTON:BN_CLICKED inside the list. Click on Edit Code. This will lead to the MFC_Tutorial_7Dlg.cpp.
  • The following function will be created in this file.
    void MFC_Tutorial_7Dlg::OnMybutton() 
    {
        // TODO: Add your control notification handler code here
        MessageBox("My Button Clicked");
    }
  • Add a line “MessageBox(“My Button Clicked”);” inside the function as highlighted above.
  • Now build and run the application.
  • We’ll see the dialog with “My Button” command button placed on the dialog. Click on the button, we’ll see a message box.

The subsequent articles in this MFC Tutorial will deal with the other controls in MFC. Please find the Sample code here.

The subsequent articles in this MFC Tutorial will deal with the other controls in MFC. Please find the Sample code here.