MFC and Win32

Source: Internet
Author: User

 

Relationship between MFC Object and Windows Object

The most important encapsulation in MFC is the encapsulation of Win32 APIs. Therefore, we need to understand Windows Object and MFC Object (C ++ Object, a C ++ class instance) the relationship between them is one of the key to understanding MFC. Windows Object is a Windows operating system Object represented by a handle in Win32, and MFC Object is a C ++ Object, is an example of a C ++ class. Here (within the scope of this book), the MFC Object has a specific meaning, which refers to the C ++ Object that encapsulates Windows objects, it does not mean any C ++ Object.

The MFC Object and Windows Object are different, but they are closely related. Take the window object as an example:

OneThe MFC window object is an instance of the C ++ CWnd class (or a derived class) and is directly created by a program. During program execution, it is generated with the call of window class constructor, and disappears with the call of the destructor. Windows is an instance of an internal data structure in Windows. It is identified by a "window handle". Windows creates it and allocates system resources to it. After an MFC window object is created in Windows, it is created by the Create member function of the CWnd class. The "window handle" is saved in the m_hWnd member variable of the window object. A Windows window can be destroyed by a program or a user action. The relationship between the MFC window object and the Windows window object is 2-1. Other Windows objects have similar relationships with corresponding MFC objects.

 

BelowCompare MFC Object with Windows Object. Some conclusions may not apply to the device description table (the MFC class is CDC, And the handle is HDC), but the specific description will be pointed out at that time.

Compare Data Structures

The MFC Object is an example of the corresponding C ++ class, which is defined by the MFC or programmer;

Windows Object is the internal structure of a Windows system and is referenced by a handle;

MFC defines a member variable for these classes to save the Windows Object handle corresponding to the MFC Object. For the device description table CDC class, two HDC handles are saved.

Hierarchical comparison

MFC Object is high-level, while Windows Object is low;

The MFC Object encapsulates most or all of the functions of Windows objects. Users of the MFC Object do not need to directly apply the Windows Object HANDLE (HANDLE) to use the Win32 API, instead, it refers to the member functions that reference the corresponding MFC Object.

Comparison from creation

The MFC Object is directly created by the program through the constructor; the Windows Object is created by the corresponding SDK function.

The following two steps are generally taken to use these MFC objects in MFC:

First, createWhen an MFC Object is created in a STACK or in a HEAP, the instance variable of the MFC Object handle is empty, or is not a valid handle.

Then, callThe MFC Object member function creates the corresponding Windows Object. The MFC handle variable stores a valid handle.

The creation of the CDC (device description table class) is different. In section 2.3, you can create and use the CDC and Its Derived classes.

Of course, you canThe following describes how to create a Windows Object in the MFC Object constructor.

Comparison from conversion

FromObtain the corresponding Windows Object handle using the MFC Object member function GetSafeHandle.

You can use an existingCreate a corresponding MFC Object for a Windows Object. Generally, use the member function Attach or FromHandle of the MFC Object to create a permanent Object. The former can obtain a temporary Object.

Use range comparison

The MFC Object is invisible and unavailable to other processes of the system. Once a Windows Object is created, its handle is global throughout the Windows system. Some handles can be used by other processes. Typically, a process can obtain the window handle of another process and send messages to the window.

For the thread of the same process, you can only useYou cannot use an MFC Object from other threads.

Comparison from destruction

The MFC Object disappears with the calling of the destructor, but the Windows Object must be destroyed by the corresponding Windows system function.

Device descriptionThe HDC handle objects of the CDC class may not be destroyed, but released.

Of course, you canThe destruction of Windows objects is completed in the destructor of the MFC Object, and the GDI class of the MFC Object is implemented in this way. However, we should see that the destruction of the two is different.

TypeWindows objects all have corresponding MFC objects. The following table lists their mappings, as shown in Table 2-1:

Table2-1 Relationship Between MFC Object and Windows Object

Related Article

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.