CTaskDialog in MFC in Visual C++ 2010

Windows Vista introduced the concept of Task Dialogs. Those are a powerful replacement for the standard message boxes. The following is a list of features supported by task dialogs:

  • Custom icon.
  • A header text that can spread over multiple lines.
  • A contents text that can spread over multiple lines.
  • An option to show a progress bar.
  • Options to show a list of radio buttons.
  • Options to show a list of command buttons.
  • An option to display an expand/collapse button to display/hide additional information.
  • An option to show a checkbox.
  • A footer text that can spread over multiple lines.
  • Support for hyperlinks within almost any part.
  • Support for a timer.

Almost everything above is optional to allow you to customize the look exactly like you want it.

The MFC in Visual C++ 2010 has a new class called CTaskDialog that is a wrapper around the task dialog API. It makes it pretty easy to show a task dialog. Using this new class requires a Unicode build of your project. Unfortunately, there is no fallback mechanism for older versions of Windows. If you run an application using the CTaskDialog class on Windows XP, you will get an error at the time the dialog should appear. If your application needs to run on older versions of Windows, you can use the CTaskDialog::IsSupported() static function to determine if task dialogs are supported or not and provide your own fallback.

This new class can be used as follows in its most simple form:

CTaskDialog dlg(_T("The header"), _T("The contents."), _T("The title."));
dlg.DoModal();

This will show the following simple task dialog.

The demo application which you can download below enables all optional options and the result looks like the following screenshot.

In my example I’m displaying a progressbar. To update the progressbar based on a timer, I derived a CMyTaskDialog from CTaskDialog and implemented the following virtual OnTimer function:

HRESULT CMyTaskDialog::OnTimer(_In_ long lTime)
{
    m_iProgressPos += 2;
    if (m_iProgressPos >= 100)
        m_iProgressPos = 0;
    SetProgressBarPosition(m_iProgressPos);
    return S_OK;
}

The OnTimer function will be called roughly every 200 milliseconds. In my OnTimer function I update the position of the progressbar. You could also update the look of the progressbar to the error state using the SetProgressBarState function.

See the MSDN page for more details about the CTaskDialog class.

The Visual Studio 2010 project below is a demo application of using task dialogs.

CTaskDialogDemo.zip

Share

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: