Partager via


CDialog::OnCancel

The framework calls this method when the user clicks Cancel or presses the ESC key in a modal or modeless dialog box.

virtual void OnCancel( );

Remarks

Override this method to perform actions (such as restoring old data) when a user closes the dialog box by clicking Cancel or hitting the ESC key. The default closes a modal dialog box by calling EndDialog and causing DoModal to return IDCANCEL.

If you implement the Cancel button in a modeless dialog box, you must override the OnCancel method and call DestroyWindow inside it. Do not call the base-class method, because it calls EndDialog, which will make the dialog box invisible but not destroy it.

Note

You cannot override this method when you use a CFileDialog object in a program that is compiled under Windows XP. For more information about CFileDialog, see CFileDialog Class.

Example

void CSimpleDlg::OnCancel()
{
   // TODO: Add extra cleanup here 

   // Ensure that you reset all the values back to the 
   // ones before modification. This handler is called 
   // when the user doesn't want to save the changes. 

   if (AfxMessageBox(_T("Are you sure you want to abort the changes?"), 
      MB_YESNO) == IDNO)
   {
      // Give the user a chance if he has unknowingly hit the 
      // Cancel button. If he says No, return. Don't reset. If 
      // Yes, go ahead and reset the values and close the dialog. 
      return; 
   }

   m_nMyValue = m_nPrevValue;
   m_pMyString = NULL; 

   CDialog::OnCancel();
}

Requirements

Header: afxwin.h

See Also

Reference

CDialog Class

Hierarchy Chart

CDialog::OnOK

CDialog::EndDialog