本逐步解說會介紹 CTaskDialog 類別,並示範如何將它新增至您的應用程式。
CTaskDialog 是一個工作對話框,可取代 Windows Vista 或更新版本中的 Windows 消息框。
CTaskDialog 會改善原始消息框,並新增功能。 Visual Studio 仍然支援 Windows 消息框。
注意
早於 Windows Vista 的 Windows 版本不支援 CTaskDialog。 如果您想要向在舊版 Windows 上執行應用程式的使用者顯示訊息,您必須撰寫替代對話框選項的程式。 您可以使用靜態方法 CTaskDialog::IsSupported 來判斷在執行期間使用者的電腦是否可以顯示 CTaskDialog。 此外,只有使用 Unicode 函式庫建置的應用程式才能使用 CTaskDialog。
CTaskDialog 支援數個選擇性元素來收集和顯示資訊。 例如,CTaskDialog 可以顯示命令連結、自定義按鈕、自定義圖示和頁尾。
CTaskDialog 也有數種方法可讓您查詢工作對話框的狀態,以判斷用戶選取的選擇性元素。
先決條件
您需要下列元件才能完成本操作指南:
- Visual Studio 2010 或更新版本
- Windows Vista 或更新版本
以 CTaskDialog 取代 Windows 消息框
下列示範 CTaskDialog的最基本用法,也就是取代 Windows 消息框。 本範例也會變更與工作對話框相關聯的圖示。 變更圖示會使 CTaskDialog 看起來與 Windows 消息框類似。
使用 MFC 應用程式精靈,建立具有所有預設設定的 Microsoft Foundation Classes (MFC) 應用程式。 請參閱《使用新的 MFC Shell 控制項逐步解說》中有關如何開啟您版本的 Visual Studio 精靈的指示。
將它命名為 MyProject。
使用 方案總管 開啟
MyProject.cpp。在包含清單之後新增
#include "afxtaskdialog.h"。尋找 方法
CMyProjectApp::InitInstance。 在return TRUE;語句之前插入下列幾行程序代碼。 此程式代碼會建立我們在 Windows 訊息框或CTaskDialog中使用的字串。CString message("My message to the user"); CString dialogTitle("My Task Dialog title"); CString emptyString; // Check whether the user's computer supports `CTaskDialog`. // If not, display a Windows message box instead. if (CTaskDialog::IsSupported()) { CTaskDialog taskDialog(message, emptyString, dialogTitle, TDCBF_OK_BUTTON); taskDialog.SetMainIcon(TD_WARNING_ICON); // Set the icon to be the same as the Windows message box taskDialog.DoModal(); } else { AfxMessageBox(message); }
編譯並執行應用程式。 應用程式會在啟動後顯示工作對話框。
將功能新增至 CTaskDialog
下列說明如何將功能添加至您在之前程序中建立的 CTaskDialog。 範例程式會示範如何根據使用者的選擇執行特定指令。
透過 檢視>>,導航至 資源檢視。
展開 [資源檢視] 至 [字串數據表] 資料夾。 展開它,然後按兩下 String Table。
捲動至字串表底部並新增一個條目。 將識別碼變更為
TEMP_LINE1。 標題設定為Command Line 1。新增另一個新條目。 將識別碼變更為
TEMP_LINE2。 標題設定為Command Line 2。請返回
MyProject.cpp。在
CMyProjectApp::InitInstance()函式中,在CString emptyString;之後,新增下列程式碼:CString expandedLabel("Hide extra information"); CString collapsedLabel("Show extra information"); CString expansionInfo("This is the additional information to the user,\nextended over two lines.");尋找
taskDialog.DoModal()語句,並以下列程式代碼取代該語句。 此程式代碼會更新工作對話框,並新增新的控制項:taskDialog.SetMainInstruction(L"Warning"); taskDialog.SetCommonButtons(TDCBF_YES_BUTTON | TDCBF_NO_BUTTON | TDCBF_CANCEL_BUTTON); taskDialog.LoadCommandControls(TEMP_LINE1, TEMP_LINE2); taskDialog.SetExpansionArea(expansionInfo, collapsedLabel, expandedLabel); taskDialog.SetFooterText(L"This is a small footnote to the user"); taskDialog.SetVerificationCheckboxText(L"Remember your selection"); INT_PTR result = taskDialog.DoModal(); if (taskDialog.GetVerificationCheckboxState()) { // Your code if the user selects the verification checkbox } switch (result) { case TEMP_LINE1: // PROCESS IF the first command line break; case TEMP_LINE2: // PROCESS IF the second command line break; case IDYES: // PROCESS IF the user clicks yes break; case IDNO: // PROCESS IF the user clicks no break; case IDCANCEL: // PROCESS IF the user clicks cancel break; default: // This case should not be hit because closing // the dialog box results in IDCANCEL break; }
編譯並執行應用程式。 應用程式會顯示使用新控制項和其他資訊的工作對話框。
顯示 CTaskDialog 而不建立 CTaskDialog 物件
下列示範如何顯示 CTaskDialog,而不先建立 CTaskDialog 物件。 此範例會繼續先前的程式。
顯示 CTaskDialog 而不建立 CTaskDialog 物件
開啟
MyProject.cpp檔案。在
CMyProjectApp::InitInstance()函式中,流覽至if (CTaskDialog::IsSupported())語句的右括弧。在
if語句的右括弧正前插入下列程式代碼(在else區塊之前):HRESULT result2 = CTaskDialog::ShowDialog(L"My error message", L"Error", L"New Title", TEMP_LINE1, TEMP_LINE2);
編譯並執行應用程式。 應用程式會顯示兩個工作對話框。 第一個對話框來自 將功能新增至 CTaskDialog 程式;第二個對話框來自上一個程式。
這些範例不會示範 CTaskDialog的所有可用選項,但應該可協助您開始使用。 如需類別的完整描述,請參閱 CTaskDialog 類別。