Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

The Structure of an MFC Application

The Structure of an MFC Application

If you're going to use Microsoft Foundation Classes (MFC) in your developing, you have to know what the classes do and how to use them. This tip, which is excerpted from Informit, discusses the structure of MFC applications. It comes from Sams Teach Yourself MFC in 24 Hours, by Michael Morrison, published by Que.

Regardless of whether an application uses MFC's document/view architecture, all MFC applications have a few things in common:

  • An application object
  • A main frame window
  • User interface elements
  • Application data
  • Resources

The application object is the foundation for all MFC applications. It works behind the scenes to orchestrate the startup, execution, and termination of an application. The application object is responsible for creating a main frame window, which serves as the basis for an application's user interface. The main frame window houses the user interface elements such as buttons and toolbars that are used to carry out application tasks.

Practically all MFC applications operate on some type of data, whether it is text, graphics, database records, or custom application-specific data. This data is typically viewed and manipulated within the client area of the main frame window. In addition to the application-specific data involved in the primary function of an application, there is also special data associated with all applications. This data comes in the form of application resources such as icons and menus. Application resources are created and associated with an application during development, then compiled directly into an application's executable .EXE file.

Inside the Application Object
The purpose of the application object is to control the initialization, running, and termination of an application. Aside from encapsulating the Win32 WinMain() function for an application, the application object provides some important member functions that can be used to control the execution of an application:

  • InitInstance()
  • Run()
  • OnIdle()
  • ExitInstance()

WinMain() still runs the show behind the scenes in an MFC application. However, the application object neatly provides access to important sections of the WinMain() function through its member functions. The next few sections examine each of these member functions.

The InitInstance() Member Function
The InitInstance() member function is called near the start of WinMain() to perform instance initialization, which occurs for each instance of an application that is executed. InitInstance() is without a doubt the most important member function in application object because practically all applications are concerned with instance initialization. In fact, you are required to override the InitInstance() member function in your own application classes. Typically, you override InitInstance() to construct your main window object and set the inherited m_pMainWnd data member to point to that window.

The Run() Member Function
In an application built solely with the Win32 API, the WinMain() function spends the vast majority of its time in a message loop that waits for and processes incoming messages. This message loop is ultimately responsible for receiving messages and dispatching them to the main frame window of an application. In an MFC application, WinMain() delegates the message loop to the Run() member function. Run() loops continually wait for a message. If no messages are available, which is often the case, Run() calls the OnIdle() member function to perform idle processing. When the application is terminated, Run() calls the ExitInstance() member function.

The OnIdle() Member Function
The OnIdle() member function is used to perform idle processing that isn't immediately critical to the operation of an application. You typically override OnIdle() to perform background tasks such as a lengthy calculation. The default version of OnIdle() updates the state of user interface elements such as menu items and toolbar buttons.

The ExitInstance() Member Function
The ExitInstance() member function is called when an instance of an application is terminated. ExitInstance() is called by the Run() member function, usually in response to the user closing an application. You typically override ExitInstance() to perform the cleanup of objects created throughout the execution of an application. However, keep in mind that MFC automatically cleans up standard objects such as documents, views, and frame windows.

To read more of this excerpt, click over to InformIT. Registration is required for InformIT, but it's free.

To learn more about MFC, check out the selection at our eMarketplace.

Dig Deeper on Windows client management