CWinApp: The Application Class
| Overview | How Do I | | Tutorial
The main application class in MFC encapsulates the initialization, running, and termination of an application for Windows. An application built on the framework must have one (and only one) object of a class derived from . This object is constructed before windows are created. (CWinApp is derived from CWinThread, which represents the main thread of execution for your application, which might have one or more threads. In recent versions of MFC, the InitInstance, Run, ExitInstance, and OnIdle member functions are actually in class CWinThread. These functions are discussed here as if they were CWinApp members instead, since the discussion concerns the object's role as application object rather than as primary thread.)
Note Your application class constitutes your application's primary thread of execution. Using Win32 API functions, you can also create secondary threads of execution. These threads can use the MFC library. For more information, see Multithreading Topics.
Like any program for Windows, your framework application has a WinMain function. In a framework application, however, you don't write WinMain. It is supplied by the class library and is called when the application starts up. WinMain performs standard services such as registering window classes. Then it calls member functions of the application object to initialize and run the application. (You can customize WinMain by overriding the CWinApp member functions that WinMain calls.)
To initialize the application, WinMain calls your application object's InitApplication
and InitInstance
member functions. To run the application's message loop, WinMain calls the Run member function. On termination, WinMain calls the application object's ExitInstance
member function. The figure Sequence of Execution below shows the sequence of execution in a framework application.
Sequence of Execution
Note Names shown in bold type in this documentation indicate elements supplied by the Microsoft Foundation Class Library and Visual C++. Names shown in monospaced
type indicate elements that you create or override.