简体   繁体   中英

how to hook control closing in an MFC custom control class

I am interested in allocating pointers, storing those in the LPARAM data of a comboboxex control, and making that control responsible for deleting those pointers when it is destroyed.

Since I am working in MFC, I can subclass a CComboBoxEx, and add either a message handler or a virtual member function.

The question is: Is this pattern possible with Win32 / MFC?

Basically, how does a control get notified that its corresponding HWND is being destroyed?

The documentation for WM_DESTROY:

The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window procedure of the window being destroyed after the window is removed from the screen . (Emphasis Mine)

Unfortunately, my vague recollection is that this means that OnDestroy() is too late for handling anything that requires interacting with the associated HWND, no?

Can I query the elements in the comboboxex during OnDestroy()? Is there another hook I can use that occurs "Just before my window / control is destroyed (instead of after!)?"

I wonder if I overrode CBEM_DELETEITEM for my subclass and forced it to delete the LPARAM data. Are all items explicitly deleted when a comboboxex is destroyed? If so, are they all destroyed via that message (does the control send that message to itself?)

During OnDestroy() your window will still be valid -- if it weren't, your window wouldn't have gotten the message at all, since it's sent via the standard Windows messaging system.

You are on the right track -- this type of scenario is what OnDestroy() is for.

From MSDN :

OnDestroy is called first for the CWnd being destroyed, then for the child windows of CWnd as they are destroyed. It can be assumed that all child windows still exist while OnDestroy runs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM