The main application class encapsulates the initialization, running, and termination of a Windows application. A Microsoft Foundation Class Library Windows application must contain one (and only one) object of a class derived from CWinApp. This class has several important member functions that you can override:
InitInstance
Windows allows you to run more than one copy, or “instance,” of the same application. InitInstance is called every time a new instance of the program starts. It must be overridden in order to create a main window and thus start the application. It is the most important member function of the class.
InitApplication
This function is called when the first instance of a program starts. The default version does nothing, but you can override it if you need special processing for the first instance only.
ExitInstance
This function is called each time an application instance terminates, usually as a result of the user quitting the application. You can override ExitInstance if you need special cleanup processing, such as closing of disk files or deallocating memory used during program execution.
OnIdle
The default version of OnIdle does nothing, but your overridden function can perform background tasks when no messages are being processed.
For a typical Windows application, you need only override InitInstance in a class derived from CWinApp. Then you construct a static object of the CWinApp- derived class.
Program Initialization–The Foundation Classes vs. Native Windows
Native Windows always starts your application by calling the WinMain function, which, in turn, creates a main window. Like other windows, this main window has an associated WndProc function that processes the window's messages.
WndProc functions are associated with windows by means of a “window class registration” procedure. The main window is registered in the WinMain function, but other window classes can be registered anywhere in the application. Registration depends on a structure that contains a pointer to the WndProc function together with specifications for the cursor, background brush, and so forth. The structure is passed as a parameter in a call to the function RegisterClass, which returns a string that is used in the window creation process. Thus a registration class can be shared by multiple windows.
The Microsoft Foundation classes include a special version of WinMain that calls member functions of class CWinApp. An object of a CWinApp-derived class is constructed prior to the execution of WinMain. The CWinApp constructor ensures that there is only one object of the class. The CWinApp member function Run intercepts Windows messages in order to do key code translation and other special processing. The Run function also calls OnIdle and ExitInstance.
In a Microsoft Foundation Class Library Windows application, there is only one WndProc function. This built-in function processes all incoming Windows messages through the message map. Most of the time, you won't have to worry about the registration of Windows classes because the Microsoft Foundation Class Library registers objects for you with default parameters. If you do need a special cursor, background brush, or other feature not supported by the window Create function, you can register your own Windows registration class with the global AfxRegisterWndClass function.