A detailed introduction to C + + MFC

Source: Internet
Author: User

We all know that MFC is a C + + class library, this article introduces the C + + class library, suitable for beginners to read, hope to help you, together to see.

MFC The various classes in the Microsoft Foundation Class Library are combined to form an application framework that is designed to allow programmers to build applications under Windows, a simpler approach than the SDK.

Because, in general, the MFC framework 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 the outline with specific application specifics 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 used to help visually design the user interface; ClassWizard is used to assist with adding code to the framework file At the end of the compilation, application-specific logic is implemented through the class library.

I. Encapsulation

The MFC framework is composed of MFC class libraries. The MFC class Library is a C + + class library. These classes either encapsulate the WIN32 application programming interface, or encapsulate the concept of an application, or encapsulate OLE features, or encapsulate the functionality of ODBC and DAO data access, and so on, as described below.

(1) Encapsulation of the WIN32 application programming interface

Use a C + + object to wrap a Windows object. For example, class CWnd is a C + + window object that encapsulates API functions related to Windows window (HWND) and Windows window in a member function of the C + + Window object, whose member variables m_ The HWND is the window handle of the former.

(2) Encapsulation of application concepts

When writing Windows applications using the SDK, you always define window procedures, register Windows Class, create Windows, and so on. MFC encapsulates a lot of similar processing to do the work for programmers. In addition, MFC proposes a document-view-centric programming model, and the MFC class library encapsulates its support. A document is a data object that is manipulated by a user, and the view is a window into which the user processes and views the data.

(3) encapsulation of the com/ole characteristics

OLE builds on the COM model, which is cumbersome because OLE-enabled applications must implement a series of interfaces (Interface). The OLE classes of MFC encapsulate the extensive and complex work of OLE APIs, which provide a more advanced interface for implementing OLE.

(4) Encapsulation of ODBC functions

A small number of C + + classes that provide a more advanced interface to ODBC encapsulate a large amount of complex work on the ODBC API and provide a database programming model.

Ii. inheritance

First, MFC abstracts out the common features of many classes and designs some base classes as the basis for implementing other classes. The most important classes in these classes are CObject and CCmdTarget. CObject is the root class of MFC, and the vast majority of MFC classes are derived, 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 attributes owned by CObject. CCmdTarget provides a schema for message processing by encapsulating some properties and methods. In MFC, any class that can handle a message 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 categories: 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 reality, derive their own classes from the appropriate MFC classes, implement specific functions, and achieve their own programming purposes.

Three, virtual functions and dynamic constraints

MFC is based on "C + +" and naturally supports virtual functions and dynamic constraints. But as a programming framework, one problem has to be solved: if dynamic constraints are only supported by virtual functions, the virtual function tables are too bloated, memory-consuming, and inefficient. For example, when CWnd encapsulates a Windows window object, each Windows message corresponds to a member function that inherits from the derived class. If these functions are designed as virtual functions, it is unrealistic to implement them because of the sheer number of them. Therefore, MFC set up a message mapping mechanism, with an efficient, easy-to-use means to solve the dynamic constraints of message processing functions.

In this way, the 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 function and message processing function into MFC's programming framework. The MFC programming framework will invoke the code of the program at the appropriate time and place. This book will fully demonstrate the inside of MFC's call to virtual functions and message handlers, giving readers a clear understanding of MFC's programming interfaces.

IV. macro Framework System of MFC

As mentioned earlier, MFC encapsulates the application concept by encapsulating classes, class inheritance, dynamic constraints, class relationships, and interactions. The result of this encapsulation is a set of development templates (or patterns) for programmers. Programmers use different templates for different applications and purposes. For example, a template for an SDI application, a template for an MDI application, a template for a regular DLL application, a template for an extension DLL application, a template for an Ole/activex application, and so on.

These templates all take the form of document-centric thinking, 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, MFC must do a lot of work inside. For example, in order 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 multi-threaded programming, MFC uses a special processing method, using module state, thread state and so on to manage some important information. While these internal processes are transparent to programmers, understanding and comprehending MFC's internal mechanisms can help write a program that is flexible and powerful.

In summary, MFC encapsulates the functionality of the underlying functions, such as the Win32 api,ole Api,odbc API, and provides a higher level of interface, simplifying Windows programming. At the same time, MFC supports direct invocation of the underlying API.

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 functions, pre-defined or implemented a lot 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 where programmers achieve 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, not interested by the base class. The basis for implementing this functionality is C + + inheritance support, support for virtual functions, and the message mapping mechanism implemented by MFC.

Hope that through the above on the introduction of MFC, can bring help to everyone.

A detailed introduction to C + + MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.