MFC › 1. Getting Started
MFC Project Types and the Wizard
SDI, MDI, dialog-based and what the generated files contain.
Explanation
In Visual Studio 2022 choose Create a new project → MFC App (requires the workload Desktop development with C++ plus the optional component C++ MFC for latest v143 build tools). The wizard asks for an application type:
| Type | Result | Use for |
|---|---|---|
| Single document (SDI) | one frame + document + view | editors working on one file |
| Multiple documents (MDI) | main frame with child windows | editors with several files |
| Dialog based | one dialog is the whole app | tools, utilities, settings apps |
| Multiple top-level documents | each document in its own top-level frame |
Other choices: Project style (MFC standard, or Visual Studio/Office style with the Ribbon), Use of MFC (shared DLL needs the MFC redistributable, static embeds MFC in your .exe), and Unicode.
A generated SDI project contains:
| File | Purpose |
|---|---|
MyApp.h / .cpp |
CMyApp : CWinApp, InitInstance, the global theApp, about box |
MainFrm.h / .cpp |
CMainFrame, the main window with toolbar and status bar |
MyAppDoc.* |
CMyAppDoc : CDocument (your data) |
MyAppView.* |
CMyAppView : CView (display and input) |
Resource.h, MyApp.rc |
resource IDs and the resource script (menus, dialogs, icons, strings) |
pch.h/.cpp (framework.h, older stdafx.h) |
precompiled header: #include <afxwin.h> etc. |
res\MyApp.rc2 |
resources edited by hand, not by the editors |
Resources are compiled by the resource compiler into the .exe; Resource.h maps names like IDD_MYAPP_DIALOG to numbers.
Example
The heart of what a dialog-based wizard project generates (shortened). The dialog template IDD_MYAPP_DIALOG lives in the .rc file:
#include <afxwin.h>
#include <afxcmn.h> // common controls
// #include "Resource.h" // the wizard's resource IDs (IDD_MYAPP_DIALOG ...)
class CMyDlg : public CDialog {
public:
enum { IDD = IDD_MYAPP_DIALOG };
explicit CMyDlg(CWnd* pParent = nullptr) : CDialog(IDD, pParent) {}
protected:
HICON m_hIcon = nullptr;
BOOL OnInitDialog() override {
CDialog::OnInitDialog();
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
SetIcon(m_hIcon, TRUE); // big icon
SetIcon(m_hIcon, FALSE); // small icon
return TRUE; // TRUE: let the system set focus to the first control
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
END_MESSAGE_MAP()
class CMyApp : public CWinApp {
public:
BOOL InitInstance() override {
INITCOMMONCONTROLSEX icc = { sizeof(icc), ICC_WIN95_CLASSES };
InitCommonControlsEx(&icc); // needed to use the common controls with visual styles
CWinApp::InitInstance();
SetRegistryKey(_T("MyCompany")); // settings go to HKCU\Software\MyCompany
CMyDlg dlg;
m_pMainWnd = &dlg;
dlg.DoModal(); // the dialog is the application
return FALSE; // FALSE: do not start the message loop afterwards
}
};
CMyApp theApp;
Common pitfalls
- If the MFC component isn’t installed,
afxwin.his missing: run the Visual Studio Installer and add C++ MFC. - Do not edit
Resource.hby hand; the resource editor rewrites it. Edit through the editor, or the.rctext file carefully. - Static vs DLL MFC: the shared DLL gives smaller .exe files but requires the Visual C++ redistributable on the target; static linking is self-contained.
- The wizard’s Use Unicode should stay on. MBCS MFC libraries are separate, optional and deprecated.
- Mixing project settings (precompiled header on some files only) causes the famous “unexpected end of file while looking for precompiled header” error: put
#include "pch.h"first in each.cpp. - Class Wizard (Ctrl+Shift+X) adds message-map entries and handler stubs for you; hand-edits between its
//{{AFX...style markers in old projects can be overwritten.
Legacy vs modern
Older projects use stdafx.h, MBCS strings and the afx_msg wizard comments. Visual Studio 2019+ produces pch.h, framework.h, Unicode and the Feature Pack (ribbon, docking panes): see Ribbon and the Feature Pack.
Exercises
1. Why does InitInstance of a dialog-based app return FALSE?
DoModal already ran the whole interaction. Returning FALSE tells MFC to exit immediately instead of entering the application’s message loop, which would have no window.