MFC is a programming framework
The various classes in the MFC (Microsoft Foundation class Library) combine to form an application framework that allows programmers to build applications under Windows on this basis, which is a simpler way to compare the SDK. Because the MFC framework, in general, defines the outline of the application and provides a standard implementation of the user interface, what the programmer has to do is to fill in this contour with the specific application of the particular app through a predefined interface. Microsoft Visual C + + provides the tools to do this: AppWizard can be used to generate preliminary framework files (code and resources, etc.); The resource Editor is designed to help visually design user interfaces; ClassWizard to assist in adding code to a framework file Finally, the compiler implements the application-specific logic through the class library.
Packaging
The MFC class library is the MFC framework. MFC class Libraries are C + + class libraries. These classes either encapsulate the WIN32 application programming interface or encapsulate the concepts of the application, or encapsulate OLE attributes, or encapsulate the capabilities of ODBC and DAO data access, and so on, as described below.
(1) Encapsulation of the WIN32 application programming interface
Wraps a Windows object with a C + + object. For example, class CWnd is a C + + window object that encapsulates the Windows window (HWND) and Windows window API functions in the member functions of C + + Window object, which has a member variable m_ The HWND is the window handle of the former.
(2) Encapsulation of the application concept
When you write a Windows application using the SDK, you always define a window procedure, register Windows Class, create a window, and so on. MFC encapsulates many of these similar processes and completes these tasks for programmers. In addition, MFC proposed a document-view-centric programming model, the MFC class library encapsulates its support. A document is a data object that is manipulated by a user, and a view is a window through which the user processes and views the data.
(3) Encapsulation of the com/ole characteristics
OLE is built on the COM model, which is cumbersome because applications that support OLE must implement a series of interfaces (Interface). MFC's OLE classes encapsulate a large amount of complex work in the OLE API, which provides a more advanced interface for implementing OLE.
(4) Encapsulation of ODBC functions
A small number of C + + classes that provide a more advanced interface with ODBC encapsulate the complex work of the ODBC API and provide a database programming pattern.
Inherited
First, MFC abstracts the common characteristics of many classes, and designs some base classes as the basis for implementing other classes. Among these classes, the most important classes are CObject and CCmdTarget. CObject is the root class of MFC, and most MFC classes are derived from them, including CCmdTarget. CObject implements some important features, including dynamic class information, dynamic creation, object serialization, support for program debugging, and so on. All classes derived from CObject will have or can have the attributes that CObject has. CCmdTarget provides a schema for message processing by encapsulating some properties and methods. In MFC, any class that can handle messages is derived from CCmdTarget.
For each of the different objects, MFC has designed a set of classes to encapsulate these objects, each with a base class that derives many more specific classes from the base class. These objects include the following kinds: Window objects, base classes are CWnd, application objects, base classes are CWinThread, document objects, base classes are CDocument, and so on.
Programmers will combine their own actual, from the appropriate MFC classes to derive their own classes, to achieve specific functions, to achieve their own programming purposes.
Virtual functions and dynamic constraints
MFC is based on "C + +", which naturally supports virtual functions and dynamic constraints. But as a programming framework, there is a problem to be solved: if only through virtual functions to support dynamic constraints, the virtual function table will be too bloated, memory consumption, inefficient. For example, when CWnd encapsulates a Windows window object, each Windows message corresponds to a member function that is inherited by derived classes. If these functions are designed as virtual functions, because of the number of too many, implementation is unrealistic. Then, MFC set up the message mapping mechanism to solve the dynamic constraint problem of message processing function in an efficient and convenient way.
In this way, MFC classes provide a rich programming interface through virtual functions and message mappings. While the programmer inherits the base class, it embeds its own virtual functions and message processing functions into the MFC programming framework. The MFC programming framework will invoke the code of the program at the right time, where appropriate. This book will fully demonstrate the MFC call virtual functions and message processing functions inside, let the reader of MFC's programming interface has a clear understanding.
The macro-frame System of MFC
As mentioned earlier, MFC implements the encapsulation of the concept of the application, and encapsulates the class, class inheritance, dynamic constraints, class relationships and interactions. The result of this encapsulation is a set of development templates (or schemas) for programmers. Programmers use different templates for different applications and purposes. Examples are templates for SDI applications, templates for MDI applications, templates for regular DLL applications, templates for extension DLL applications, templates for Ole/activex applications, and more.
These templates all take the idea of document-as the center, with each template containing a specific set of classes. The composition of a typical MDI application is discussed in the next section.
In order to support the encapsulation of application concepts, there is a lot of work to be done within MFC. For example, to implement the message mapping mechanism, the MFC programming framework must ensure that the message is first received and then processed in the established way. For example, in order to support DLL programming and multithreaded programming support, MFC internal use of special processing methods, using the module state, thread state, etc. to manage some important information. While these internal processes are transparent to programmers, knowing and understanding MFC's internal mechanisms can help write powerful and flexible programs.
In short, MFC encapsulates the functionality of the underlying functions, such as the Win32 api,ole Api,odbc API, and provides a higher level of interface to simplify Windows programming. At the same time, MFC supports direct calls to the underlying APIs.
MFC provides a Windows application development model, the control of the program is mainly done by the MFC framework, and MFC has done most of the functionality, predefined or implemented a number of events and message processing, and so on. The framework either handles events by itself, does not depend on the programmer's Code, or invokes the programmer's code to handle application-specific events.
MFC is a C + + class library in which programmers implement specific purposes by using, inheriting, and extending the appropriate classes. For example, when inheriting, application-specific events are handled by the programmer's derived classes and are not interested in the base class. This functionality is based on C + + support for inheritance, support for virtual functions, and message mapping mechanisms implemented by MFC.