How to Make MFC Dialog Boxes
A dialog box is a basic window derived from the CDialog class of the Microsoft Foundation Class (MFC) Library. You can use this example to understand dialog boxes by calling one dialog box from another. You'll first create a CDialog object through the project wizard, then store a second dialog as a resource and create from the Class Wizard a class associated to it.
Things You'll Need
- Microsoft Visual C++ or Visual Studio .NET
- Basic familiarity with the IDE
Instructions
-
-
1
Launch Microsoft Visual C++ 6.0 or Visual Studio .NET. Create a new MFC AppWizard (exe) project and name it "Dialogs." Make sure that "Dialog based" and "Use MFC in a Shared DLL" are selected. Delete the "TODO" label from the dialog box.
-
2
Add a second dialog box. Click "Project" and then "Add Resource." Select "Dialog" under "Resource type" and click "New."
-
-
3
Set up the second dialog box. Right-click on it and change its ID to "IDD_DIALOGBOX2" and its caption to "Second." Close the "Properties Dialog." Associate the second dialog with a class. Open the Class Wizard by pressing Ctrl+W. Select "Create a new class," enter "CSecondDialog" in the "Name" text box and select "CDialog" as its base class. Click "Finish."
-
4
Add functionality by placing a button on the first dialog from the controls toolbox. Do a right-click on that button. Make the ID "IDC_BUTTONSECOND" and change the caption to "Second." Double-click the button, accept the default function name and make the following changes in the code of the DialogsDlg.cpp file.
Under the "TODO" comment enter:
CSecondDialog m_D2;
m_D2.DoModal();
Include the "SecondDialog" definition file at the top:
#include "SecondDialog.h"
-
5
Compile and run the code. Test the application to make sure it works.
-
1
Tips & Warnings
Dialog boxes are either "modal" or "modeless." A modal dialog box must be cancelled before the application can continue. A modeless dialog box allows work on other tasks.
The second dialog is an example of a modal window. The first dialog is an example of a modeless window.