Using MFC for Windows CE as an Application Framework

MFC not only is a basic Windows class library, but it also serves as a complete application framework; that is, a set of classes specifically designed for building a working application. Some of the application framework classes encapsulate elements of the Windows CE API, such as CToolbar. Others encapsulate functional MFC concepts such as documents, views, and the application itself.

Among the classes that encapsulate functional MFC concepts are the classes that implement the MFC message mapping architecture. The Windows CE operating system communicates with an application window by placing messages in the message queue of the thread that created the window. The thread implements a message loop, which removes the messages from the queue and dispatches them to the appropriate window procedure. In traditional, non-MFC applications, the window procedure is a large C or C++ switch statement that first determines what kind of message it has received, and then processes the message accordingly. MFC applications use message maps to route windows messages and commands to windows, documents, views, and other objects. A message map is implemented by a set of simple macros defined in MFC.

CCmdTarget is the base class for the MFC message mapping architecture. Any class derived from CCmdTarget is capable of routing messages to message handler functions by means of message maps, rather than switch statements. Although you can maintain your classes’ message maps and handler functions yourself, the MFC for Windows CE ClassWizard automates this process by providing ready-made message maps for the most common messages.

The CWnd class, one of the most important classes derived from CCmdTarget, encapsulates all the behavior of Windows CE windows and all the functionality related to window management. CWnd and its subclasses are fundamental to almost every Windows CE-based application.

Another important class derived from CCmdTarget is CWinApp. Every MFC for Windows CE-based application must have exactly one instance of a class derived from CWinApp. This object, called the application object, is always declared with global scope. This ensures that the application object is constructed before Windows CE calls WinMain, which is the Windows counterpart to the C/C++ main function. WinMain calls the application object’s methods to initialize, run, and terminate the application. The MFC for Windows CE application framework provides the implementation of the WinMain function.

If you are writing an application that has a standard user interface, you can take advantage of the MFC document/view architecture. This architecture is based on the following three classes: