Relationship between MFC object and Windows Object

Source: Internet
Author: User
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.

Next, we will compare the MFC object and 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.
  1. Compare Data Structures
MFC object is an instance of the corresponding C ++ class, which is defined by MFC or programmers. Windows Object is the internal structure of the 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.
  1. Hierarchical comparison
MFC object is high-level, while windows object is low. MFC object encapsulates most or all functions of Windows Object. Users of MFC object do not need to directly apply the handle (handle) of Windows Object) the Win32 API is used to reference the member functions of the corresponding MFC object instead.
  1. 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. To use these MFC objects in MFC, follow these steps: first, create an MFC object, create it in the stack, or create it in the heap, the instance variable of the MFC object handle is null, or is not a valid handle. Then, call the MFC object member function to create 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 can create the corresponding windows object in the MFC object constructor. the GDI class of MFC is implemented in this way, but in essence, the creation of an MFC object is different from that of a Windows Object.
  1. Comparison from conversion
You can obtain the corresponding windows object handle from an MFC object. Generally, you can use the MFC object member function getsafehandle to obtain the corresponding handle. You can create a corresponding MFC object from an existing windows object. Generally, you can use the member function attach or fromhandle of the MFC object to create a permanent object, the latter may obtain a temporary object.
  1. 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, only the MFC object created by this thread can be used, and the MFC object of other threads cannot be used.
  1. 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. The devices in the device description table have different CDC objects. The corresponding HDC handle objects may not be destroyed but released. Of course, the destruction of Windows objects can be 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.

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.