A few days ago, I found that my program uses the non-modal dialog box. The Debug version has the following warning:
Warning: calling DestroyWindow in CWnd ::~ CWnd OnDestroy or PostNcDestroy in derived class will not be called
Because it was Warnning, I didn't care too much about it. Later I went online and found out that this problem was still very serious.
Finally, I have not figured out some basic problems. At the same time, I have learned a lot of new things in this process. There is a lot of information on this issue on the internet, and I feel that there are not many clear questions. I will give a summary today. I also want to answer some online questions. More importantly, I forgot my questions and can come back for reference.
Let's startMode dialog boxCall principle. That is, you must close this dialog box before you can operate the dialog box in the subsequent window)
InMode dialog boxLower, UseCDialog ::OnOK () orCDialog ::OnCancel () andCDialog ::OnClose () Close dialog box.
Be sure to pay attention to the previousCDialog ::Of course, you can reload the virtual function in your own dialog box. However, you must callCDialog ::)
For details, refer to the simple style to solve the problem.
CDialog ::Close () is called by default.CDialog ::OnCancel (),CDialog ::OnCancel () callCDialog ::Enddicel (IDCANCEL );
CDialog ::EndDialog callCDialog ::DestroyWindows (),CDialog ::DestroyWindows callCDialog ::OnDestroy ();
CDialog ::OnOK () first by defaultCDialog ::UpdateData (), and then callCDialog ::EndDialog (IDOK ).
CDialog ::EndDialog callCDialog ::DestroyWindows (),CDialog ::DestroyWindows callCDialog ::OnDestroy ()
It seems that you probably know the following equation.
CDialog ::Close () =CDialog ::OnCancel () =CDialog ::The enddicel (IDCANCEL) is the same.
CDialog ::OnOK () =CDialog ::EndDialog (IDOK ).
WhileCDialog ::EndDialog (IDOK) AndCDialog ::EndDialog (IDCANCEL) The difference is thatCDialog ::UpdateData (), that isCDialog ::EndDialog (IDOK). After the close dialog box is closed,Variable valueUpdated to the last value entered by the user. WhileCDialog ::EndDialog (IDCANCEL.
I wantClose the mode dialog boxI made it clear.
Let's talk aboutNon-Modal Dialog Box.
The Calling principle is:
In the mode-free dialog box, reload your OnOK (), that isCMyDialog ::OnOK () callCDialog: DestroyWindows ().Note:Not the defaultCDialog: OnOK ()To close the dialog box.
Heavy LoadCMyDialog ::OnCancel () callCDialog: DestroyWindows ().Note:Instead of calling the default CDialog: OnCancel () to close the dialog box.
It sounds simple, but the following describes a very specific programming problem.
Generally, the non-modal dialog box isGlobalThat is, it is created through new on heap instead of stack.
In this case, we also need to use delete to destroy objects, otherwise there will be Memory leakage. When will we delete this object?
On msdn, we can see that the methods provided by Microsoft are,
We need to reloadPostNcDestroy)That is, we need to implementCMyDialog: PostNcDestroy (). The content is as follows:
CMyDialog: PostNcDestroy () {CDialog: PostNcDestroy (); delete this ;}
In this way, our class will be Auto-Cleanup.
The msdn suggestion is in English. I did not understand it twice. Now it's easy to look back. But now you don't need to read this blog.
Http://msdn.microsoft.com/en-us/library/5zba4hah.aspx