Home arrow C++ arrow Page 4 - Using MFC in C++ Part 3: Dialog Boxes

Using MFC in C++ Part 3: Dialog Boxes

In part three of this ten part series, Mitchell talks about modal and modeless dialog boxes. "To create a fully interactive user experience, any application must employ some sort of dialog-based interaction. Dialog boxes come in two flavors: modal and modeless..."

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 55
December 08, 2001
  1. · Using MFC in C++ Part 3: Dialog Boxes
  2. · Creating a modal dialog box
  3. · Creating a modal dialog box (contd.)
  4. · Encapsulating our dialog resource
  5. · Creating a modeless dialog box
  6. · The OnInitDialog() function
  7. · Conclusion

print this article

Using MFC in C++ Part 3: Dialog Boxes - Encapsulating our dialog resource
(Page 4 of 7 )

Notice how I have been referring to our dialog as a “resource”. This is because we haven’t actually created a class that can use our dialog resource yet. For our dialog resource to become part of our application, we must derive a class from the MFC CDialog class, just like we have derived classes from CFrameWnd and CWinApp.

Once we have derived a class from CDialog, we will reference our dialog resource and be able to display it as part of our application. Lets start by creating our new class, which will be derived from the MFC class, CDialog.

Double-click on main.h, and add the following code at the end of the file:

class CTestDialog : public CDialog



CTestDialog(char* DialogName, CWnd* Owner) :

CDialog(DialogName, Owner) {}



We are declaring a new class named CTestDialog, which is derived publicly from CDialog, meaning that our new class inherits every method and variable declared as either public or protected in its CDialog base class.

We have named our new class CTestDialog. This is simply the text id of our dialog resource, prefixed with a “C”, for class. This is the standard C++ naming convention and although optional, it should be adhered to.

Our new CTestDialog class contains a constructor, which is shown below:

CTestDialog(char* DialogName, CWnd* Owner) : CDialog(DialogName, Owner) {}

This is an empty constructor, which accepts the name of a dialog resource to create, as well as a pointer to a CWnd* object. A CWnd* object is simply a pointer to a window, such as our main window (which is a CFrameWnd object, derived from the CWnd base class). The constructor simply passes these two arguments to its CDialog() base constructor, which handles the creation and memory allocation of our dialog box.

Lastly, we have the DECLARE_MESSAGE_MAP() macro, which allows our CTestDialog class to accept messages from windows.

Next, we need to add the implementation code for our CTestDialog class to the bottom of main.cpp, just before the “CApp App;” line:


// Message map macros for CTestDialog will go here


As described in previous articles, our BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() macros allow us to tell windows which messages our application will handle. The first parameter is the class that we are defining message handlers for, CTestDialog. The second parameter is the base class of the first parameter, CDialog.

The last step is to actually create the code that will load an instance of our CTestDialog class and display the dialog box. Change the CMainWin::OnShowDialog() function in main.cpp to look like this:

afx_msg void CMainWin::OnShowDialog()


CTestDialog testDlg("TestDialog", this);



We simply create a new instance of our CTestDialog class, passing the id of our dialog resource, “TestDialog” as the first parameter, and “this” as the second parameter. Because the OnShowDialog function is part of the CMainWin class (which is derived from the CFrameWnd MFC class), “this” returns a pointer to the current class, which is CMainWin. CMainWin is derived from the CFrameWnd class, which has CWnd as its base class, which is what the second parameter has to be.

Compile and run your application by pressing Ctrl+F5. When your application loads, either select the “Show Dialog” menu option, or press Ctrl+S. Your new dialog class will be instantiated and displayed, as shown below:

Our new, empty modal dialog box

Notice that you can’t click back to the main window without closing the modal dialog box first. There will be some cases where you will want to have access to the dialog box AND the main window. This is where modeless dialog boxes will come in handy, so let’s take a look at them now.
blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials