Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
void MFC_Tutorial_7Dlg::OnMybutton()
{
// TODO: Add your control notification handler code here
MessageBox("My Button Clicked");
}
12. Add a line "MessageBox("My Button Clicked");" inside the function as highlighted above.
13. Now build and run the application.
14. We'll see the dialog with "My Button" command button placed on the dialog. Click on the button,
we'll see a message box.
Check Box Usage
Check boxes are used to check binary states. Yes or No answers, Male/Female kind of conditional user
interface inputs are easier with a check box. This part of Codersource MFC Tutorial explains how to use a
Check box in dialog box applications of MFC.
Check box:
There are quite a few techniques to be learnt with all the MFC Controls. Some of them related to the
Check Box control are
void CMfc_tutorial_8Dlg::OnFirstcheckbox()
{
// TODO: Add your control notification handler code here
MessageBox ("First Check box Clicked");
}
Build the application and Run it. If you click on the check box, you should see a message box.
Manipulating the check box:
A Check box can return a value ofTrue or False. Let's add a Command Button to get the values of
check box.
Open the dialog editor. Add a Command Button with a caption "Get Second Box". Name the id
with IDC_GETSECONDCHECKBOXVAL.
Press Ctrl+W and create a BN_CLICKED handler for it. On the Message Maps tab, Select
IDC_GETSECONDCHECKBOXVAL on theObject ID and select the BN_CLICKED Message.
Click on Add function with a function name as OnGetSecondBox and add the handler.
On the Member Variables tab, select IDC_SECONDCHECKBOX and click on the "Add
Variable". Give the name of the variable asm_ctlSecondCheckBox. On the Category combo,
choose Control and theVariable Type will be automatically selected as CButton.
Now open the "mfc_tutorial_7dlg.cpp" (in your case "YourApplicationNameDlg.cpp") and look for the
OnGetSecondBox() function. Add the following code to it.
void CMfc_tutorial_8Dlg::OnGetSecondBox()
{
// TODO: Add your control notification handler code here
CString l_strCheckBoxVal;
int l_ChkBox = m_ctlSecondCheckBox.GetCheck();
l_strCheckBoxVal.Format("Second Check Box Value : %d",l_ChkBox);
MessageBox(l_strCheckBoxVal);
}
If the Second Check Box is checked, then the message box will display the string as "Second Check
Box Value : 1". If not, it will display the string as "Second Check Box Value : 0". That means, the
GetCheck function returns 1 if the control is checked and 0 if it is not.
The variable that we use is m_ctlSecondCheckBox , which was created earlier using the Member
variable tab of the class wizard.
In the same way we can use SetCheck function to set the values of the check box.
DDX mechanism:
This part of the check box MFC Tutorial deals with an elegant mechanism. This DDX mechanism is an
easier way out of the programming intricacies. MFC itself takes care of all the complexities of
programming for us. We don't have to worry about getting or setting the values of check boxes ( or any
other controls related), by calling GetCheck or SetCheck.
This is called as Dynamic Data Exchange mechanism. The MFC Framework itself keeps track of the
values on the controls. At any point of time, the values are accessible through the variables declared as
the member variables.
1. Click on the Ctrl+W to open the class wizard
2. In the Member variables tab, select the IDC_SECONDCHECKBOX and clickAdd Variable
3. Enter the variable name as m_SecondCheckBox
4. Leave the Category and Variable typesunchanged at Variable and bool. Click ok.
5. Add a new command Button with a caption "Get DDX" and add a handler to it.
6. We can now get the value of the checkbox without using theGetCheck and SetCheck functions
as follows.
void CMfc_tutorial_8Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString l_strCheckBoxVal;
UpdateData();
int l_ChkBox = m_SecondCheckBox;
UpdateData(FALSE);
l_strCheckBoxVal.Format("Second Check Box Value : %d",l_ChkBox);
MessageBox(l_strCheckBoxVal);
}
The new code is highlighted with the bold letters. This is the way MFC handles all the controls from
which we need some values to be extracted. This Dynamic Data exchange eases the programmer's job
very much. Now we can get the same output as in the manual handling of the check box.
The CEdit class provides the functionality of an edit control in Microsoft Windows based systems. It is a
rectangular child window which can be used to enter text in GUI Programs. This article explains how to
use the CEdit class for some of the important operations to be performed in a dialog based program. This
is also applicable in SDI/MDI applications which use a dialog box.
CEdit - Setting up the program:
The following steps explain how to setup the base application/dialog box for using the edit control. The
class CEdit will be used to manipulate the edit control being used. If this basic set up is already done,
readers can skip this section and move to the next one.