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
- Open Visual Studio.
- Go to File > New > Project....
- In the template list, select Windows Desktop Wizard.
- Name the project and click OK.
- In the next screen:
- Application type: select Desktop application
- Check Empty Project to start from scratch.
⚙️ Step 2: Enable MFC Support
To use MFC (Microsoft Foundation Classes):
- Right-click the project in Solution Explorer and select Properties.
- Navigate to:
Project Properties > Configuration Properties > General
- Set Use of MFC to:
- Use MFC in a Static Library.
- 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
- Right-click on Header Files > Add > New Item
- Choose Header File (.h) and name it
Main.h
.
#pragma once
#include "afxwin.h"
class Main : public CWinApp {
public:
BOOL InitInstance();
};
➤ Add the Source File
- Right-click on Source Files > Add > New Item.
- 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;
}
🧩 Step 4: Add a Dialog Box Resource
- Right-click Resource Files > Add > Resource.
- Select Dialog and click New.
- A dialog template named
IDD_DIALOG1
will appear in the editor.
You can customize the dialog using the toolbox, adding buttons, labels, etc.
🧾 Step 5: Create the Dialog Class
- Right-click on the dialog template and select Add Class....
- Name the class
CMainDlg
. - This will generate two files:
CMainDlg.h
CMainDlg.cpp
➤ 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()
};
➤ 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()
🎨 Step 6: (Optional) Enable Windows Visual Styles
To make your application use modern Windows controls:
- Right-click the project and select Add > New Item.
- Choose XML File, name it
app.manifest
. - 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>
This enables high-DPI support and modern UI styles for controls like buttons.
▶️ Step 7: Run the Application
- Build the project (Ctrl + Shift + B).
- Run the application with Ctrl + F5.
- The dialog box should appear as the main window.
✅ 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
💬 Questions or improvements? Fork this guide or leave a comment in your repo!
Top comments (0)