【MFC】CDialog类详解
00. 目錄
文章目錄
- 00. 目錄
- 01. 概述
- 02. 繼承層次結構
- 03. 成員方法
- 04. 常用成員方法描述
- 4.1 構造函數(shù)
- 4.2 Create函數(shù)
- 4.3 DoModal函數(shù)
- 4.4 EndDialog函數(shù)
- 4.5 OnCancel函數(shù)
- 4.6 OnInitDialog函數(shù)
- 4.7 OnOK函數(shù)
- 05. 附錄
01. 概述
用于在屏幕上顯示對話框的基類。
對話框分為兩種類型:模式和無模式。 在應用程序繼續(xù)之前,用戶必須關閉模式對話框。 無模式對話框允許用戶在不取消或刪除對話框的情況下顯示對話框并返回到其他任務。
CDialog對象是對話框模板和 CDialog 派生類的組合。 使用對話框編輯器創(chuàng)建對話框模板并將其存儲在資源中,然后使用添加類向導創(chuàng)建派生自的類 CDialog 。
對話框與任何其他窗口一樣,接收來自 Windows 的消息。 在對話框中,您特別希望處理來自對話框的控件發(fā)出的通知消息,因為這是用戶與您的對話框進行交互的方式。 使用 類向導 可以選擇要處理的消息,并將相應的消息映射項和消息處理程序成員函數(shù)添加到類。 只需在處理程序成員函數(shù)中編寫特定于應用程序的代碼即可。
如果您愿意,您始終可以手動編寫消息映射項和成員函數(shù)。
在除最普通對話框以外的所有對話框中,您可以向派生對話框類添加成員變量,以存儲用戶在對話框控件中輸入的數(shù)據(jù)或顯示用戶的數(shù)據(jù)。 您可以使用 “添加變量向導” 創(chuàng)建成員變量并將其與控件相關聯(lián)。 同時,為每個變量選擇變量類型和允許的值范圍。 代碼向導將成員變量添加到您的派生對話框類。
系統(tǒng)將生成數(shù)據(jù)映射,以自動處理成員變量與對話框控件之間的數(shù)據(jù)交換。 數(shù)據(jù)映射提供了一些函數(shù),這些函數(shù)用適當?shù)闹党跏蓟瘜υ捒蛑械目丶?#xff0c;檢索數(shù)據(jù)并驗證數(shù)據(jù)。
若要創(chuàng)建模式對話框,請使用派生對話框類的構造函數(shù)在堆棧上構造對象,然后調用 DoModal 以創(chuàng)建對話框窗口及其控件。 如果希望創(chuàng)建無模式對話框,請 Create 在對話框類的構造函數(shù)中調用。
還可以使用 DLGTEMPLATE 數(shù)據(jù)結構在內存中創(chuàng)建模板,如 Windows SDK 中所述。 構造 CDialog 對象后,調用 CreateIndirect 創(chuàng)建無模式對話框,或調用 InitModalIndirect 和 DoModal 創(chuàng)建模式對話框。
交換和驗證數(shù)據(jù)映射是用 CWnd::DoDataExchange 添加到新對話框類的的重寫編寫的。 CWnd 有關 exchange 和驗證功能的詳細信息,請參閱中的 DoDataExchange 成員函數(shù)。
程序員和框架 DoDataExchange 通過調用 CWnd::UpdateData間接調用。
UpdateData當用戶單擊 “確定” 按鈕關閉模式對話框時,框架會調用。 (如果單擊 “取消” 按鈕,則不檢索數(shù)據(jù)。 ) OnInitDialog 的默認實現(xiàn)也會調用 UpdateData 來設置控件的初始值。 通常會重寫 OnInitDialog 以進一步初始化控件。 OnInitDialog 在所有對話框控件都創(chuàng)建之后,在對話框顯示之前調用。
CWnd::UpdateData在執(zhí)行模式對話框或無模式對話框的過程中,您可以隨時調用。
如果手動開發(fā)一個對話框,則需要自行向派生對話框類添加必要的成員變量,并添加成員函數(shù)以設置或獲取這些值。
當用戶按 “確定” 或 “取消” 按鈕時,或者當你的代碼調用成員函數(shù)時,模式對話框將自動關閉 EndDialog 。
實現(xiàn)無模式對話框時,請始終重寫 OnCancel 成員函數(shù)并 DestroyWindow 從該函數(shù)中調用。 請勿調用基類 CDialog::OnCancel ,因為它 EndDialog 會調用,這會使對話框不可見,但不會銷毀它。 還應 PostNcDestroy 為無模式對話框重寫,以便刪除 this ,因為無模式對話框通常是用分配的 new 。 模式對話框通常是在框架上構造的,無需 PostNcDestroy 清除。
有關的詳細信息 CDialog ,請參閱 對話框。
02. 繼承層次結構
CObject └CCmdTarget └CWnd └CDialog 頭文件 afxwin.h03. 成員方法
構造函數(shù)
| CDialog::CDialog | 構造 CDialog 對象。 |
公有成員方法
| CDialog::Create | 初始化 CDialog 對象。 創(chuàng)建無模式對話框并將其附加到 CDialog 對象。 |
| CDialog::CreateIndirect | 從內存中的對話框模板創(chuàng)建無模式對話框 (基于資源的) 。 |
| CDialog::DoModal | 調用模式對話框并在完成時返回。 |
| CDialog::EndDialog | 關閉模式對話框。 |
| CDialog::GetDefID | 獲取對話框的默認按鈕控件的 ID。 |
| CDialog::GotoDlgCtrl | 將焦點移到對話框中的指定對話框控件。 |
| CDialog::InitModalIndirect | 從內存中的對話框模板創(chuàng)建一個模式對話框, (不是基于資源的) 。 在調用函數(shù)之前,將存儲參數(shù) DoModal 。 |
| CDialog::MapDialogRect | 將矩形的對話框單位轉換為屏幕單位。 |
| CDialog::NextDlgCtrl | 將焦點移到對話框中的下一個對話框控件。 |
| CDialog::OnInitDialog | 重寫以增加對話框初始化。 |
| CDialog::OnSetFont | 重寫以指定對話框控件在繪制文本時使用的字體。 |
| CDialog::PrevDlgCtrl | 將焦點移到對話框中的上一個對話框控件。 |
| CDialog::SetDefID | 將對話框的默認按鈕控件更改為指定的按鈕。 |
| CDialog::SetHelpID | 為對話框設置區(qū)分上下文的幫助 ID。 |
受保護的方法
| CDialog::OnCancel | 重寫以執(zhí)行 “取消” 按鈕或 ESC 鍵操作。 默認關閉對話框并 DoModal 返回 IDCANCEL。 |
| CDialog::OnOK | 重寫以在模式對話框中執(zhí)行 “確定” 按鈕操作。 默認關閉對話框并 DoModal 返回 IDOK。 |
04. 常用成員方法描述
4.1 構造函數(shù)
explicit CDialog(LPCTSTR lpszTemplateName,CWnd* pParentWnd = NULL);explicit CDialog(UINT nIDTemplate,CWnd* pParentWnd = NULL);CDialog();參數(shù) lpszTemplateName包含一個以 null 結尾的字符串,它是對話框模板資源的名稱。 nIDTemplate包含對話框模板資源的 ID 號。 pParentWnd指向對話框對象 所屬) 類型 的父或所有者窗口對象 (。 如果為 NULL,則對話框對象的父窗口將設置為主應用程序窗口。4.2 Create函數(shù)
virtual BOOL Create(LPCTSTR lpszTemplateName,CWnd* pParentWnd = NULL);virtual BOOL Create(UINT nIDTemplate,CWnd* pParentWnd = NULL); 功能: 調用 Create 以使用資源的對話框模板創(chuàng)建無模式對話框。 參數(shù): lpszTemplateName包含一個以 null 結尾的字符串,它是對話框模板資源的名稱。 pParentWnd指向對話框對象 所屬) 類型 的父窗口對象 (。 如果為 NULL,則對話框對象的父窗口將設置為主應用程序窗口。 nIDTemplate包含對話框模板資源的 ID 號。 返回值如果對話框創(chuàng)建和初始化成功,兩個窗體都將返回非零值;否則為0。 示例: void CMyDialog::OnMenuShowSimpleDialog() {//m_pSimpleDialog initialized to NULL in the constructor of CMyDialog classm_pSimpleDlg = new CSimpleDlg();//Check if new succeeded and we got a valid pointer to a dialog objectif (m_pSimpleDlg != NULL){BOOL ret = m_pSimpleDlg->Create(IDD_SIMPLEDIALOG, this);if (!ret) //Create failed.{AfxMessageBox(_T("Error creating Dialog"));}m_pSimpleDlg->ShowWindow(SW_SHOW);}else{AfxMessageBox(_T("Error Creating Dialog Object"));} }4.3 DoModal函數(shù)
virtual INT_PTR DoModal(); 功能:調用此成員函數(shù)以調用模式對話框并在完成后返回對話框結果。 參數(shù):無 返回值:一個 int 值,該值指定傳遞給 CDialog:: EndDialog成員函數(shù)的 n 結果 參數(shù)的值,該參數(shù)用于關閉對話框。 如果函數(shù)無法創(chuàng)建對話框,則返回值為-1; 如果發(fā)生其他錯誤,則返回值為 IDABORT,在這種情況下,"輸出" 窗口將包含來自 GetLastError的錯誤信息。 參考示例: void CMyDialog::OnMenuShowAboutDialog() {// Construct the dialog box passing the// ID of the dialog template resourceCDialog aboutDlg(IDD_ABOUTBOX);// Create and show the dialog boxINT_PTR nRet = -1;nRet = aboutDlg.DoModal();// Handle the return value from DoModalswitch (nRet){case -1:AfxMessageBox(_T("Dialog box could not be created!"));break;case IDABORT:// Do somethingbreak;case IDOK:// Do somethingbreak;case IDCANCEL:// Do somethingbreak;default:// Do somethingbreak;}; }4.4 EndDialog函數(shù)
void EndDialog(int nResult); 功能:調用此成員函數(shù)以終止模式對話框。 參數(shù): N 結果包含要從對話框返回給調用方的值 DoModal 。參考示例: void CMyDialog::OnMenuShowSimpleModal() {CSimpleDlg myDlg;INT_PTR nRet = myDlg.DoModal();if (nRet == IDOK || nRet == 5){AfxMessageBox(_T("Dialog closed successfully"));} }void CSimpleDlg::OnRButtonUp(UINT nFlags, CPoint point) {UNREFERENCED_PARAMETER(nFlags);// Do somethingint nRet = point.x; // Just any value would do!EndDialog(nRet); // This value is returned by DoModal!// Do somethingreturn; // Dialog closed and DoModal returns only here! }4.5 OnCancel函數(shù)
virtual void OnCancel(); 功能:當用戶在模式對話框或無模式對話框中單擊 " 取消 " 或按 ESC 鍵時,框架會調用此方法。參考示例: 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(); }4.6 OnInitDialog函數(shù)
virtual BOOL OnInitDialog(); 功能:調用此方法以響應 WM_INITDIALOG 消息。 參數(shù):返回值:指定應用程序是否已將輸入焦點設置到對話框中的某個控件。 如果 OnInitDialog 返回非零值,則 Windows 將輸入焦點設置到對話框中的第一個控件的默認位置。 僅當應用程序已將輸入焦點顯式設置到對話框中的某個控件時,應用程序才能返回0。參考示例: BOOL CSimpleDlg::OnInitDialog() {CDialog::OnInitDialog();// TODO: Add extra initialization herem_cMyEdit.SetWindowText(_T("My Name")); // Initialize control valuesm_cMyList.ShowWindow(SW_HIDE); // Show or hide a control, etc.return TRUE; // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE }4.7 OnOK函數(shù)
virtual void OnOK(); 功能:當用戶單擊 "確定" 按鈕時調用 () ID 為 IDOK 的按鈕。參考示例: void CSimpleDlg::OnOK() {// TODO: Add extra validation here// Ensure that your UI got the necessary input// from the user before closing the dialog. The// default OnOK will close this.if (m_nMyValue == 0) // Is a particular field still empty?{// Inform the user that he can't close the dialog without// entering the necessary values and don't close the// dialog.AfxMessageBox(_T("Please enter a value for MyValue"));return;}CDialog::OnOK(); // This will close the dialog and DoModal will return. }05. 附錄
總結
以上是生活随笔為你收集整理的【MFC】CDialog类详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【MFC】MFC对话框类
- 下一篇: 【Tools】Visual Studio