MFC learning notes 1--mfc Overview

Source: Internet
Author: User
Tags object serialization

1. What is MFC?

MFC (Microsoft Foundation Class Library) is a programming framework consisting of various types

Objective: To allow programmers to establish applications in Windows based on the framework. The MFC framework defines the outlines of applications and provides standard implementation methods for user interfaces, all the programmers have to do is fill in the specific things of the specific application with the predefined interface.

Microsoft Visual C ++ provides corresponding tools to complete this task: Appwizard can be used to generate a preliminary framework file (code and resources); the resource editor is used to help intuitively design user interfaces; classwizard is used to help add code to the Framework file.

2. Encapsulation

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.

(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) APIS related to Windows are encapsulated in C ++ window object member functions. 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 an application supporting Ole must implement a series of interfaces, it is quite tedious. 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.

3. class 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.

4. 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.

5. 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.

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 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.

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.