C++. Creating a simple dialog application Windows of type Dialog Based Application by using MFC library

Creating a simple dialog application Windows of type Dialog Based Application by using MFC (Microsoft Foundation Classes) library


1. Run MS Visual Studio.


2. Calling a project in C++

After loading MS Visual Studio you need to call command

File -> New -> Project

In window, which will be opened (Fig. 1) you need to select template “C++” and then in the list of project types you need to select “MFC Application“.

Folder name, where you save the project, you need to set, for example:


Application name set as “MFCApplication1“. If option “Create directory for solution” is set, then will be created another subdirectory named MFCApplication1.

MS Visual Studio. C++ - MFC Application. Selecting of application type "MFC Application"

Fig. 1. Selecting of application type “MFC Application

As a result, the wizard “MFC Application Wizard” will be opened (Fig. 2).

Wizard displays information about current settings of project, which you can leave unchanged by pressing button “Finish“.

Current settings of project are following:

1. Support by the project many documents – MDI Application (MDIMultiple Document Interface). This means that an application can simultaneously keep open several documents, each of which contains a separate file.

2. Without databases support.

3. Without support of embedded documents (container, mini-server, full-server and so on).

4. The appearance of the application – it can be set (tool palette, status bar and so on).

5. The appearance of the application like MS Visual Studio 2008.

6. The project’s style like Visual Studio (design of panels, opening documents etc.).

MS Visual Studio. C++ - MFC Application. Options by default

Fig. 2. Options by default

In our case we select other variant. Do the click on the button “Next >“.

Window “Application Type” will be showed (Fig. 3).

MS Visual Studio. C++ - MFC Project. Window "Application Type"

Fig. 3. Window “Application Type

Let us explain some of the fields of the window.

Application Type:

  1. Single Document – support SDI-application (Single Document Interface – interface with single document). This is interface with a single document, which enables in any time open only one document. For example, editor Notepad.
  2. Multiple Documents – support MDI-application (Multiple Document Interface). In this case you can create (open) simultaneously several documents, every of them is a separate file. For example, editor Microsoft Word of old versions.
  3. Dialog Based – creating an application as simple dialog window, which don’t opens any documents. For example, application “Calculator“.
  4. Multiple top level documents – support of multiple document interface is the same as in the MDI-application. For example, new versions Windows support this mode.

In our case, you need to select “Dialog Based“, as shown at figure 3.

Field “Use of MFC:” determines, how you can form MFC library.

There are two variants. First variant “Use MFC in a shared DLL” proposes, that library MFC is like distributed dynamic linked library (shared DLL). Using DLL as dynamic link library reduces the size of application but complicates the installation of the product.

Second variant “Use MFC in a static library” proposes MFC as a static library. In this case you have a file “.exe” of large size, but there will not be difficulties in the maintenance of applications. In this case, modules of library are connected to “.exe” file statically, therefore the application moves from one to another computer easily.

For our application, it does not matter what variant of MFC library forming you need to select.

After selection “Next >” window “User Interface Features” will be open. In this window is proposed to select decor of frame of main window of application. Frame includes system menu, title string, minimization and maximization buttons, borders of window and so on.

MS Visual Studio. C++ - MFC Application

Fig. 4. Selection of decor of frame of main window of application.

Properties of frame “Main frame styles” are the next:

  1. Thick frame – in this case the borders of window are thickened.
  2. Minimize box – button of minimize the window.
  3. Maximize box – button of maximize the window.
  4. System menu – system menu, which is placed in the top-left corner of window.
  5. Minimized – mode, when the window of application is minimized to an icon.
  6. Maximized – mode, when the window of application is set to full screen.

You can set the selection of options at their own discretion.

In field “Dialog Title“, title of application is set.

After setting up the options of frame you need to click on the “Next >“. As a result, window in Figure 5 will be opened.

MS Visual Studio. C++ - MFC Application. Window of settings

Fig. 5. Window of settings

In the window at Figure 5 are different options of settings of application.

Let us explain some of them.

  1. Context Sensitive Help (HTML) – context help. Menu “Help” of application with options “Index” and “Using Help” will be created.
  2. Active X controls – allows include Active X controls in application.
  3. Automation – allows an application to expose objects for scripts and other applications.
  4. Windows sockets – allows an application have access to Internet using protocols FTP, HTTP.

Do the click on the “Next >“. In the last window of wizard (Fig. 6) you need to confirm names of classes and filenames, which “MFC Application Wizard” creates.

MS Visual Studio. C++ - MFC Application. Request for confirmation of the names of the generated classes

Fig. 6. Request for confirmation of the names of the generated classes

We leave it as it is and do click on “Finish“. The program code of generated classes will be formed. Application Form will look as shown in Fig. 7.

MS Visual Studio. C++ - MFC Project. Application form after executing "App Wizard"

Fig. 7. Application form after executing “App Wizard

Now you can run the application and analyze it work.


3. Classes, that are created in the application

As a result of above actions, will be created two classes with names “CMFCApplication1App” and “CMFCApplication1Dlg“.

Class CMFCApplication1App is inherited from CWinApp. This class is the class of application in general.

Class CMFCApplicationDlg is the class of dialog for application.

After creating the application, in Solution Explorer the files of classes are created (Fig. 8).

Class “CMFCApplication1App” responds to files “MFCApplication1.h” and “MFCApplication1.cpp“.

Class CMFCApplication1Dlg responds to files “MFCApplication1Dlg.h” and “MFCApplication1Dlg.cpp“.

MS Visual Studio. C++ - MFC Project. Application files in Solution Explorer

Fig. 8. Application files in Solution Explorer

Listing of header file “MFCApplication1.h” for application is following:

// MFCApplication1.h : main header file for the PROJECT_NAME application

#pragma once

#ifndef __AFXWIN_H__
      #error "include 'stdafx.h' before including this file for PCH"

#include "resource.h"            // main symbols

// CMFCApplication1App:
// See MFCApplication1.cpp for the implementation of this class

class CMFCApplication1App : public CWinApp
    // Overrides
      virtual BOOL InitInstance();
    // Implementation

extern CMFCApplication1App theApp;

In the above listing between directive

#ifndef ... #endif

realized preventing of re-use of the same file name headers that can even be placed in different folders.

Next is the declaration of class CMFCApplicationApp, which is inherited from class CWinApp.

Class CWinApp includes most of the functionality, which are needed to application.

Wizard “AppWizard” has generated several functions for inherited class, which overload own functions of base class.

Virtual function InitInstance() is overloaded in the class.



means, that is used message map.

The realization of class CMFCApplicationApp is given in file “CMFCApplication1App.cpp“.

Listing of file “CMFCApplication1App.cpp” is following.

#include "stdafx.h"
#include "MFCApplication1.h"
#include "MFCApplication1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW

// CMFCApplication1App

      ON_COMMAND(ID_HELP, &CWinApp::OnHelp)

// CMFCApplication1App construction
      // support Restart Manager
      m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;
      // TODO: add construction code here,
      // Place all significant initialization in InitInstance

// The one and only CMFCApplication1App object
CMFCApplication1App theApp;

// CMFCApplication1App initialization
BOOL CMFCApplication1App::InitInstance()
      // InitCommonControlsEx() is required on Windows XP if an application
      // manifest specifies use of ComCtl32.dll version 6 or later to enable
      // visual styles. Otherwise, any window creation will fail.
      InitCtrls.dwSize = sizeof(InitCtrls);
      // Set this to include all the common control classes you want to use
      // in your application.
      InitCtrls.dwICC = ICC_WIN95_CLASSES;

      // Create the shell manager, in case the dialog contains
      // any shell tree view or shell list view controls.
      CShellManager *pShellManager = new CShellManager;

      // Standard initialization
      // If you are not using these features and wish to reduce the size
      // of your final executable, you should remove from the following
      // the specific initialization routines you do not need
      // Change the registry key under which our settings are stored
      // TODO: You should modify this string to be something appropriate
      // such as the name of your company or organization
      SetRegistryKey(_T("Local AppWizard-Generated Applications"));

      CMFCApplication1Dlg dlg;
      m_pMainWnd = &dlg;
      INT_PTR nResponse = dlg.DoModal();
      if (nResponse == IDOK)
            // TODO: Place code here to handle when the dialog is
            // dismissed with OK
      else if (nResponse == IDCANCEL)
            // TODO: Place code here to handle when the dialog is
            // dismissed with Cancel

      // Delete the shell manager created above.
      if (pShellManager != NULL)
            delete pShellManager;

      // Since the dialog has been closed, return FALSE so that we exit the
      //   application, rather than start the application's message pump.
      return FALSE;

In the above listing of greatest interest is the function InitInstance ().

In function InitInstance() the application window is created. Is described the variable dlg of type of application window.


m_pMainWnd = &dlg;

means, that given window of application is the main window of all windows, which can be created in application.

Next is calling the function DoModal(), which opens an application window. After the call, the system checks for the value that is returned after pressing on the “OK” or “Cancel“.

Related themes