The Coffee Project
File-New to get the new dialog box
Select MFC AppWizard (exe) as the project type
Click on the button next to Location and browse to the directory you’re using for your
projects in Visual C++
Type Coffee for
the project name
Click OK to get
the first AppWizard dialog box to appear.
We want a Dialog based application in English
Click NEXT button
Enter ‘The 330 Coffee Bar’ as the title for the
dialog
Click Next – the 3rd AppWizard dialog box appears
We want a standard MFC application with source
code comments being generated and using the MFC library as a
shared resource (to save space).
Click Next buttton.
Click Finish – to accept all of the default
values. The new project information box
appears
Click OK button
Open the Build menu and click on Set Active
Configuration.
Click on Coffee - Win32 Release. This speeds up the subsequent build process by not including any
debugging information. This is ok for
small projects.
Click
OK.
Adding a Group Box
Click on the ResourceView tab
Expand the Dialog folder
Double-click on IDD_COFFEE_DIALOG
to display the Coffee Bar dialog box
Click on the OK button to select it, then delete it using the Delete key.
Delete the Cancel button and the TODO text.
Right click on the empty dialog box, and select Properties
Click the Font button, and change the default font to Times
Roman 14 point
Click on the group box control and draw the box in about the top
third of the dialog box
Right click on the box border ... and select Properties
Fill in the caption box with "Good Morning. Please enter
your last name and your sex"
Adding an Edit Box
Click on the Static Text control in the tool box. Draw the control near the left side of the
group box. Make sure the left border
isn't touching the group box border.
Right-click on the static text control ... select Properties
Change the Caption to Last Name
In the toolbox, now add an edit box control ... right click
it ... select Properties... and change the ID to IDC_LNAME_EDIT
Adding Radio Buttons - allows selection of one of a number of
alternatives
** These should always be created in sequence!! ***
Click the radio button control in the toolbox
Draw the button. In the same way, draw a second button to the
right of the first
Change the ID properties to IDC_MALE_RADIO and IDC_FEMALE_RADIO
Change the captions to &Male and &Female -
& denotes the underline and the keyboard selection character
Click on the Group box for the first button. It indicates that it's the first button of a
group.
Check to make sure that the radio buttons are numbered in
sequence.
Click View and
select Resource Symbols and then check the sequence numbers
Adding Check Boxes - allows one or more than one choice to
be selected
Using the toolbox, add another group box to the middle third of
the screen
Make its caption How would you like your coffee?
Add 3 check boxes -
·
IDC_CREAM_CHECK captioned
With &Cream
·
IDC_SUGAR_CHECK captioned
With &Sugar
·
IDC_SCOTCH_CHECK captioned With &Scotch
Finally, add a command
button, with an ID of IDC_ORDER_BUTTON, and a caption of &Place
Your Coffee Order
Here's what everything should now look like -
Coding the Program
* Declare member variables for the controls that get data from the
user:, the edit box, radio buttons, and
the check boxes;
* Make the program set initial values for the controls when it
first loads;
* Attach 'event code' to the controls so that they respond
correctly when the user clicks on them;
* Exchange data between the controls and their member
variables by calling the UpdateData(
) function
Declaring Member Variables
The program can't directly read the edit box contents. The data must first be copied to a member
variable, then the program can access it and use it inside of a message box.
Open ClassWizard (View-ClassWizard) and select Member Variables
tab
Click on Add Variable
Make the member name m_SexRadio
Similarly, add the following member variables -
Control Variable Name
IDC_CREAM_CHECK m_CreamCheck
IDC_SUGAR_CHECK m_SugarCheck
IDC_SCOTCH_CHECK m_ScotchCheck
IDC_LNAME_EDIT m_LnameEdit
When done, do a File-Save
Initializing the Controls
The only control that really needs to be initialized is the &Male
radio button ...
The OnInitDialog event starts when the program loads ... so
we put initialization code there ...
In ClassWizard, click on Message Maps tab
Make sure the class name is CCoffeeDlg
In ObjectIDs, make sure that CCoffeeDlg is selected
In the Messages list, select WM_INITDIALOG (WM = windows message)
Click on OK
Then click the Edit
Code button.
The main C++ code file is opened, coffee.cpp, with
the OnInitDialog( ) function displayed.
Scroll down to the line that says // TODO: add extra
initialization code
Add the following code:
M_SexRadio = 0; /
/ the first button will be selected to start
UpdateData(FALSE); //
copies data from member variables to their associated controls
//
TRUE copies data from controls to member variables
Do a File-Save All
Build and run it ... notice that
the Male Button is selected.
Attaching Code to Radio Buttons
ClassWizard - Message Maps
Make sure the CCoffeeDlg is in the class name box
In the ObjectID list, select IDC_MALE_RADIO
In the Messages list, click on BN_CLICKED.
Click on Add Function button ... click OK to select
the default
Click on Edit Code ... the OnMaleRadio( ) function
appears
Add: m_SexRadio = 0;
Using the same method, create an OnFemaleRadio() function and add
m_SexRadio =
1;
Note, adding code to
check boxes is identical to adding code to radio buttons.
Adding Code to the Command Button
ClassWizard - Message Maps
Make sure the CCoffeeDlg is in the class name box
In the ObjectID list, select IDC_ORDER_BUTTON
In the Messages list, click on BN_CLICKED.
Click on Add Function button ... click OK to select
the default
Click on Edit Code ... the OnOrderButton( ) function
appears - add the following code:
void CCoffeeDlg::OnOrderButton()
{
// TODO: Add your
control notification handler code here
CString Salutation;
CString CoffeeStuff;
UpdateData(TRUE);
CoffeeStuff =
"";
if (m_CreamCheck)
CoffeeStuff +=
"Cream";
if (m_SugarCheck)
CoffeeStuff +=
"\nSugar";
if (m_ScotchCheck)
CoffeeStuff +=
"\nScotch";
if (m_SexRadio == 0)
Salutation =
"Mr. ";
else
Salutation =
"Ms. ";
MessageBox("Good
Morning, "+Salutation+m_LnameEdit, "The Coffee Program");
MessageBox("Your
coffee will be served with: \n" + CoffeeStuff, "The Coffee
Program");
}
Rebuild ... and run ... pick some options.
Created and
last update by DrB on Friday, July 30, 2004 10:58 AM