Creating a Dialog Based Program
- Open up Visual C++
- First create a project
- Under the pull down menus select “File -> New…”
- Make sure you are under the “Projects” Tab
- Highlight “MFC AppWizard (exe)”

- Under “Project Name” type the name for the project
(i.e. Ch9_5 for Chapter 9, program 5) – this will create a directory
(folder) underneath your programming folder to store all the files
associated with the project
- Make sure the bullet “Create new workspace” is
selected (not “add to current workspace”)
- Click “OK” to create the project
- Select "Dialog based" on the next screen

- Click "Next", "Next", "Next" & "Finish"
Example of Editing your Dialog box
Let's say we want our window to multiply two numbers
together and print them out…
- Initially our window will look something like this:

- Click on the "TODO: Place dialog controls here." And hit
the Delete button on the keyboard
- Select an edit box from your controls toolbar (the "ab|"
button) and create 3 edit boxes.
- Select some static text from your controls toolbar (It
is the "Aa").
- Change the static text to show an "X" and an "=" (either
by just typing once it has been selected, or by right clicking, selecting
"Properties" and changing the caption)
- Resize your window so that it looks similar to:

- Delete the "OK" and "Cancel" buttons and create a new
one (by selecting it from the controls toolbar). Right click on the new
button, and select properties.

- Change the properties of the new button so that the ID
is "IDC_MULTIPLY" and the Caption is "Multiply"
- Note: (you can also change the IDs of the 3 edit boxes
to something more descriptive if you wish).
- Also Note that if you expand the Dialog folder on the
left side of the window, it shows your main window. If at any point while you
are coding you wish to get back to the dialog box, you can double click on
"IDD_P9_5_DIALOG"
- Next, right click anywhere and click on ClassWizard so
that ClassWizard appears.

- There are two tabs on the class wizard that we care
about. The "Message Maps" tab lets us create function calls when certain
actions are taken. In this case, we want a function to be called when the
Multiply button is clicked, so we click on it's ID code (IDC_MUTLIPLY) and
then on "BN_CLICKED" and hit "Add Function…" and a box appears asking us what
we want the function to be called

- We'll leave it with the suggested name of "OnMultiply"
to let us know that this function will be called whenever the button is
clicked on the multiply button.
- Next click on the "Member Variables" tab at the top of
the ClassWizard. Under this tab we can add variables that are linked to the
Edit boxes. This way we can put numbers to the screen or get numbers from the
screen by either setting or getting what is stored in the variable we create.
- Let's make the first edit box have a floating point
variable with the name of m_A.
- Putting "m_" at the beginning of the variable is a
convention programmers will use to show that a variable can be accessed
anywhere within the class (so we'll use that convention since variables
created in the ClassWizard can be accessed anywhere within the CP9_5Dlg
class).
- To make the variable, select "IDC_EDIT1" and click on
"Add Variable…"

- Name the variable m_A and give it a type of "float"
- Make the second edit box have a floating point variable
with the name of m_B.
- Make the third edit box have a floating point variable
with the name of m_C.

- Click "OK" to close the ClassWizard
- Click the ClassView tab on the bottom left corner of the
screen (next to the ResourceView tab)
- Expand the P9_5 classes, and expand CP9_5Dlg class.
- You'll see a list of all the functions that are
members of the class (including OnMultiply), as well as all the variables
that are members of the class (including m_A, m_B, m_C)
- By double-clicking on the OnMultiply function, the
window on the right will open the file OnMultiply is contained in (in this
case P9_5Dlg.cpp) and move you to the definition of that function.

- In this function we will add the code we want to execute
each time the Multiply button is pushed.
- The UpdateData function lets the compiler know whether
to take the values currently in the edit boxes and update the linked
variables (m_A, m_B, m_C), or to update the edit boxes with the values in
the linked variables.
- Calling UpdateData with an argument of TRUE (or you
could use 1), will take the values that are in the edit boxes and put them
into the variables. Likewise, calling UpdateData with an argument of FALSE
(or 0) will take the values that are in the variables and put them into the
edit boxes.
- Thus, the code of the function should like something
like:

- You can then compile, build and execute your program.
- Finally, if you go into windows explorer, you can find
an executable file (*.exe) that contains your program (that you could copy and
run by itself somewhere else).
- As a side note, if you go to the Build menu, and
select Set Active Configuration … you can change it to build in
"Release Mode" instead of "Debug Mode." This way your executable will be much
smaller since it does not have debug information in it. If you were to
release one of your programs to the public, this is the configuration you
would want. J