How to use TaskDialog API in C#


The TaskDialog API replaces MessageBox. A message box is useful for prompting users for an acknowledgment, confirmation, or an answer to a yes or no question. Message boxes are popular because of the MessageBox function is convenient for developers to use. TaskDialog is the preferred API to use because it is similar to MessageBox but much more flexible. Previously, developers have created their own message box implementations when greater functionality was required. Unfortunately .Net framework doesn’t expose TaskDialog API directly; you need to use WIN32 api for it. Here is one Task dialog implementation in C#. Only limitation of Task Dialog API is it doesn’t supported by Windows operating systems less than Windows Vista.

Here is the API declarations.

[DllImport("comctl32.dll", CharSet = CharSet.Unicode, EntryPoint = "TaskDialog")]
static extern int TaskDialog(IntPtr hWndParent, IntPtr hInstance, String pszWindowTitle,
String pszMainInstruction, String pszContent, int dwCommonButtons, 
IntPtr pszIcon, out TaskDialogResult pnButton);

//Dialog buttons
public enum TaskDialogButtons : int
    Ok = 0x0001,
    Cancel = 0x0008,
    Yes = 0x0002,
    No = 0x0004,
    Retry = 0x0010,
    Close = 0x0020

//Dialog Results
public enum TaskDialogResult : int
    IDOK = 1,
    IDCANCEL = 2,
    IDRETRY = 4,
    IDYES = 6,
    IDNO = 7,
    IDCLOSE = 8,
    NONE = 0

//Dialog Icons
public enum TaskDialogIcon
    Information = UInt16.MaxValue - 2,
    Warning = UInt16.MaxValue,
    Stop = UInt16.MaxValue - 1,
    Question = 0,
    SecurityWarning = UInt16.MaxValue - 5,
    SecurityError = UInt16.MaxValue - 6,
    SecuritySuccess = UInt16.MaxValue - 7,
    SecurityShield = UInt16.MaxValue - 3,
    SecurityShieldBlue = UInt16.MaxValue - 4,
    SecurityShieldGray = UInt16.MaxValue - 8

And here is my TaskDialog wrapper function.

public static TaskDialogResult Show(IntPtr handle, string messageTitle, string mainTitle,
    string mainContent, TaskDialogButtons taskDialogButtons, TaskDialogIcon taskDialogIcon)
    TaskDialogResult buttonClicked = TaskDialogResult.IDCANCEL;
    TaskDialog(handle, IntPtr.Zero, messageTitle, mainTitle, mainContent,
        (int)taskDialogButtons, (IntPtr)(int)taskDialogIcon, out buttonClicked);
    return buttonClicked;

And here is the screen shot of Task Dialog running in my Windows 7 machine.

Task Dialog - On Windows 7 machine

Task Dialog - On Windows 7 machine

Anuraj is currently working as Technical Lead. He is a regular speaker on Technology events. He has more than 11+ years experience with Microsoft.Net Technologies.

Facebook Twitter LinkedIn Google+ 

  • Confused


    First of all I like to say Good job, second, an example of how to use the wrapper would be great.

    • Anuraj P

      Hi Confused.

      Its simple. You can do like this.

      Show(this.Handle, messageTitle, mainContent, messageContent, TaskDialogButtons.Ok, TaskDialogIcon.SecuritySuccess);

  • Jorge

    What if you don’t have a parent window?

    • Anuraj P

      I don’t think it will work. I will confirm once and update it.