CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Visual C++ – How to create modal dialog and modeless dialogs using MFC ?

MFC (Microsoft Foundation Classes) provides classes to deal with dialogs. Dialog boxes are very useful to take user inputs, displaying and allowing the users to change configuration settings, displaying messages or warnings or errors etc.,.

Dialogs are classified into two types depending on their behavior. One is modal dialog and another one is modeless dialog.

The main difference between modal and modeless dialogs is, modal dialog doesn’t allow the user to access any other part in an application window until the modal dialog is closed. Modeless dialog allow the users to access its application window without closing the dialog itself.

In this article we are going to look at the “Visual C++” code to create both modal and modeless dialogs using MFC class library.

Lets start with CDialog class. This is the base class for all dialog classes in MFC.

To create a dialog, first we need to create a template for it using dialog editor and save the template into “Visual C++” project’s resource file. I am not going to explain on creating a dialog template; but I am going to use an existing template from your “Visual C++” project. Whenever you create a windows based “Visual C++” project using Visual Studio IDE, Visual Studio creates a IDD_ABOUTBOX resource template. I am going to use this template in this article to create both, modal and modeless dialogs.

Creating a modal dialog is simple. Once the modal dialog is displayed, whole control goes to the modal dialog; and the main application window can not be accessible until the modal dialog is closed. This is very useful while taking inputs from the users.

Step 1. To create a modal dialog, first we need to create an instance for CDialog class.

CDialog dlg(IDD_ABOUTBOX);

Observe that I have used IDD_ABOUTBOX resource template while creating an instance for CDialog class.

Step 2. Once we have CDialog‘s instance, we can call its member function DoModal.

dlg.DoModal();

DoModal member function is an important function for creating a modal dialog. DoModal function will create the dialog based on the given resource template and display the dialog box. Observe that the function will return only when the dialog is closed by you or clicked on OK or Cancel button on the dialog. It returns the ID of the button you clicked. For example: if you click on OK button on modal dialog, DoModal will return the IDOK value.

Modeless dialog creation is different than modal dialog creation. Modeless dialog allows to access application window even though modeless dialog is kept open.



Step 1. Create an instance of CDialog class.

CDialog m_dlg;

This should be in class scope; not in function scope. If you declare this in function scope, you will not see the dialog at all. Because you are creating the dialog, but when the function is out of scope the dialog is automatically destroyed.

If this is in class scope, you can use this dialog until the instance of the class is valid.

Step 2. Create dialog window using CDialog‘s Create method. Yes, in Windows programming we need to create a window for each control or dialog or window. We are going to create a window based on the resource template IDD_ABOUTBOX.

CDialog‘s Create function will return FALSE if it is failed to create a dialog. Otherwise it returns TRUE.

m_dlg.Create(IDD_ABOUTBOX);

Step 3. Once the dialog is successfully created show the dialog using CDialog‘s ShowWindow method. ShowWinodw method is used to show or hide the dialog or window based on its argument.

m_dlg.ShowWindow(SW_SHOW);

SW_SHOW is the value tells to show the window. If you want to hide the window you can pass the value SW_HIDE.

Once the variable m_dlg is out of scope, the dialog will be destroyed.

These are the ways we can create modal and modeless dialogs using Microsoft Foundation Classes (MFC) in “Visual C++”.

Here is the full project, available; this code was developed using Visual Studio 2019 and tested in Windows 10 Operating System.

in GitHub

**

Visual C++ – How to create modal dialog and modeless dialogs using MFC ?

One thought on “Visual C++ – How to create modal dialog and modeless dialogs using MFC ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top