Clear of the MFC window object in VC ++

zhaozj2021-02-11  196

For VC beginners, I always feel that the clearance process of window objects is somewhat inexplicably. It seems that DELETE is explicitly called, which seems to violate the rules related to initialization and clearing in C . So how do the program cancel one? Window object?

To eliminate window objects, you must clear the composition of the window object. In a usual program, create a C window object, then create the actual window structure and return the handle and the C object. That is, the window object contains C Window objects and Windows window objects, both through the handle hwnd.

Now let's take a look at "Regular" window object clearance process. The so-called object's clearance refers to the resource occupied by the release object. The Windows window object possessed by the Windows window object is system resources. The C object possesses the memory resources. Release system Resources is relatively simple: call virtual functions DestroyWindow delete the Windows window object. If DESTROYWINDOW deletes the parent window, Windows will automatically call DestroyWindow for the child window. Generally, the program does not have to call DESTROYWINDOW. Because when the user closes the window, Windows is WINDOWS Send a WM_CLOSE message, the default message processing function of WM_CLOSE CWnd :: OnClose calls DestroyWindow.

At this time, the clearing work has been completed, the window on the screen is gone! But don't forget, there is a C window object in the memory. Let's take a look at the process of C object clearance: When the window is canceled One message sent by the window is WM_NCDESTROY. It default message processing function CWnd :: OnNCDESTROY separates the C window object with the handle HWND, and calls a very important virtual function PostncDestroy. This function is the key to clear window object Clear POSTNCDESTROY in. Cwnd does not do anything. Some MFC window classes are overloaded, and the DELETE THIS code is added to delete the C object. These window classes are often built in the pile in the New operator. Because of the heavy load of PostncDestroy, make The window has an automatic cleaning function. Therefore, we don't have to care for the problem. Additional MFC window classes are typically created in variables, and the MFC does not have to overload the PostncDestroy function for them.

No window classes for automatic clearance, generally create or embedded in other C objects in the stack:

All standard Windows control classes (such as CSTATIC, CEDIT, CLISTBOX, etc.)

Sub-window objects (such as user custom controls) by CWND classes

Split window (CSPLitterWnd)

Default control bar class (derived class of ccontrolbar)

Dialog Box Class (CDIALOG) Modal dialog box created on the stack

All Windows General dialogs (except CFINDREPLACEDIALOG)

Dialog box created by ClassWizard

Window classes with automatic cleaning feature, generally created in the heap:

Main frame window (direct or indirectly derived from CFRAMEWND)

View class (direct or indirectly derived from the CView class)

To a certain extent, the "service home" of MFC makes the beginner can't find the north. However, I have to admit that the MFC is very beautiful!

When you talk about it, we should understand an important criterion in C : clear the window object with DESTROYWINDOW, do not use "delete".

For window classes that do not have automatic clearance, "Delete" first adjusts DestroyWindow in the destructor, because in the destructor, the virtual machine does not work, only the local version (CWND class) DestroyWindow function, obviously this is not what we want. For window classes with automatic cleaning, it seems more problem more, and the overloaded PostncDestroy has already contains "delete this", so that C object is released two Time.

转载请注明原文地址:https://www.9cbs.com/read-3939.html

New Post(0)