CTaskDialog 简介en 包含头文件afxtaskdialog.h MFC10.0 Visual C++ 2010

CTaskDialog 简介en 包含头文件afxtaskdialog.h MFC10.0 Visual C++ 2010
2010-01-06 19:13

The new MFC library from Visual Studio 2010 supports another Vista specific feature, the task dialog. This is meant as a replacement for the classic dialog box, and can display command links, customized buttons, customized icons, and a footer.
Here is an example of such a dialog (from Windows Server 2008).

MFC contains a new class called CTaskDialog that implements the task dialog. You must include afxtaskdialog.h in your files to be able to use it. Since the task dialog is available only on Vista or newer versions (Server 2003, Server 2008, Windows 7) you must check whether you can use it or not. For that the class CTaskDialog provides a static method called IsSupported() that returns true if the task dialog is available on the running operating system. In addition, the task dialog is only available when you build for UNICODE.

The code below shows how to create and display the task dialog from the previous image.

void CTasksDemoDlg::OnBnClickedButtonTasks1()
{
CString strMessage("Do you want to save your changes to the document?");
CString strDialogTitle("Save document");
CString strMainInstruction("Save document options");

CString expandedLabel("Hide extra information");
CString collapsedLabel("Show extra information");
CString expansionInfo("You can select to save your document either as XML or binary. You should prefer to save as XML as this is the new standard format.");

if (CTaskDialog::IsSupported())
{
CTaskDialog taskDialog(strMessage, strMainInstruction, strDialogTitle, TDCBF_OK_BUTTON);
taskDialog.SetMainIcon(TD_INFORMATION_ICON);

taskDialog.SetCommonButtons (TDCBF_NO_BUTTON | TDCBF_CANCEL_BUTTON);
taskDialog.LoadCommandControls (IDS_SAVE_OPTION1, IDS_SAVE_OPTION2);
taskDialog.SetExpansionArea (expansionInfo, collapsedLabel, expandedLabel);
taskDialog.SetFooterText (L"Note: If you don't chose to save your changes will be lost.");
taskDialog.SetVerificationCheckboxText (L"Remember your selection");

INT_PTR result = taskDialog.DoModal();

if (taskDialog.GetVerificationCheckboxState() )
{
// PROCESS IF the user selects the verification checkbox
}

switch (result)
{
case IDS_SAVE_OPTION1:
AfxMessageBox(L"You chose to save as XML");
break;
case IDS_SAVE_OPTION2:
AfxMessageBox(L"You chose to save as binary");
break;
case IDNO:
AfxMessageBox(L"You chose not to save");
break;
case IDCANCEL:
AfxMessageBox(L"You chose to cancel");
break;
default:
// this case should not be hit
ASSERT(FALSE);
break;
}

}
else
{
AfxMessageBox(strMessage);
}
}
In this sample IDS_SAVE_OPTION1 (”Save in XML based format”) and IDS_SAVE_OPTION2 (”Save in binary format (old version)”) are two strings defined in the string table from the Resource editor.
There are several predefined icons in commctrl.h that can be used as the main icon.
#define TD_WARNING_ICON MAKEINTRESOURCEW(-1)
#define TD_ERROR_ICON MAKEINTRESOURCEW(-2)
#define TD_INFORMATION_ICON MAKEINTRESOURCEW(-3)
#define TD_SHIELD_ICON MAKEINTRESOURCEW(-4)
The following flags for default buttons are defined in the same header:
enum _TASKDIALOG_COMMON_BUTTON_FLAGS
{
TDCBF_OK_BUTTON = 0x0001, // selected control return value IDOK
TDCBF_YES_BUTTON = 0x0002, // selected control return value IDYES
TDCBF_NO_BUTTON = 0x0004, // selected control return value IDNO
TDCBF_CANCEL_BUTTON = 0x0008, // selected control return value IDCANCEL
TDCBF_RETRY_BUTTON = 0x0010, // selected control return value IDRETRY
TDCBF_CLOSE_BUTTON = 0x0020 // selected control return value IDCLOSE
};
typedef int TASKDIALOG_COMMON_BUTTON_FLAGS;
An easier way to create a task dialog, but with fewer customization options is using the static method ShowDialog() from CTaskDialog. The following example displays a dialog similar to the first one.
void CTasksDemoDlg::OnBnClickedButtonTasks2()
{
HRESULT result2 = CTaskDialog::ShowDialog(
L"Do you want to save your changes to the document?",
L"Save document options",
L"Save document",
IDS_SAVE_OPTION1,
IDS_SAVE_OPTION2,
TDCBF_NO_BUTTON | TDCBF_CANCEL_BUTTON,
TDF_ENABLE_HYPERLINKS | TDF_USE_COMMAND_LINKS,
L"Note: If you don't chose to save your changes will be lost.");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值