[C++ – MFC] How MFC Works (2) – MDI

MDI (Multiple Document Interface) uses the same architecture (Application, Template, Document, and View) as SDI does. Because MDI requires to open/manipulate multiple documents, there are some complications you might encounter. In this post, I am going to create a simple text editor (“MultiNotePad”) that can open multiple text files.  


1. Create a Project

MFC Application Wizard generates a lot of code for you. When creating a simple text editor, there is even no code you need to add.

  1. New Project -> Visual C++ -> MFC -> MFC Application
  2. Select the location and type the name of a solution (MultiNotePad)
  3. Click “OK” and the you can see the Welcome screen. Click “Next”
  4. In the “Application Type” window, select “Multiple Documents” (selected by default). And select the “MFC Style” as a project style. Finally, uncheck the “Use Unicode libraries”. Click “Next”
  5. Click “Next” until the “Generated Classes” window. The wizard is very intuitive. You can modify the settings as you want
  6. In the “Generated Classes” window, select the “CMultiNotePadView” class. Change the base class from the “CView” to “CEditView“. It creates a text view rather than an empty view
  7. Click “Finish”


2. Physical/Logical Structures of the Project

Basically, the physical and logical structure of a MDI project is the same as those of a SDI project.


3. Application Class

The application class is derived from “CWinAppEx“.

class CMultiNotePadApp: public CWinAppEx { }

Now let’s look at the “InitInstance()” function. Let’s compare how MDI and SDI create document template object.

BOOL CMultiNotePadApp::InitInstance()
  // Register the application's document templates.
  CMultiDocTemplate* pDocTemplate;
  pDocTemplate = new CMultiDocTemplate(
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
  if (!pDocTemplate)
    return FALSE;

SDI is here:

BOOL CMiniNotePadApp::InitInstance()
  // Register the application's document templates.
  CSingleDocTemplate* pDocTemplate;
  pDocTemplate = new CSingleDocTemplate(
    IDR_MAINFRAME, // menu and toolbar
    RUNTIME_CLASS(CMainFrame),       // main SDI frame window
  if (!pDocTemplate)
    return FALSE;

There are 2 differences:

  • CMultiDocTemplate vs. CSingleDocTemplate
  • The frame window parameter: CChildFrame vs. CMainFrame

In SDI, the main frame window is linked to a single view. Meanwhile in MDI, each child frame window is mapped to a different view (When you use an application you need to active one of the views).


4. Document Class

The document class is also inherited from “CDocument” class.


5. View Class

The view class is inherited from “CView” class. In our application, “CEditView” is used as a base class.


6. How does the Program Work

MDI applications work in the same way as SDI application do.

Once the application object is created, the “WinMain()” calls the “InitInstance()” and then the “Run()” function.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s