共用方式為


CDialog::OnCancel

當使用者按一下 [取消] 或在強制回應或非強制回應對話方塊中按 ESC 鍵時,架構會呼叫這個方法。

virtual void OnCancel( );

備註

覆寫這個方法會執行動作 (例如還原舊資料),當使用者按一下 [取消] 或點擊 ESC 鍵關閉對話方塊時。 預設會呼叫 EndDialog 和讓 DoModal 傳回 IDCANCEL 關閉強制回應對話方塊。

如果您實作非強制回應對話方塊的 [取消] 按鈕,您必須覆寫 OnCancel 方法並在其中加入 DestroyWindow 。 不要呼叫基底類別方法,此方法,因為它會呼叫 EndDialog,讓對話方塊不可見,但不會刪除它。

注意事項注意事項

您不能覆寫這個方法,當您在編譯在 Windows XP 待測程式時使用 CFileDialog 物件。如需 CFileDialog 的詳細資訊,請參閱 CFileDialog 類別

範例

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();
}

需求

Header: afxwin.h

請參閱

參考

CDialog 類別

階層架構圖表

CDialog::OnOK

CDialog::EndDialog