DEV Community

Stefor07
Stefor07

Posted on • Edited on

How to develop a basic MFC Application in Visual C++ using a Dialog Box

This guide walks you through creating a basic MFC desktop application using Visual C++, starting from scratch and using a dialog box as the main interface.


📦 Step 1: Create a New Project in Visual Studio

  1. Open Visual Studio.
  2. Go to File > New > Project....
  3. In the template list, select Windows Desktop Wizard.

Select Project Type

  1. Name the project and click OK.
  2. In the next screen:
    • Application type: select Desktop application
    • Check Empty Project to start from scratch.

Select Application Type


⚙️ Step 2: Enable MFC Support

To use MFC (Microsoft Foundation Classes):

  1. Right-click the project in Solution Explorer and select Properties.
  2. Navigate to:
   Project Properties > Configuration Properties > General
Enter fullscreen mode Exit fullscreen mode
  1. Set Use of MFC to:
    • Use MFC in a Static Library.
  2. Click Apply, then OK.

🧱 Step 3: Create the Main Application Class (Main)

This class represents the entry point for the application.

➤ Add the Header File

  1. Right-click on Header Files > Add > New Item
  2. Choose Header File (.h) and name it Main.h.
   #pragma once
   #include "afxwin.h"

   class Main : public CWinApp {
   public:
       BOOL InitInstance();
   };
Enter fullscreen mode Exit fullscreen mode

➤ Add the Source File

  1. Right-click on Source Files > Add > New Item.
  2. Select C++ File (.cpp) and name it Main.cpp.
   #include "afxwin.h"
   #include "Main.h"
   #include "CMainDlg.h"  // To be created later

   Main app;

   BOOL Main::InitInstance() {
       CMainDlg dlg;
       m_pMainWnd = &dlg;
       dlg.DoModal();
       return TRUE;
   }
Enter fullscreen mode Exit fullscreen mode

🧩 Step 4: Add a Dialog Box Resource

  1. Right-click Resource Files > Add > Resource.
  2. Select Dialog and click New.
  3. A dialog template named IDD_DIALOG1 will appear in the editor.

You can customize the dialog using the toolbox, adding buttons, labels, etc.

add dialog


🧾 Step 5: Create the Dialog Class

  1. Right-click on the dialog template and select Add Class....
  2. Name the class CMainDlg.
  3. This will generate two files:
    • CMainDlg.h
    • CMainDlg.cpp

dialogbox class wizard

➤ Edit CMainDlg.h

Make sure it includes the necessary header:

#pragma once
#include "afxdialogex.h"

class CMainDlg : public CDialogEx {
public:
    CMainDlg(CWnd* pParent = nullptr); // Constructor

#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_DIALOG1 };
#endif

protected:
    virtual void DoDataExchange(CDataExchange* pDX);

    DECLARE_MESSAGE_MAP()
};
Enter fullscreen mode Exit fullscreen mode

➤ Edit CMainDlg.cpp

Ensure this file includes the resources header and links to the dialog ID:

#include "resource.h"
#include "CMainDlg.h"

IMPLEMENT_DYNAMIC(CMainDlg, CDialogEx)

CMainDlg::CMainDlg(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_DIALOG1, pParent) {}

void CMainDlg::DoDataExchange(CDataExchange* pDX) {
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMainDlg, CDialogEx)
END_MESSAGE_MAP()
Enter fullscreen mode Exit fullscreen mode

🎨 Step 6: (Optional) Enable Windows Visual Styles

To make your application use modern Windows controls:

  1. Right-click the project and select Add > New Item.
  2. Choose XML File, name it app.manifest.
  3. Paste the following into the file:
   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
     <dependency>
       <dependentAssembly>
         <assemblyIdentity 
           type="win32" 
           name="Microsoft.Windows.Common-Controls" 
           version="6.0.0.0" 
           processorArchitecture="*" 
           publicKeyToken="6595b64144ccf1df" 
           language="*" />
       </dependentAssembly>
     </dependency>
     <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
       <security>
         <requestedPrivileges>
           <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
         </requestedPrivileges>
       </security>
     </trustInfo>
   </assembly>
Enter fullscreen mode Exit fullscreen mode

This enables high-DPI support and modern UI styles for controls like buttons.


▶️ Step 7: Run the Application

  1. Build the project (Ctrl + Shift + B).
  2. Run the application with Ctrl + F5.
  3. The dialog box should appear as the main window.

dialog box opened


✅ What's Next?

Now that you have a basic MFC app running with a dialog, you can:

  • Add buttons, text boxes, combo boxes, etc.
  • Handle events like button clicks with ON_BN_CLICKED macros.
  • Store user input using UpdateData(TRUE/FALSE).

📺 Related Video

Win32 Dialog App Tutorial


💬 Questions or improvements? Fork this guide or leave a comment in your repo!

Top comments (0)