Before you begin adding code to all the controls on your application window, you need to add a little bit of code to initialize the variables, setting starting values for most of them. Do this by following these steps:
1. Using the Class Wizard, on the Message Maps tab, select the OnInitDialogfunction
in the list of member functions. You can do this by finding the function in the Member Functions list, or by selecting the CDay2Dlgobject in the list of object IDs
and then selecting the WM_INITDIALOGmessage in the messages list, as shown in
Figure 2.9.
FIGURE2.9.
You can use the Class Wizard to locate exist- ing functions.
2. Click Edit Code to be taken to the source code for the OnInitDialogfunction.
3. Find the T O D Omarker, which indicates where to begin adding your code, and add
the code in Listing 2.1.
LISTING2.1.DAY2DLG.CPP—THEOnInitDialogFUNCTION IS WHERE YOU NEED TO ADD INITIALIZATION CODE. 1: BOOL CDay2Dlg::OnInitDialog() 2: { 3: CDialog::OnInitDialog(); 4: 5: . 6: . 7: . 8:
9: // TODO: Add extra initialization here 10:
11: /////////////////////// 12: // MY CODE STARTS HERE 13: /////////////////////// 14:
15: // Put a default message in the message edit 16: m_strMessage = “Place a message here”; 17:
2
19: m_bShowMsg = TRUE; 20: m_bShowPgm = TRUE; 21: m_bEnableMsg = TRUE; 22: m_bEnablePgm = TRUE; 23:24: // Update the dialog with the values 25: UpdateData(FALSE);
26:
27: /////////////////////// 28: // MY CODE ENDS HERE 29: /////////////////////// 30:
31: return TRUE; // return TRUE unless you set the focus to a
➥control 32: }
There is more code in theOnInitDialogfunction than has been included in
Listing 2.1. I won’t include all the code for every function in the code listings throughout this book as a means of focusing on the code that you need to add or modify (and as a means of keeping this book down to a reasonable size). You are welcome to look at the code that has been left out, to learn what it is and what it does, as you build your understanding of MFC and Visual C++.
Note
This initialization code is simple. You are setting an initial message in the edit box that you will use to display messages for the user. Next, you are setting all the check boxes to the checked state. It’s the last line of the code you added to this function that you really need to notice.
TheUpdateDatafunction is the key to working with control variables in Visual C++. This
function takes the data in the variables and updates the controls on the screen with the variable values. It also takes the data from the controls and populates the attached vari-
If you’ve programmed in C or C++ before, you’ve noticed that you are set- ting the value of the m_strMessagevariable in a very un–C-like manner. It looks more like how you would expect to set a string variable in Visual Basic or PowerBuilder. That’s because this variable is a CStringtype variable. The
CStringclass enables you to work with strings in a Visual C++ application in
much the same way that you would work with strings in one of these other programming languages. However, because this is the C++ programming language, you still need to add a semicolon at the end of each command.
ables with any values changed by the user. This process is controlled by the argument passed into the UpdateDatafunction. If the argument is FALSE, the values in the variables
are passed to the controls on the window. If the argument is TRUE, the variables are
updated with whatever appears in the controls on the window. As a result, which value you pass this function depends on which direction you need to update. After you update one or more variables in your code, then you need to call UpdateData, passing it FALSEas
its argument. If you need to read the variables to get their current value, then you need to call UpdateDatawith a TRUEvalue before you read any of the variables. You’ll get the
hang of this as you add more code to your application.
Closing the Application
The first thing that you want to take care of is making sure that the user can close your application. Because you deleted the OK and Cancel buttons and added a new button for closing the application window, you need to place code into the function called by the Exit button to close the window. To do this, follow these steps:
1. Using the Class Wizard, add a function for the IDC_EXITobject on the BN_CLICKED
message, as you learned to do yesterday.
2. Click the Edit Code button to take you to the new function that you just added. 3. Enter the code in Listing 2.2.
LISTING2.2.DAY2DLG.CPP—THEOnExitFUNCTION. 1: void CDay2Dlg::OnExit()
2: {
3: // TODO: Add your control notification handler code here 4:
5: /////////////////////// 6: // MY CODE STARTS HERE 7: /////////////////////// 8:
9: // Exit the program 10: OnOK();
11:
12: /////////////////////// 13: // MY CODE ENDS HERE 14: /////////////////////// 15: }
A single function call within the OnExitfunction closes the Window and exits the appli-
cation. Where did this O n O Kfunction come from, and why didn’t you have to call it in
2
CDialogclass from which your CDay2Dlgclass is inherited. In the CDialogclass, the mes-
sage map already has the object IDs of the OK and Cancel buttons attached to the O n O K
and OnCancelbuttons so that buttons with these IDs automatically call these
functions. If you had specified the Exit button’s object ID as IDOK, you would not have
needed to add any code to the button unless you wanted to override the base O n O K
functionality.