A traditional Windows program that is written without the Microsoft Foundation Class Library typically has at least the following five components:
Component | Purpose |
WinMain | Calls initialization function and processes message loop |
InitApplication | Initializes window data and registers one or more Windows registration classes |
InitInstance | Saves instance handle and creates main window |
MainWndProc | Processes messages for main window |
About | Processes messages for “About” dialog box |
A Windows program that is written with the Microsoft Foundation Class Library has a slightly different structure than a traditional Windows program. Much of the functionality that you have to provide yourself in a traditional Windows program, such as the WinMain function and the message loop, is provided automatically by the Microsoft Foundation Class Library. Thus, you have fewer programming tasks when making a Microsoft Foundation-based Windows program.
The following list shows how the parts of a traditional Windows program are replaced by the capabilities of the Microsoft Foundation Class Library.
Windows component | Foundation capability |
WinMain | Provided by Microsoft Foundation Class Library. Instead of writing WinMain yourself, you derive an application class from CWinApp and create an object of that derived class. |
InitApplication | Default implementation provided by Microsoft Foundation Class Library. You can override the InitApplication function of CWinApp to add your own special initialization. |
InitInstance | You typically override the InitInstance function of CWinApp to create the application's main window. |
MainWndProc | Instead of writing a window procedure, you derive a window class from one of the existing Foundation window classes. |
About | Instead of writing a dialog window procedure, you create a CModalDialog object and let it process the user interaction. |
The following list shows the typical steps you will complete to take advantage of the Microsoft Foundation Class Library to write a Windows application:
·To use Microsoft Foundation classes to write a Windows program:
1.Derive your own window class to serve as the main window. You typically derive from the Microsoft Foundation class CFrameWnd or CMDIFrameWnd. (For more information on this topic, see the cookbook section “Deriving Frame Windows.”)
2.Define a message map to associate specified window messages with member functions of your derived window class. For a complete description of how to define message maps, see the cookbook section “Handling Window Messages.”
3.Derive your own application class from CWinApp.
4.Override the InitInstance member function of CWinApp to create a main window.
5.Define an object of your application class. You define the application object as a global variable in the main .CPP file for your program, as shown in the example below. This object will be automatically initialized by the Microsoft Foundation-provided WinMain at program startup.
// in .H file
class CTheApp : public CWinApp
{
// ... class declaration ...
}
// in .CPP file
CTheApp myApp; // define global application object
For an example of how to derive the minimum necessary application and window objects for a Microsoft Foundation-based Windows application, see the sample program files HELLO.H and HELLO.CPP. The following section describes some of the specific tasks that must be done by overridden member functions of the application class. Later sections describe the functions of the window class that can be overridden.