Over a million developers have joined DZone.

Using the new TaskDialog via WinAPI

·

With Windows Vista (and now with Windows 7) a replacement has been introduced for the standard MessageBox. It is called the TaskDialog. It is an advanced version of the MessageBox control , adding more customization and message types. If you used Windows Vista/7 for a while, you’ve probably seen the security popups as well as messages generated by the latest native Windows applications – those look nothing like the old style messages.

In this article I am going to focus on a more basic version of the custom task dialog (without custom options), however it should give you a solid understanding of what the control can do and how to use it instead of the regular MessageBox.

There are two ways a developer can go to display a TaskDialog – use a wrapper or call WinAPI directly. I am that kind of developer that tries to avoid wrappers unless necessary, simply to avoid yet another dependency layer and eliminate a possible source of bugs (I am not implying that wrappers will necessarily carry bugs, but there is such a possibility). Therefore, in this article I am going to show how to call the TaskDialog directly.

First of all, the DLL signature should be inserted in the code:

[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 int pnButton);

Here I am creating a simple TaskDialog that will display a message, without an icon and with the OK button as the only option:

int result;
TaskDialog(this.Handle, IntPtr.Zero, "Sample TaskDialog", "This is the header", "Contents here", 1, new IntPtr(0), out result);

The sixth parameter represents the set of buttons to be shown in the dialog. The button indexes are shown below:

OK – 1
Cancel – 8
Yes – 2
No – 4
Retry – 10
Close – 20

These indexes can be used together. For example, if I want to put both OK and Cancel, I will write the statement like this:

TaskDialog(this.Handle, IntPtr.Zero, "Sample TaskDialog", "This is the header", "Contents here", 1 | 8, new IntPtr(0), out result);

Note that you can only have one instance of a button of a specific type in a TaskDialog. Therefore, repeating an index won’t add multiple buttons of the same types.

The clicked button will be returned in the result variable.

The sample dialog will look like this:

This is quite simple. Let’s get some icons on it, that will be displayed depending of the type of the message. At this moment, there are ten types you can use. I am going to show each one of them, with their index as well as with a screenshot of how it looks like below.

Question (0)

Information (UInt16.MaxValue -2)

Warning (UInt16.MaxValue)

Stop (UInt16.MaxValue -1)

Security Error (UItn16.MaxValue – 6)
 

Security Warning (UInt16.MaxValue – 5)

Security Success (UInt16.MaxValue – 7)
 

Security Shield (UInt16.MaxValue - 3)
 

Security Shield (Blue) (UInt16.MaxValue – 4)
 

Security Shield (Gray) (UInt16.MaxValue – 8)
 

The index is passed as IntPtr (seventh parameter) with the help of a statement like new IntPtr ((int)value). Therefore, I can have a statement like this to display an error:

TaskDialog(this.Handle, IntPtr.Zero, "Sample TaskDialog", "This is the header", "Contents here", 1 | 8, new IntPtr((int)UInt16.MaxValue - 1), out result);

This pretty much covers the TaskDialog and its basic usage. You can detect the pressed button by the returned result, so you can wire it to an event handler.
The reason why I was not using enums in this case to bundle all possible dialog styles and buttons is because I am not creating a wrapper. For example, if I am only going to use the TaskDialog once in the application (that is less likely to happen but still possible), then I don’t need the additional code baggage for unused types. If you are sure that you will be using multiple styles as well as button combinations, it would be a good idea to create a separate class with the above mentioned enums.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}