MFC › 3. Document/View Architecture
SDI and MDI Applications
Single vs multiple document interfaces and templates.
intermediate #mfc#sdi#mdi#cmultidoctemplate#cmdiframewnd#cmdichildwnd#window-menu
Explanation
Both styles use document/view. They differ in how frames are arranged:
| SDI (single document) | MDI (multiple document) | |
|---|---|---|
| Template | CSingleDocTemplate |
CMultiDocTemplate |
| Main window | CFrameWnd (also holds the view) |
CMDIFrameWnd (a container, no view) |
| Per-document window | the main frame itself | a CMDIChildWnd inside the main frame |
| Documents open at once | one (File New reuses the window) | many |
| Menu | one menu | two: the main menu (no documents open) and the document menu (a child is active), switched automatically |
MDI details:
- Each
CMultiDocTemplatehas anIDR_...TYPEresource: the menu, icon and document-type string for that document type. Several templates (e.g. text and drawing documents) can be registered, and File New asks which type. - The Window menu (
ID_WINDOW_CASCADE,ID_WINDOW_TILE_HORZ,ID_WINDOW_ARRANGE,ID_WINDOW_NEW) is handled byCMDIFrameWndautomatically. - Get the active child with
MDIGetActive(), and from it the view withGetActiveView()and the document withGetActiveDocument(). - The commands also include the “multiple top-level documents” style where each document has its own top-level frame, like Word’s older style.
A modern alternative to MDI child windows is the tabbed MDI of the Feature Pack (EnableMDITabs).
Example
An MDI text editor skeleton using MFC’s built-in CEditView, so no drawing code is needed:
#include <afxwin.h>
#include <afxext.h>
class CTextDoc : public CDocument {
DECLARE_DYNCREATE(CTextDoc)
public:
void Serialize(CArchive& ar) override {
POSITION pos = GetFirstViewPosition();
CEditView* pView = static_cast<CEditView*>(GetNextView(pos));
pView->SerializeRaw(ar); // the edit view reads/writes the text itself
}
};
IMPLEMENT_DYNCREATE(CTextDoc, CDocument)
class CMainFrame : public CMDIFrameWnd {
DECLARE_DYNAMIC(CMainFrame)
protected:
CStatusBar m_status;
int OnCreate(LPCREATESTRUCT lpcs) {
if (CMDIFrameWnd::OnCreate(lpcs) == -1) return -1;
static UINT indicators[] = { ID_SEPARATOR, ID_INDICATOR_CAPS, ID_INDICATOR_NUM };
if (!m_status.Create(this) || !m_status.SetIndicators(indicators, 3)) return -1;
return 0;
}
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()
class CChildFrame : public CMDIChildWnd {
DECLARE_DYNCREATE(CChildFrame)
};
IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)
class CEditorApp : public CWinApp {
public:
BOOL InitInstance() override {
CWinApp::InitInstance();
SetRegistryKey(_T("Demo"));
AddDocTemplate(new CMultiDocTemplate(
IDR_TEXTTYPE, // menu + icon + strings used while a text child is active
RUNTIME_CLASS(CTextDoc),
RUNTIME_CLASS(CChildFrame),
RUNTIME_CLASS(CEditView)));
CMainFrame* pFrame = new CMainFrame;
if (!pFrame->LoadFrame(IDR_MAINFRAME)) return FALSE; // main menu when no document is open
m_pMainWnd = pFrame;
CCommandLineInfo cmdInfo;
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing; // start with no document
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo)) return FALSE;
pFrame->ShowWindow(m_nCmdShow);
pFrame->UpdateWindow();
return TRUE;
}
};
CEditorApp theApp;
Common pitfalls
- In MDI, the main frame is created by hand in
InitInstance(LoadFrame), not through the document template; SDI lets the template create it. - The main frame must be assigned to
m_pMainWndbeforeProcessShellCommand. - Missing
IDR_...TYPEresources give an assertion at start-up (“Failed to load the document template resources”). CMDIChildWndis not a top-level window; itsGetParent()is the MDI client window, not the frame.- Heavy state kept in the main frame doesn’t know about individual documents. Put per-document state in the document.
- Mixing SDI-style assumptions (one document) into MDI code: always ask “which document?” via
GetActiveDocument().
Win32 vs MFC
Win32 has the MDICLIENT window class and a set of WM_MDI* messages you must forward to DefFrameProc / DefMDIChildProc. MFC wraps all of it, including the menu switching. MDI itself has fallen out of fashion: most current applications use tabs or separate top-level windows.
Exercises
1. How do you start an MDI application with no empty document open?
Set cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing; before ProcessShellCommand.