Visual C++/MFC Tutorial - Lesson 5: Dialog-Based Applications

Lesson 5: Dialog-Based Applications

We won't build a dialog application just yet, but I will tell you enough here so that you get the picture of what's going on in dialog applications. Dialog apps are the simplest apps in my opinion. In the IDE, go to File, New, Projects, MFC AppWizard(exe), and type in a project name. Hit next. Select Dialog Application as the type of application and then hit finish. Next go to the File View. You will see the source files created automagically. You should be able to compile and run the application as it is.

What is going on in all these files?  Everything boils down to the CWinApp derived class and the CDialog derived class (which is derived from CWnd). Look in the source file named after your project. You should see a the InitInstance() function there. Inside of that function you can see that a dialog class is constructed, it is set as the 'main window' of the application, and it is displayed with the DoModal() function. Once you exit your dialog app, the DoModal() function returns and your dialog is hidden. InitInstance() returns FALSE and your application ends. Now the question is, "What is DoModal()?"

There are 2 ways to create a dialog, Modal and Modeless. A Modal dialog suspends the program until you press OK, Cancel, or Close. On the other hand a modeless dialog can remain opened while allowing you to press buttons and whatnot on the rest of the application. An example of a Modal dialog is one of those annoying error boxes with an OK button on it. That is the only type of dialog we'll talk about here. To create a Modal dialog, you simply need to call the dialog's DoModal() function. It returns either IDOK or IDCANCEL depending on how you exited the dialog. Internally the DoModal() will call OnInitDialog() which is a good place to initialize your dialog variables. If you create a dialog app, you will notice that a default dialog class and resource is created for you. The file name for the class will be your project name with a 'Dlg' tacked on at the end.

Though we aren't making an application just yet, I have to tell you how to put something useful on the dialog. First open up the resource view. Open up your dialog in the editor. Right click on the dialog and select 'properties'.  Make sure the 'Visible' checkbox is checked. If it isn't, you won't be able to see your dialog. (Remember this, it will come back to haunt you in the future). You can change the Dialog's caption here as well as other things.

Now drag a button control onto the dialog somewhere. Right click on it and select properties. You can change the ID of the button to something more descriptive like IDC_SHOWMESSAGE. You can also change the text on the button to something more descriptive like 'Show Message'. You have a button now. But it does nothing. You can easily fix that. Press Ctrl-W to bring up the class wizard. Make sure you are on the first tab. You should see your dialogs name and the button's ID in the list on the left. If you select the dialog's name you can
see all of the functions and messages that the class wizard will let you add code for on the right. You can see the WM_PAINT and all the other messages we talked about. If you select the ID of the button you can see the messages that the button sends.  Double click on the CLICK message and accept the default function name. You see that it appears in the list at the bottom. Now double-click on the function in the bottom list. Shazam, you are transported right to the cpp file where you need to fill in the code. Let's do something easy. Just add the line:

AfxMessageBox("Stupid Text");

Compile, build and run your application (just press Ctrl-F5). If you press the button, you should see a message box pop up when you press the new button. (There are some Afx... functions that are useful. I think the message box one is the most useful out of all of them. It is instant feedback).

That was dead simple. You see how to add message handlers now (like for the button click), but you need at least one more vital bit of information to make a useful dialog box. How to use the automatic data handling stuff in MFC. This is best described by going back to the code again. Bring up the resource editor one more time and this time add an Edit Box to your dialog. Again right click on it and give it a nice and friendly ID. Hit Ctrl-W and bring up the class wizard. This time go to the second Tab. Here is where you add member variables that are associated with the controls on your dialog. Double click on the Edit Box's ID. You now have the choice to add a variable to your project. Give it a name like m_strMessage since it will be a string for our message box. Make sure the data type selected is CString there at the bottom. Press OK. And press it again to get out of the class wizard.

When you add a member variable to a dialog like this, you can set the value to that in the control of the dialog by calling


Likewise you can change the data displayed in the dialog to represent your variable's value by calling


Let's put this to use and finish up this lesson. Go towards the end of your OnInitDialog() function and in it put the two lines:

m_strMessage = "Initial text";

Then go to the function which is called when the button is pressed and replace the AfxMessageBox() line with these two lines:


Ok, we are done. What we just did was set the initial text in the edit box to "Initial text" with the UpdateData(FALSE). Then when the button is pressed the text in the message box is displayed since we get the text from the dialog with UpdateData(TRUE).

By playing around and by reading the help (or a good book) you will learn how to use the other controls. On of the trickiest to figure out with out help is the slider bar. If you use one of these you will have to handle the dialog's scroll messages. That's just a hint for you.


  • reasonable

    Posted by arun on 04/06/2012 11:20pm

    Thank you very much

  • Good Article

    Posted by TE1 on 06/24/2006 12:13pm

    I created a dialog box using MFC. I would like to make so the only option the user has is the dialog box. I have been trying to create a system modal dialog box but can't seem to get it right. If I could just get control of mouse when the dialog opens and limit it to the active dialog until the box is complete then return control. Any ideas? I am really new to programming.

  • Visible

    Posted by gopal.srini on 04/02/2006 12:37pm

    Hi, As suggested by the document, i unchekded the Visible checkbox and still when i ran the application with ctrl + F5 i was able to see the Dialog Application window on the screen.. How??

  • awesome information

    Posted by gap001 on 10/06/2005 10:02pm


  • visual C++

    Posted by Legacy on 01/10/2004 12:00am

    Originally posted by: abd rahman

    i would like to know how to load static image and zoom using dialog-based application?

  • saving data in a dialog aplication

    Posted by Legacy on 12/29/2003 12:00am

    Originally posted by: Gaby

    I need to save and restore the information introduced by the user in the dialog, because i need to make a data base whit that information.

    please help me

  • Numbers...?

    Posted by Legacy on 10/13/2003 12:00am

    Originally posted by: darkhitman

    Ok how can I get a message box that will accept numbers WITHOUT an assertion error? The tutorial was good, though.

  • very Good information

    Posted by Legacy on 10/06/2003 12:00am

    Originally posted by: Anwar

    Great stuffs ..this will surely help new bees a lot

  • A gift to 2 all

    Posted by Legacy on 10/01/2003 12:00am

    Originally posted by: aXe

    Here�s a complete tut about MFC.

  • Want help on edit box!

    Posted by Legacy on 09/05/2003 12:00am

    Originally posted by: sara imtiaz

    i want help on edit box, i want to use MFC dialog based applications to implement hangman in vc++. i want that there should be a code such that when user enter wrong letter, it should not be displayed in edit box and message should be generated that "u r wrong"

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Despite the popularity of cloud software, many companies are reluctant to move their contact centers to the cloud. This white paper breaks down cloud architecture, the benefits of cloud and the difference between cloud and on-premise systems. It's a helpful resource for companies who are curious about cloud contact center software but have reservations. It offers practical explanations, extensive research and a guide to selecting the right cloud contact center vendor.

  • On-demand Event Event Date: March 23, 2017 As you adopt the use of cloud services, whether in public/IaaS, SaaS or hybrid environments, the attack surface expands and, if breached, the costs increase exponentially. This session is designed to help IT and security leaders understand and address the unique challenges that enterprises typically face when they deploy their applications in the public cloud. It summarizes the areas that the public cloud vendors typically take care of and highlights the areas …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date