Windows 7 Development: Working With Task Dialog Class

Last year, I blogged about few things related to Windows 7 development which includes Jump Lists and Glass Form. The Windows 7 API Code Pack comes with many other features which can be used while developing applications for Windows 7 platform. The Windows 7 API Code Pack comes with some sample applications which exposes the power of the API.

In one of my previous projects I used the Task Dialog class to use some cool looking message boxes like the one we see in Windows 7. If you see the sample code and and the executable, they are working fine but when you write the same code in the code in your application, this will not turn out to be as you would have expected. You will see a runtime error as the one below.

Windows 7 API exception

This problem can be resolved by adding a app.manifest file to your project and modifying some part of it. Move to the end of the file and un-comment the <dependency> section of the file.


Hit F5 again and this will do the trick for you, this time you will be able to view the message box as you wanted to. The API sample contains code for all the possible / available task dialog for you to use. But the usage is bit difficult as this cannot be acaieved in a single line like we do with the MessageBox class. Therefore I suggest you to build a helper class / function to get this easy for you. As I used this in one of my projects I have two functions one for showing the information and error messages respectively. You can also have a overload method for this, but as I have used different method names instead as I will be using only two dialogs throughout my project.

For Error Messages:

public static void ShowErrorMessage(string ErrorTitle, string Instruction, string ErrorMessage, string ErrorTrace)
            TaskDialog tdError;
            tdError = new TaskDialog();
            tdError.DetailsExpanded = false;
            tdError.Cancelable = true;
            tdError.Icon = TaskDialogStandardIcon.Error;
            tdError.Caption = ErrorTitle;
            tdError.InstructionText = Instruction;
            tdError.Text = ErrorMessage;
            tdError.DetailsExpandedLabel = "Hide details";
            tdError.DetailsCollapsedLabel = "Show details";
            tdError.DetailsExpandedText = ErrorTrace;

All the properties in the above function is self-explainatory. The code at line:12 will have a expander in the dialog box which will contain more details of the exception.

Function for showing information is also the same but with few things “missing”:

public static void ShowInfoMessage(string InfoTitle, string Information, string Message)
            TaskDialog tdInfo;
            tdInfo = new TaskDialog();
            tdInfo.DetailsExpanded = false;
            tdInfo.Cancelable = true;
            tdInfo.Icon = TaskDialogStandardIcon.Information;
            tdInfo.Caption = InfoTitle;
            tdInfo.InstructionText = Information;
            tdInfo.Text = Message;

Sample Usage:

//For Error Dialog
ShowErrorMessage("Application Error", "There was an error in creating XML file", ex.Message, ex.StackTrace);
//For Information Dialog
ShowInfoMessage("Application Name", "Image more than 32x32 pixels will be rejected", "Please check the image pixel before you use choose a package icon");

Error Dialog Box

Windows 7 API error message dialog box

Click the Show Details expander icon to view the complete error stack trace.

Information Dialog Box

Windows 7 API information dialog box

This is just the two types of dialogs I have used for my project, but there are more supported in the API. So to get started see the sample project accompanying the API Code Pack including the one you see while running programs as Administrator while UAC is turned on, a dialog box with a progress bar and lots of more to check. The samples above would get you started.

If you are a Visual Studio 2010 user with NuGet installed, then you can add the API reference to your project with the below command.

Install-Package Windows7APICodePack
comments powered by Disqus