Skip to content

wusaneryi/WPF-Task-Dialog

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WPF Task Dialog Wrapper

It is a drop-in assembly that lets you call up Task Dialogs in your WPF app. Uses the native TaskDialogIndirect API calls when available (Vista/7) and falls back to an emulated WPF dialog when not (XP and earlier).

Now available via NuGet! Search for "taskdialog" and give it a whirl!

Background

I've made very good use of Hedley Muscroft's WinForms emulator and wrapper for TaskDialogs in the past. In that same spirit, I've taken part of his hard work and done something very similar, but this time for WPF and .NET 4.0.

There are tons of other WPF/WinForms TaskDialog implementations out there, either those that wrap the Win32 APIs, or just providing a custom WPF dialog to use. A quick web search will find you plenty. What they don't give you is a unified way to use the native that still works on older Windows. I go into more detail on the rationale and history of all this in a blog post here.

Hedley's work is great and I've used it plenty of times, but I wanted something very similar that worked for WPF apps. I also thought I might take the time to try to improve on it, mostly thanks to the power of WPF and .NET 4.0.

Code

Add the reference to your WPF project. You'll find all of the classes under the TaskDialogInterop namespace. Use the TaskDialog class's static methods to show dialogs. Some work more akin to the more basic MessageBox, accepting a string and other parameters to customize the display. The real power, though, is found in the overload that takes a TaskDialogOptions object.

The basic flow in your apps will typically be something like this:

TaskDialogOptions config = new TaskDialogOptions();

config.Owner = this;
config.Title = "Task Dialog Title";
config.MainInstruction = "The main instruction text for the TaskDialog goes here";
config.Content = "The content text for the task dialog is shown " + 
             "here and the text will automatically wrap as needed.";
config.ExpandedInfo = "Any expanded content text for the " + 
                  "task dialog is shown here and the text " + 
                  "will automatically wrap as needed.";
config.VerificationText = "Don't show me this message again";
config.CustomButtons = new string[] { "&Save", "Do&n't save", "&Cancel" };
config.MainIcon = VistaTaskDialogIcon.Shield;
config.FooterText = "Optional footer text with an icon can be included.";
config.FooterIcon = VistaTaskDialogIcon.Warning;

TaskDialogResult res = TaskDialog.Show(config);

The options object lets you specify all of the particulars. Anything you don't define will be ignored or unused. This makes it very easy to display as much or as little as you need to.

As you can see, buttons are handled via simple string arrays. Each string in the array will be converted to a button using the given label. Use the ampersand character (&) before a letter to denote the accelerator.

Depending on the method you use, you'll either get back a TaskDialogSimpleResult or a TaskDialogResult. The simple one works just like the DialogResult you're probably used to. In fact, it can be cast successfully back and forth between the two. This is nice if you have existing services/code that expect a DialogResult. The full result object contains everything you'd want to know about how the user acted on your dialog. It has properties you can read to find out which button, command link, radio, etc. was chosen/clicked.

You can read more as well as see screenshots here.

Details

  • Project created in Visual Studio 2010
  • .NET 4.0 Client Profile
  • Uses the native TaskDialogIndirect API when available (Vista/7)
  • Licensed under The Code Project Open License (CPOL) 1.02
    • Can be freely redistributed
    • Can be modified
    • Can be used in commercial software
    • No warranty/guarantee

About

A TaskDialog wrapper class with fallback emulator (for XP and earlier).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%