Dialog Boxes

Dialog boxes can be an important part, or even the sole part of a program with a GUI.  This is a brief tutorial on how to incorporate them in a visually designed program

First, start a new MFC AppWizard executable project.  In the next dialog box select Dialog based application as shown in the diagram here.  Choose Finish and the project will be created. dialog.gif (16796 bytes)
 
Next, choose the second (green) tab, which is the resource tab.   Then select IDD_DIALOG (you will have to expand the folders "dialog resources," and "dialog").  Double-click on IDD-DIALOG and you should see the dialog box itself. resources.GIF (11613 bytes)
 
A sample dialog box is shown here.  The first thing to do is to change the text that is displayed in the box.  You can right-click on the text and choose properties.  You can then edit some information about the text itself. layout.GIF (12000 bytes)
 
You can change the name of the text itself which is used internally for reference using the ID settting. Here we are just interested in the Caption setting which controls the text displayed in the dialog box.  To make this change just type in the Caption box. static.GIF (7514 bytes)
 
Now, add a button to the dialog box by clicking on a the button icon (third one down in the right column) on the tools palette.  Drag a new button to the dialog box.  You can then right-click on the new button and change its name using the properties dialog box in the same way that you changed the text in the text box.   Then right-click on the button again and choose Class Wizard.  You should then be presented with the dialog box on the right.  Double-click on BN_CLICKED and choose OK.  Then select Edit Code. wizard2.GIF (22300 bytes)

You can now enter code that will be executed when the button is clicked.  You can use the button to change settings in much the same way that right clicking changed colors or changed shapes.  You can also use it to display Message Boxes that would convey information to the user (or they can just be used in programming to confirm that the button was pressed).  In its simplest form, you can display a message box using the following command:

AfxMessageBox("Type text here");

You can actually display different types of message boxes and get information on which button was pressed in the message box.  You can change the icon changed in the message box and the types of buttons displayed by adding two parameters to the AfxMessageBox statement so that it now reads:

AfxMessageBox("Type text here",column1|column2);

Where column1 and column 2 define the icon and buttons as shown in the following columns:

Column1- Icon Column2- Buttons
MB_ICONEXCLAMATION MB_ABORTRETRYIGNORE
MB_ICONINFORMATION MB_OK
MB_ICONQUESTION MB_OKCANCEL
MB_ICONSTOP MB_RETRYCANCEL
  MB_YESNO
  MB_YESNOCANCEL

Finally, message boxes can return values in the form of integers.   You can use this feedback in the following way:

int choice;

choice=AfxMessageBox("Are you sure?",MB_YESNO|MB_ICONQUESTION);

if(choice==IDYES)

    do stuff here

The different choices for return values are shown here:

Button Response
IDABORT
IDCANCEL
IDIGNORE
IDNO
IDOK
IDRETRY
IDYES

Try using this information on dialog boxes to refine your drawing program to give the user some control over the drawing process using buttons and message boxes.

WB01624_.gif (281 bytes) Back to Reference Parameters