C ++ beginners

Source: Internet
Author: User

MFCMicrosoft Foundation Class Library combines various types to form an application framework. It aims to allow programmers to establish applications under Windows on this basis, this is a simpler method than the SDK.

In general, the MFC framework defines the outlines of applications and provides standard implementation methods for user interfaces, all programmers need to do is fill in the specific things of the specific application with the predefined interface.

Microsoft Visual C ++ provides corresponding tools to do this: AppWizard can be used to generate preliminary framework file code and resources); resource editor is used to help intuitively design user interfaces; classWizard is used to help you add code to the Framework file. Finally, you can use the class library to implement the specific logic of the application.

1. Encapsulation

The MFC class library constitutes the MFC framework. The MFC class library is a C ++ class library. These classes can encapsulate Win32 application programming interfaces, application concepts, OLE features, or ODBC and DAO data access functions, it is described as follows.

1) Encapsulation of Win32 application programming interfaces

Use a C ++ Object to package a Windows Object. For example, class CWnd is a C ++ window object. It encapsulates Windows window (HWND) and Windows window-related API functions in the member functions of C ++ window object, the latter member variable m_hWnd is the former window handle.

2) Encapsulation of application concepts

When using the SDK to write a Windows application, you must always define the Window Process, register the Windows Class, create a window, and so on. MFC encapsulates many similar processes to complete these tasks for programmers. In addition, MFC proposes a document-View-centered programming mode. The MFC class library encapsulates support for it. A document is a data object operated by a user. A view is a data operation window through which users process and view data.

3) Encapsulation of COM/OLE features

OLE is built on the COM model. Because OLE-enabled applications must implement a series of interfaces, it is rather cumbersome. The OLE classes of MFC encapsulate a large amount of complex work of OLE APIs, which provide more advanced interfaces for implementing OLE.

4) Encapsulation of ODBC Functions

A small number of C ++ classes that can provide more advanced interfaces with ODBC encapsulate a large amount of complex work of ODBC APIs and provide a database programming mode.

Ii. Inheritance

First, MFC abstracts many common features and designs some base classes as the basis for implementing other classes. Among these classes, the most important classes are CObject and c1_target. CObject is the root class of MFC. The vast majority of MFC classes are derived from it, including csf-target. 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 features of CObject. Cve-target provides a message processing architecture by encapsulating some attributes and methods. In MFC, any class that can process messages is derived from csf-target.

For each different object, MFC has designed a set of classes to encapsulate these objects, each group of classes has a base class, derived from the base class many more specific classes. These objects include the following types: Window objects, the base class is CWnd, the application object, the base class is CwinThread, the document object, and the base class is Cdocument.

Programmers will combine their own reality to generate their own classes from appropriate MFC classes to implement specific functions and achieve their own programming goals.

Iii. virtual functions and dynamic constraints

Based on "C ++", MFC naturally supports Virtual functions and dynamic constraints. However, as a programming framework, there is a problem that must be solved: If dynamic constraints are only supported by virtual functions, the virtual function tables will inevitably become too bloated, consume memory, and be inefficient. For example, when CWnd encapsulates Windows window objects, each Windows Message corresponds to a member function, which is inherited by the derived class. If these functions are all designed as virtual functions, the implementation is unrealistic due to the large number of functions. Therefore, MFC establishes a message ing mechanism to solve the dynamic constraints of message processing functions in an efficient and easy-to-use way.

In this way, through virtual functions and message ing, the MFC class provides a wide range of programming interfaces. While inheriting the basic classes, programmers embed their own virtual functions and message processing functions into the MFC programming framework. The MFC programming framework calls the program code at appropriate times and places. This book will fully show the inside story of MFC calling virtual functions and message processing functions, so that readers can have a clear understanding of the programming interfaces of MFC.

Iv. MFC macro framework system

As mentioned above, MFC encapsulates the concept of an application and encapsulates the inheritance, dynamic constraints, and relationships and interactions of classes and classes. The encapsulated result is a set of development templates or models for programmers ). Programmers use different templates for different applications and purposes. For example, SDI application templates, MDI application templates, rule DLL application templates, extended DLL application templates, and OLE/ACTIVEX application templates.

These templates all adopt the document-centric idea, and each template contains a group of specific classes. The composition of typical MDI applications will be discussed in the next section.

To support encapsulation of application concepts, MFC must do a lot of internal work. For example, to implement the message ing mechanism, the MFC programming framework must first obtain the message and then process it according to the established method. For example, to support DLL programming and multi-threaded programming, MFC uses special internal processing methods to manage important information, such as module status and thread status. Although these internal processes are transparent to programmers, understanding and understanding the internal mechanism of MFC helps to write flexible and powerful programs.

In short, MFC encapsulates the functions of underlying functions such as Win32 API, ole api, and odbc api, and provides interfaces at a higher level to simplify Windows programming. In addition, MFC supports direct calls to underlying APIs.

MFC provides a Windows application development mode. program control is mainly completed by the MFC framework, and most of the functions are completed by MFC, predefined or implemented many events and message processing. The framework can handle events by itself, independent of the programmer's code, or call the programmer's code to process specific events in the application.

MFC is a C ++ class library. programmers use, inherit, and extend appropriate classes to achieve specific purposes. For example, during inheritance, the application-specific events are handled by the programmer's derived classes, and those that are not interested are processed by the base classes. The foundation for implementing this function is C ++'s support for inheritance, virtual functions, and message ing mechanism implemented by MFC.

We hope that the above introduction to MFC will help you.

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.