Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt

Use this file to discover all available pages before exploring further.

The Microsoft Foundation Class (MFC) Library provides an object-oriented C++ wrapper over much of the Win32 API and COM interfaces, enabling developers to build rich Windows desktop applications more rapidly than raw Win32 programming allows. MFC has been a cornerstone of Windows desktop development since 1992 and remains fully supported in modern Visual Studio — though it no longer receives new features. It is most suitable for maintaining existing MFC codebases or rapidly building complex UI-driven applications that leverage features such as toolbars, docking panes, ribbon bars, and Office-style interfaces.
The MFC library continues to be supported in Visual Studio, but new features and documentation updates have ceased. For new projects, consider WinUI 3, Qt, or raw Win32 with DirectX depending on your needs.

What MFC Provides

Window & Control Wrappers

Classes like CWnd, CButton, CEdit, CListCtrl wrap every Win32 window and common control, providing C++ encapsulation with virtual function dispatch.

Document-View Architecture

CDocument and CView separate data from presentation, enabling multiple simultaneous views on the same document with built-in serialization support.

Message Maps

The BEGIN_MESSAGE_MAP / END_MESSAGE_MAP macro system routes Windows messages (WM_PAINT, WM_LBUTTONDOWN, etc.) to C++ member functions without a manual switch statement.

GDI Wrappers

CDC, CPen, CBrush, CFont, and CBitmap wrap GDI device contexts and drawing objects with RAII semantics.

Dialog & Resource Support

CDialog, CFileDialog, CColorDialog wrap Windows common dialogs; CDataExchange (DDX/DDV) binds dialog controls to member variables automatically.

Collections & Serialization

CArray, CList, CMap provide typed collections. CArchive and Serialize() enable binary object persistence with versioning support.

The MFC Class Hierarchy

The entire MFC library descends from CObject, which provides runtime class identification, serialization, and diagnostic support. Below is a simplified view of the most important inheritance chains:
CObject
├── CCmdTarget          (command routing — responds to menus, accelerators)
│   ├── CWinThread      (thread abstraction; CWinApp extends this)
│   │   └── CWinApp     (application object — exactly one per process)
│   ├── CDocument       (application data model)
│   └── CWnd            (base for all windows)
│       ├── CFrameWnd   (top-level SDI frame window)
│       │   └── CMDIFrameWnd  (MDI main frame)
│       ├── CView       (client area of a frame; renders the document)
│       │   ├── CScrollView
│       │   ├── CFormView
│       │   └── CEditView
│       ├── CDialog     (modal and modeless dialogs)
│       └── CControlBar (toolbars, status bars, docking panes)
└── CDocTemplate        (ties CDocument, CView, CFrameWnd together)
    ├── CSingleDocTemplate  (SDI)
    └── CMultiDocTemplate   (MDI)

Document-View Architecture

The document-view pattern is the organizing principle of MFC applications. Documents store data; views render it. Multiple views can display the same document simultaneously, and the framework synchronizes them automatically.
1

CDocument — Data Model

Stores the application data. Implements Serialize() for reading/writing to CArchive. Calls UpdateAllViews() to notify views when data changes.
class CMyDocument : public CDocument {
    DECLARE_DYNCREATE(CMyDocument)
public:
    CString m_text;    // Application data

    virtual void Serialize(CArchive& ar) {
        if (ar.IsStoring())
            ar << m_text;
        else
            ar >> m_text;
    }
};
2

CView — Presentation Layer

Renders the document in the client area. Receives OnDraw() calls from the framework. Calls GetDocument() to access data.
class CMyView : public CView {
    DECLARE_DYNCREATE(CMyView)
public:
    virtual void OnDraw(CDC* pDC) {
        CMyDocument* pDoc = (CMyDocument*)GetDocument();
        if (pDoc)
            pDC->TextOut(10, 10, pDoc->m_text);
    }
};
3

CFrameWnd — Window Shell

Hosts the view, provides the menu bar, toolbar, and status bar. Handles frame-level messages and command routing.
4

CWinApp — Application Entry Point

Replaces main()/WinMain(). Initializes the application, registers document templates, and runs the message pump.
class CMyApp : public CWinApp {
public:
    virtual BOOL InitInstance() {
        CSingleDocTemplate* pTemplate = new CSingleDocTemplate(
            IDR_MAINFRAME,
            RUNTIME_CLASS(CMyDocument),
            RUNTIME_CLASS(CMainFrame),
            RUNTIME_CLASS(CMyView));
        AddDocTemplate(pTemplate);
        OnFileNew();
        return TRUE;
    }
};
CMyApp theApp; // Global application object

Message Maps

MFC replaces the Win32 WndProc with a declarative message map system. Each class declares which Windows messages it handles using macros:
// In MyView.h
class CMyView : public CView {
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnPaint();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};

// In MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
    ON_WM_LBUTTONDOWN()
    ON_WM_PAINT()
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

void CMyView::OnLButtonDown(UINT nFlags, CPoint point) {
    CString msg;
    msg.Format(_T("Clicked at (%d, %d)"), point.x, point.y);
    AfxMessageBox(msg);
    CView::OnLButtonDown(nFlags, point);
}

void CMyView::OnPaint() {
    CPaintDC dc(this); // RAII device context
    dc.TextOut(20, 20, _T("Hello from MFC!"));
}
The afx_msg qualifier is a hint to developers (it expands to nothing) and helps tools like IntelliSense recognize message handler declarations. The ON_WM_* macros in the message map connect Windows messages to the corresponding member function signatures.

When to Use MFC vs Alternatives

  • You are maintaining or extending an existing MFC codebase
  • You need Office-style UI quickly: ribbon bars, docking panes, property grids
  • Your team already has MFC expertise and tight deadlines
  • You require deep integration with legacy COM/OLE components
  • You need maximum control over window creation, message handling, and painting
  • Your application is a service, background process, or lightweight utility with minimal UI
  • You want zero framework overhead and a minimal binary footprint
  • You are building a new desktop application for Windows 10/11
  • You want modern Fluent Design controls, touch support, and accessibility
  • You need compositing and GPU-accelerated rendering without MFC overhead

Simple MFC Window Example

Here is a minimal MFC application that creates a frame window and displays text:
#include <afxwin.h>   // Core MFC headers

// ---- View ----
class CMyView : public CView {
    DECLARE_DYNCREATE(CMyView)
    DECLARE_MESSAGE_MAP()
public:
    virtual void OnDraw(CDC* pDC) override {
        pDC->SetBkMode(TRANSPARENT);
        pDC->TextOut(20, 20, _T("Hello from MFC!"));
    }
};
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
END_MESSAGE_MAP()

// ---- Frame ----
class CMyFrame : public CFrameWnd {
public:
    CMyFrame() {
        Create(nullptr, _T("My MFC App"),
               WS_OVERLAPPEDWINDOW, rectDefault);
    }
    DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyFrame, CFrameWnd)
END_MESSAGE_MAP()

// ---- Application ----
class CMyApp : public CWinApp {
public:
    virtual BOOL InitInstance() override {
        CMyFrame* pFrame = new CMyFrame();
        m_pMainWnd = pFrame;
        pFrame->ShowWindow(SW_SHOW);
        pFrame->UpdateWindow();
        return TRUE;
    }
};

CMyApp theApp; // Global application object — MFC entry point
To compile MFC applications, your project must set Use of MFC to Use MFC in a Shared DLL or Use MFC in a Static Library in Project Properties → General. The MFC header <afxwin.h> replaces <windows.h>.

Build docs developers (and LLMs) love