[C++ – MFC] How MFC Works (1) – SDI

MFC (Microsoft Foundation Classes) is a library, mainly for GUI. But the MFC is more than that. It might be more correct to say that MFC is a host system for MFC applications. Therefore it is crucial to understand how MFC organizes application objects(views, documents, and frame windows) in your solution. Let’s find out how MFC works using a simple SDI application.

In this post I am going to create a simple text editor (MiniNotePad).  


1. Create a project

MFC Application Wizard generates a lot of code for you. As a simple text editor, there’s 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 (MiniNotePad)
  3. Click “OK” and the you can see the welcome screen. Click “Next”
  4. In the “Application Type” window, select “Single Document“. Also select the “MFC Style” as a project style. 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 “CMiniNotePadView” 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 Structure of the Project

The MFC Wizard creates a lot of files for you. Open the “Windows Explorer” and move to the location where you created the project.

The root folder is the solution folder. You can find at least 2 files in the root folder.

  • *.sln: Solution File (a list of projects and their dependencies, Project Configurations …)
  • *.suo: Solution User Option File (Tasks, breakpoints …)

Now, we have only one project “MiniNotePad”. In the project folder, you can see a lot of files and the “res” folder.

The project related files are:

  • *.vcxproj: Visual C++ Project File (MSBuild xml file, which includes the list of all files in the project)
  • *.vcxproj.filters: This file is used by Visual Studio to organize the files logically (header files, source files, or resource files)
  • *.vcxproj.user: The user file stores the user specific properties such as debugging or deployment settings.


3. Logical Structure of the Project

All source files and header files are also located in the project folder. It might be really confusing if you need to work with this structure. But in the solution explorer, the files are organized in the a couple of predefined locations. (The file is located according to the setting in the “*.vcxproj.filters” file)

  • Header Files: *.h files
  • Resource Files: icons, images, *.rc (main resources), and *.rc2 (additional resources) files
  • Source Files: *.cpp files


4. Application Class

The main class you can start to look at is the Application class derived from “CWinApp“.

class CMiniNotePadApp : public CWinAppEx { }

From VS 2008, the application is not directly inherited from “CWinApp“. It uses another middle layer class “CWinAppEx“. The “CWinAppEx” class handles the application state, saves the state to the registry, loads the state from the registry.

By default, you application class already overrides several virtual functions: “InitInstance()” and “ExitInstance()“.

BOOL CMiniNotePadApp::InitInstance()
  // call base class's InitInstance

  // Initialize libraries and controls depending on the project settings:
  // OLE, menu, toolbar, keyboard, tooltips ...

  // Standard initialization
  SetRegistryKey(_T("Local AppWizard-Generated Applications"));

  // 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;

  // Parse command lines and Dispatch commands
  // specified on the command line

  // The window has been initialized, so show and update it

  return TRUE;

The important part of the code is where the document template is created. You can specify the document, the frame window, and the view classes to be used.

Also you can see the “SetRegistryKey()” function. It specify the registry key for application related settings.  The default value is “Local AppWizard-Generated Applications” and the settings will be saved in the “HKEY_CURRENT_USER/Software/Local AppWizard-Generated Applications/YourAppName“. In our “MiniNotePad” application, the settings are saved in the “HKEY_CURRENT_USER/Software/Local AppWizard-Generated Applications/MiniNotePad


5. Document Class

The document class is inherited from “CDocument” class.

It provides 2 basic virtual functions you might want to override:

class CMiniNotePadDoc : public CDocument
  virtual BOOL OnNewDocument();
  virtual void Serialize(CArchive& ar);


6. View Class

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

The most important function might be the “GetDocument()“. When you work in the view, you need to access the document object for data. The “GetDocument()” function returns the pointer to a document object.

class CMiniNotePadView : public CEditView
  CMiniNotePadDoc* GetDocument() const;


7. How does the Program Work

Go to the “Class” View and expand the “Global Functions and Variables”. You can see the “theApp” instance.

CMiniNotePadApp theApp; // MiniNotePad.cpp file

MFC libraries takes care of the application startup process. Once the application object is created, the “WinMain” calls the “InitInstance()” function of the application object. And then “CWinApp::Run()” function is called and application is waiting for any message to be handled.

When a user closes an application, the “ExitInstance()” function of the application object is called and the application is terminated.

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