Using MFC for Windows CE as an Application Framework

As useful as MFC is as a basic Windows class library, there's a lot more to it than that. MFC is also a complete application framework. An application framework provides both a structural foundation, upon which many types of applications can be built, and a set of fundamental components that you can use to expand on the structure and adapt it to different purposes. MFC for Windows CE is a framework specifically designed for creating Windows CE applications.

One example of this is the MFC message mapping architecture. Windows CE is a message-driven operating system. This means the operating system communicates with an application window by placing messages in the message queue of the thread that created the window. The thread has a message pump, 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++ switch statement whose purpose is to determine what kind of message it has received, then process the message accordingly.

The MFC framework manages messages a little bit more elegantly. CCommandTarget is the base class for MFC's message mapping architecture. The framework provides a message pump for every class derived from CCommandTarget. These classes use message maps, rather than switch statements, to route messages to their message handler functions. A message map is implemented by a set of simple macros defined in the MFC library. Although you can maintain your classes' message maps and handler functions yourself, the WCE MFC ClassWizard automates this process.

You generally won't derive classes from CCommandTarget directly. There are several classes derived from CCommandTarget, however, from which you will frequently derive classes of your own. One of the most important classes derived from CCommandTarget is the CWnd class. CWnd encapsulates all the behavior of Windows CE windows and all the functionality related to window management. This class and its subclasses are fundamental to almost every Windows CE application.

Another notable class derived from CCommandTarget is CWinApp. Every MFC 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 version of the C/C++ main function. WinMain calls the application object's member functions to initialize, run, and terminate the application. The MFC framework provides the implementation of the WinMain function.

If you're writing an application that has a standard user interface, you will reap enormous benefits from taking advantage of the MFC document/view architecture. This architecture is based on three important classes, CDocument, CView, and CFrameWnd. Document classes, which you derive from CDocument, encapsulate an application's data and are responsible for saving the data and retrieving it from storage. View classes, which you derive from CView or one of it's subclasses, provide the functionality necessary to present the data to the user and to receive user input. A single document may have several views associated with it, so you can present the same data in a variety of formats within the same application. When a user edits a view, the view object notifies the document object. The document object, in its turn, updates all the views associated with it whenever the data changes. The frame window, which you derive from CFrameWnd, provides the frame that contains the views and the command bar.