In Vc++ 6.0 Dialog Based MFC application: I do not want my user close the window by pressing the button [X] in the top-right side of the window itself and also (Alt+F4). I want to display a messageBox ("Do you really want to close the application"); if the user clicks the OK button then the application has to close, else if user clicks the CANCEL button then the application must not be closed.
You need to implement OnClose()
and only call the base class's OnClose()
if you want to quit.
Handle the WM_SYSCOMMAND
message and do something like this in it.
void CMyApp::OnSysCommand(UINT nID, LPARAM lParam)
{
if(nID == SC_CLOSE)
{
if(MessageBox(_T("Really"), _T("What"), MB_YESNO) == IDYES);
//Do What you want here.
else
//Do something else
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
Here is how to add WM_SYSCOMMAND Handler to your Code:
Go to ClassView. Right click your dialog class if it is a dialog based application OR your mainframe class if it is a SDI/MDI Application. Click Properties.
In Properties Window, click on the Messages button. Scroll down to WM_SYSCOMMAND and on drop-down combo double-click to add the handler.
OR
You can do it manually as well by adding an entry in the message map. And adding declaration/definition in .h/.cpp respectively.
-
OnSysCommand(UINT nID, LPARAM lParam) how to add this member funtion to my application. please i am new in VC++ – vijay Jul 22 '09 at 7:21
-
this option is in .net but i am using vc++ 6.0 , in vc++ 6.0 if you right click on dialog class of dialog based application there is no option like properties. better i will try manually but sory to ask you , i dont know to how to add manually if give steps it is helpful to me. – vijay Jul 22 '09 at 8:28
-
Wow its working nice , Thank You Aamir final i got the result – vijay Jul 22 '09 at 8:57
-
In VC++ 6.0 you can do it through class wizard which comes up by pressing Ctrl + W (I think) – Aamir Jul 22 '09 at 10:19