MFC: destruction of MFC window objects (cwnd objects) and window objects (objects indicated by hwnd)

Source: Internet
Author: User

From: http://blog.csdn.net/xiaoxiongli/article/details/1670277

 

An MFC window object contains two elements: one is the window object encapsulation window, that is, the hwnd (window handle) stored in the m_hwnd member ), second, the window object itself is a C ++ object. To delete an MFC window object, delete the window object encapsulated by the window object and then delete the window object itself.

The most direct method to delete a window is to call cwnd: destroywindow or: destroywindow. The former encapsulates the latter's functions. The former not only calls the latter, but also invalidates the hwnd saved by m_hwnd (null ). If destroywindow deletes a parent window or owner window, the function automatically deletes all child windows or owner windows before deleting the parent window or owner. In general, you do not need to directly call destroywindow in the program to delete the window, because MFC will automatically call destroywindow to delete the window. For example, when a user exits the application, a wm_close message is generated, which causes the MFC to automatically call cwnd: destroywindow to delete the main framework window, when you press the OK or cancel button in the dialog box, MFC automatically calls cwnd: destroywindow to delete the dialog box and its controls.

The deletion of window objects is divided into two types based on the object creation method. In MFC programming, a large number of window objects are used. Some window objects are embedded in another object as variables or created on the stack as local variables, some are created in the heap using the new operator. For a window object created as a variable, the programmer does not have to worry about its deletion, because the life cycle of the object is always limited. if the object is a member variable of an object, it disappears as the parent object disappears. if the object is a local variable, it is cleared when the function returns.

The lifetime of a window object dynamically created in the heap is arbitrary. When beginners are learning C ++ programming, they are often not very practical about the use of the new operator. Because New is used to create an object in the heap, they cannot forget to delete the object with Delete. When learning the MFC routine, the reader may have such a question: why does some programs use new to create a window object without explicitly using Delete to delete it? The answer is that some MFC window objects can be automatically cleared.

As mentioned earlier in the non-modal dialog box, when you call cwnd: destroywindow or: destroywindow to delete a window, the postncdestroy member function of the deleted window will be called. The default postncdestroy does nothing, but some MFC window classes will overwrite the function and call Delete this in the new version of postncdestroy to delete the object, thus providing the automatic clearing function. This type of window objects are usually created in the heap with the new operator, but the programmer does not have to worry about using the delete operator to delete them, because once the destroywindow is called to delete the window, the corresponding window object will be deleted immediately.

The window classes that do not have the automatic clearing function are as follows. These window objects are usually created in the form of variables, without the need to automatically clear the function.

  1. All standard Windows Control classes.
  2. Subwindow objects derived directly from the cwnd class (such as custom controls ).
  3. Split Window class csplitterwnd.
  4. The default control bar class (including the toolbar, Status Bar, and dialog bar ).
  5. Modal Dialog Box class.

 

The window classes with the automatic clearing function are as follows. These window objects are usually created in the heap.

  1. Main framework window class (derived directly or indirectly from the cframewnd class ).
  2. View class (derived directly or indirectly from the cview class ).

 

When designing your own derived window class, you can decide whether to design the window class to be automatically cleared Based on the window object creation method. For example, for a non-modal dialog box, the object is created in the heap, so it should have the automatic clearing function.

To sum up, for the MFC window class and its derived class, you do not need to explicitly Delete the window object in the program. That is to say, you do not need to call destroywindow to delete the window encapsulated by the window object, or use the delete operator explicitly to delete the window object itself. As long as the non-automatically cleared window objects are created in the form of variables, the automatically cleared window objects are created in the heap, And the MFC operating mechanism can ensure that the window objects are completely deleted.

To manually delete a window object, call the corresponding function (such as cwnd: destroywindow) to delete the window and then delete the window object. window objects created in the form of variables are automatically deleted by the framework. for non-automatically cleared window objects dynamically created in the heap, after the window is deleted, delete is called explicitly to delete objects (usually in the destructor of the owner or parent window ). for window objects with the automatic clearing function, you only need to call cwnd: destroywindow to delete the window objects and window objects. Note: Do not use the delete operator to delete window objects created in the heap before the window is closed.

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.