How to use the "close"-button of a window
I want to use the close-button not only for closing the window, there should also be an order to open a message-box before ending the programm. How can I do that ?
[163 byte] By [
newbie Joe] at [2007-11-18 1:40:31]

# 1 Re: How to use the "close"-button of a window
Use the WM_DESTORY Windows message handler. This is fired before the window is actually destoryed. Then you will have an OnDestroy event that you can place you code in. Just make sure all is placed before the line
CFrameWnd::OnDestroy();
# 2 Re: How to use the "close"-button of a window
It depends upon where in the close processing you want the MessageBox to appear. It also depends if you need to do something more than you are not telling us.
You can process the WM_CLOSE message and call the MessageBox function there.
# 3 Re: How to use the "close"-button of a window
Thanks for your help. The message-box appears. But I have 3 other problems:
1. It appears after the main-window of the program has closed and not before. Maybe I did sth. wrong.
2. I have an Exit-Button in the program with following source code:
GetDocument()->OnCloseDocument();
3. The message-box also appears here when I use this button to end the program, but it should not appear here.
Another problem is that I didn't find
CFrameWnd::OnDestroy();
in the source-code.
Can you help me ?
# 4 Re: How to use the "close"-button of a window
Sorry, grabbed the wrong spot on what I was working on. Use the WM_CLOSE message handler which generates the OnClose event.
If this is a CDialog you will place you code before the
CDialog::OnClose();
line (which the TODO says anyway).
Then do your message box and if is a response that will override code just set the logic up so that the
CDialog::OnClose();
is not called and that will prevent the Window closing.
Sorry 'bout that.
# 5 Re: How to use the "close"-button of a window
You also can use PostQuitMessage in your close-button function:
void CMyDlg::OnCloseButton()
{
AfxMessageBox("Ho-ho!", MB_OK, 0);
PostQuitMessage(0);
}
deesan at 2007-11-10 8:58:15 >

# 6 Re: How to use the "close"-button of a window
Originally posted by deesan
You also can use PostQuitMessage in your close-button functionNo!
# 7 Re: How to use the "close"-button of a window
Originally posted by newbie Joe
1. It appears after the main-window of the program has closed and not before.You did not say in your original question that you needed the message to appear before the main window is closed. I suspected that is what you wanted but you did not say so.
Originally posted by newbie Joe
2. I have an Exit-Button in the program with following source code:
GetDocument()->OnCloseDocument();Give the button the control id "ID_APP_EXIT". Then you don't have to write any source code to get the application to exit.
# 8 Re: How to use the "close"-button of a window
Thanks for all your help. I did it. Now it works. Here my source code:
void CMainFrame::OnClose()
{
CFileFind fileFind;
if(fileFind.FindFile("c:\\test.exe", 0) == TRUE)
MessageBox("Closing window","Notification",MB_OK);
DestroyWindow();
}
:D ;) :D
I think there is no problem anymore. I also used the WM_CLOSE message handler. Great help !!!
# 9 Re: How to use the "close"-button of a window
Instead of:DestroyWindow();use:CFrameWnd::OnClose();
# 10 Re: How to use the "close"-button of a window
I looked at the MFC source code for CFrameWnd::OnClose and there is a lot there that would not be done if DestroyWindow is called directly. The last line is:DestroyWindow();In other words, use CFrameWnd::OnClose instead of DestroyWindow as I said previously.
# 11 Re: How to use the "close"-button of a window
Sorry, yesterday I didn't have time to look for a new message and write you back. Thanks that you helped me again. I changed DestroyWindow() into CFrameWnd::OnClose and it works great !!!
:)