本演练介绍了 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 或更高版本
将 Windows 消息框替换为 CTaskDialog
下面演示了 CTaskDialog
的最基本用法,即替换 Windows 消息框。 此示例还会更改与任务对话框关联的图标。 更改图标会使 CTaskDialog
看起来类似于 Windows 消息框。
使用 MFC 应用程序向导 创建一个具有所有默认设置的 Microsoft 基础类(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
。 示例代码演示如何根据用户的选择执行特定说明。
通过 视图>>导航到 资源视图。
将 资源视图 展开到 字符串表 文件夹中。 展开它,并双击 "字符串表"。
滚动到字符串表底部并添加新条目。 将 ID 更改为
TEMP_LINE1
。 将标题设置为Command Line 1
。添加另一个新条目。 将 ID 更改为
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 类。