Handle in MFC

Source: Internet
Author: User

1.Picking handle

CDC problems:
1. cdc dc; DC. lineto (point); cannot run

2. CDC * Dc = getdc (); DC-> lineto (point );
There are a large number of handle packaging classes in MFC. As the name suggests, the so-called handle packaging class refers to these classes encapsulate the handles of System Objects (hwnd, HDC, hbitmap, etc.), and provides a group of functions as interfaces to access system objects. (This is an embodiment of the concept of resource encapsulation ). From the meaning of these handle packages, we can see that an MFC object is fully constructed and in a good state, and the handle member is used to mark a system object) it must be valid (this is a required condition) before the member functions can be used to operate on this system object normally. Therefore, the construction of a general MFC handle packaging class object requires two tasks:

First, create a handle packaging class object. (In heap or stack, depending on actual needs) Second, initialize the system object handle. Most packaging objects are usually constructed in two steps: 1. Create an object, 2. Create a system object through a member function such as create and initialize the handle members in the object. Of course, these two parts can also be completed directly in the constructor. (In this case, a system object handle is usually used as a parameter, or another well-constructed handle packaging class object is used as a parameter to initialize the handle member in the constructor. Other methods include using attach and detach to bind some constructed objects to the system object handle, or using fromhandle to obtain a temporary handle packaging Class Object)
Now the problem is clear: cdc dc; DC. lineto (point); cannot run because the DC object is only partially constructed, and the system object handle member is not assigned a valid drawing device handle, naturally, subsequent lineto calls will fail. And: CDC * Dc = getdc (); DC-> lineto (point );
Call the getdc () member function of the window class to return a well-constructed CDC object (that is, the device handle member in this object has been assigned a valid handle ), therefore, the subsequent lineto operations are okay. That is, the key to the problem is not whether the object is created in the stack or heap, but whether a handle packaging class object is fully constructed.

 

2.What is the handle? What is the pointer?

Actually, the handle is nothing magical. No matter which handle, it is actually an integer. It identifies a resource, such as a window or a bitmap. Just like finding a person, you must know its address. If you want to operate a resource, you must first obtain the handle. "Retrieve window handle ()" is not only a window handle. You can use this command to retrieve all window controls, such as edit boxes and labels. For example: edit box 1. take the window handle () or label 1. take the window handle (). The handle of a control is the same. Any control has its own special properties, and the handle also refers to its special properties (including commonalities ).

Handle is intended to be a handle. It means something that deals with the operating system. For example, if you are admitted to a university, the school (operating system) will give you a student ID card number. Note: This number is specified by the school. YouUnable to select. With this number (student ID card, if one card is used for multiple purposes), you can enjoy the services provided by the school. For example, you can borrow books from the library, go to the canteen for dinner, and go to the classroom for classes. But you cannot buy beer in the canteen because the school does not allow this service. The service provided by the system in the computer is called by API. With handle, you can confidently propose an API service to the system. The pointer has a lot of power. With the pointer, you can drink and fight everywhere, and the school (operating system) cannot handle it, the difference between a handle and a pointer is that a handle can only call the services provided by the system. While the handle is a number that can be differentiated from each other, it is different from our ordinary ID number.ProgramThe handler is defined by the system when the object is generated. It is used to differentiate the objects in the system. This handle is not assigned by the programmer.

You can refer to a person on csdn to describe the relationship between the handle and pointer object instances:

Shepherd's remote finger Apricot Flower village. The hand of the shepherd boy is the pointer, the brand of the apricot Village is the handle, and the apricot Village Hotel is an instance of the object.

 

3.In-depth discussion of handle

to be more thorough, a handle is a pointer to a pointer. As we all know, pointers are memory addresses. After the application starts, the objects that make up the program stay in the memory. If we simply understand it, it seems that we only need to know the first address of the memory, then we can use this address to access the object at any time. However, if you really think so, you are very wrong. We know that Windows is an operating system based on virtual memory. In this system environment, Windows Memory Manager often moves objects back and forth in memory to meet the memory needs of various applications. The object is moved, which means its address has changed. If the address is always changing, where should we find this object? To solve this problem, the Windows operating system frees up some internal storage addresses for each application to specifically register the address changes in the memory of each application object, and this address (the location of the storage unit) the body remains unchanged. After the Windows Memory Manager moves the object's location in the memory, it notifies the new address of the object to save the address. In this way, we only need to remember this handle address to indirectly know the location of the object in the memory. This address is assigned by the system during object loading. When the system is detached, it is released to the system. Handle address (stable) → record the address of the object in the memory → address of the object in the memory (unstable) → actual object nature: in Windows, a physical address is not used to identify a memory block, file, task, or dynamic Mount module. In contrast, Windows API assigns a definite handle to these projects, return the handle to the application, and then use the handle to perform the operation. However, it must be noted that each time the program is restarted, the system cannot ensure that the handle assigned to the program is the original one, and the vast majority of cases are indeed different. If we watch a movie at a cinema as an application, the handles assigned to the application by the system are always different, this is the same as the fact that tickets sold to us at a cinema are always 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.