Usually we understand that the message WM_CREATE is issued when the window is created, but there is a message WM_NCCREATE that is issued before this message.
Namely:
WM_NCCREATE Corresponding to
WM_CREATE
, the message WM_DESTROY, WM_NCDESTROY is issued when the window is closed.
Sequence:
WM_DESTROY
WM_NCDESTROY
It should be noted here that when a window is closed, if there is a child window in this window, the WM_DESTROY and WM_NCDESTROY messages of the child window will be sent first, and then the WM_DESTROY and WM_NCDESTROY messages of the parent window will be sent.
Sequence:
WM_DESTROY //CHILD, child window message
WM_NCDESTROY //CHILD, child window message
WM_DESTROY //Parent, parent window message
WM_NCDESTROY //Parent, parent window message
The following is an example to verify the execution sequence:
1. Create a new MFC project , the project name is TestCreate, then there is already a default form TestCreate, and the settings are as follows:
2. Next, add the subform TestChild without setting it.
3. Add the open subform event response:
- #include "TestChild.h"
- void CTestCreateDlg::OnButton2() //Open the subform
- {
- if (!frm.GetSafeHwnd())
- {
- frm.Create(IDD_TESTCHILD_DIALOG,this);
- }
- frm.ShowWindow(SW_SHOW);
- }
- LRESULT CTestCreateDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: Add your specialized code here and/or call the base class
- switch(message)
- {
- case WM_CREATE:
- TRACE0("----------------------WM_CREATE/n");
- break;
- case WM_NCCREATE:
- TRACE0("----------------------WM_NCCREATE/n");
- break;
- case WM_DESTROY:
- TRACE0("----------------------WM_DESTROY/n");
- break;
- case WM_NCDESTROY:
- TRACE0("----------------------WM_NCDESTROY/n");
- break;
- default:
- break;
- }
- return CDialog::DefWindowProc(message, wParam, lParam);
- }
- LRESULT CTestChild::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- // TODO: Add your specialized code here and/or call the base class
- switch(message)
- {
- case WM_CREATE:
- TRACE0("----------------------WM_CREATE_CHILD/n");
- break;
- case WM_NCCREATE:
- TRACE0("----------------------WM_NCCREATE_CHILD/n");
- break;
- case WM_DESTROY:
- TRACE0("----------------------WM_DESTROY_CHILD/n");
- break;
- case WM_NCDESTROY:
- TRACE0("----------------------WM_NCDESTROY_CHILD/n");
- break;
- default:
- break;
- }
- return CDialog::DefWindowProc(message, wParam, lParam);
- }
点击按钮打开子窗体,然后切换到父窗体,并点击关闭按钮,可以看到输出结果:
----------------------WM_NCCREATE
Loaded 'C:/WINDOWS/system32/version.dll', no matching symbolic information found.
Loaded 'C:/WINDOWS/system32/MSCTFIME.IME', no matching symbolic information found.
----------------------WM_CREATE
----------------------WM_NCCREATE_CHILD
----------------------WM_CREATE_CHILD
----------------------WM_DESTROY_CHILD
----------------------WM_NCDESTROY_CHILD
----------------------WM_DESTROY
----------------------WM_NCDESTROY