Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

Vista Task Dialog and Emulating It Redux

Rate me:
Please Sign up or sign in to vote.
4.57/5 (15 votes)
5 Oct 2008BSD3 min read 48.1K   897   37   13
Revisiting Vista Task Dialog and emulating it.

Introduction

This article intends to try and emulate the Task Dialog in the older Windows platform and not lose a lot of functionality of the Task Dialog. We will be using the excellent managed code of TaskDialog for WinForms by KevinGre. His article will be a required reading as this is not a wrapper of his code. Instead we will use his code then detect if Task Dialog is available. If it is not available, we will try to create our own Task Dialog.

This article also uses a class “CommandButton.cs” from Vista TaskDialog Wrapper and Emulator by Hedly Muscroft.

Since there is a Task Dialog emulator made by Hedly Muscroft, why make another? Don’t get me wrong, Hedly Muscroft’s emulator is very good. In fact I took some of his ideas to make this emulator.

But his emulator doesn’t provide a lot of functionality that the original Task Dialog has (unless I'm mistaken, my apologies), for example notifications (callback) and messages (actions), which gives feedback on links that was clicked or setting the progress bar value. I intend to try to solve it.

Emulating the Task Dialog

As you know Task Dialog supports notifications. This allows Task Dialog to inform the caller on the state of the Task Dialog, like how much time has passed since the Task Dialog is shown, or whether a user clicked a link. Note that I did not implement all the notifications. The basic ones implemented are link clicked and the timer.

The following code snippet will show you how notifications are used to send information back. 

C#
private void timer_Tick ( object sender, EventArgs e )
{
    this.timerTickCount += (uint)this.timer.Interval;

    TaskDialogNotificationArgs notificationArgs = new TaskDialogNotificationArgs ( );
    notificationArgs.Notification = TaskDialogNotification.Timer;
    notificationArgs.TimerTickCount = this.timerTickCount;

    if ( this.taskDialog.Callback ( new ActiveTaskDialog ( this.Handle ),
         notificationArgs, null ) )
    {
        // Reset timer.
        this.timerTickCount = 0;
    }
}

Task Dialog also allows you to change the progress bar value, or change whether a button on the Task Dialog is enable or disable and so on. KevinGre’s code (“ActiveTaskDialog.cs”), or the MSDN documentation as referenced below, describes all the possible messages that the Task Dialog can receive.

Note that I have not implemented all the messages the emulated Task Dialog can receive. Only setting the progress bar value message is implemented.

If you need to recieve more messages, the following code snippet describes how to do so.

C#
protected override void WndProc ( ref Message m )
{
    switch ( m.Msg )
    {
        case (int)UnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_PROGRESS_BAR_POS:
            if ( this.progressBar != null )
            {
                if ( m.WParam.ToInt32 ( ) <= this.progressBar.Maximum )
                {
                    this.progressBar.Value = m.WParam.ToInt32 ( );
                }
            }
            break;
        default:
            base.WndProc ( ref m );
            break;
    }
}

Using the code

To call the Task dialog is EXACTLY the same as KevinGre’s article. In fact, the demo uses the exact same code as KevinGre’s. The difference is that I moved the check of whether the Vista Task Dialog is available to TaskDialog.Show () itself. If Vista Task Dialog is not supported, the emulated Task Dialog will be called instead.

What I will show is the emulated Task dialog on Windows XP, note that all screenshots in this article are taken in Windows XP. The simplest usage for the code:

C#
TaskDialog taskDialog = new TaskDialog ( );
taskDialog.WindowTitle = "My Application";
taskDialog.MainInstruction = "Do you want to do this?";
taskDialog.CommonButtons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No;
int result = taskDialog.Show ( );
if ( result == (int)DialogResult.Yes )
{
   // Do it.
}

TaskDialogRedux03.png

Complex usage:

C#
TaskDialog taskDialog = new TaskDialog ( );
taskDialog.WindowTitle = "My Application";
taskDialog.MainInstruction = "Do you want to do this?";

taskDialog.EnableHyperlinks = true;
taskDialog.Content =
      "If you do this there could be all sorts of consequnces. "
      +
      "If you don't there will be other consequences. "
      +
      "You can <a href='http://www.codeproject.com/Learn/'>"
      +
      "learn more about those consequences</a> or more "
      +
      "about <a href='http://www.codeproject.com/blah/'>"
      +
      "blah blah blah</a>.";
taskDialog.Callback = new TaskDialogCallback ( this.MyTaskDialogCallback );

taskDialog.VerificationText = "Don't ask me this ever again.";
taskDialog.VerificationFlagChecked = false;
taskDialog.UseCommandLinks = true;

TaskDialogButton doItButton = new TaskDialogButton ( );
doItButton.ButtonId = 101;
doItButton.ButtonText = "Do It.\nThis is an extra line?";

TaskDialogButton dontDoItButton = new TaskDialogButton ( );
dontDoItButton.ButtonId = 102;
dontDoItButton.ButtonText = "Don't Do It";

taskDialog.Buttons = new TaskDialogButton[] { doItButton, dontDoItButton };

bool dontShowAgain;
int result = taskDialog.Show ( null, out dontShowAgain );
if ( dontShowAgain )
{
                // Suppress future asks.
}
if ( result == doItButton.ButtonId )
{
                // Do it.
}
TaskDialogRedux04.png

More Screenshots

TaskDialogRedux01.png

TaskDialogRedux05.png

TaskDialogRedux02.png

Conclusion

I hope this article will help someone who is looking in implementing a fully functional emulated Task Dialog.

References

History

2008 October 5

  • First published.

2008 October 7

  • Added VS2005 source files.
  • Added a new screenshot.

2008 October 9

  • Fixed a couple of bugs where a grey bar will be shown when not showing the sub controls area (custom/common buttons, verification checkbox, expanded info button) and/or footer area. (With thanks to Loic Berthollet)

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUndocumented Icon Styles Pin
Trevor Davis Canada9-May-09 13:36
Trevor Davis Canada9-May-09 13:36 
QuestionCustom Icon and image Pin
Holpit5-Apr-09 22:49
Holpit5-Apr-09 22:49 
GeneralSystem.EntryPointNotFoundException, "wrong" ComCtl32 loaded until a Form is up and running... Pin
Chris Richner18-Jan-09 20:07
Chris Richner18-Jan-09 20:07 
GeneralRe: System.EntryPointNotFoundException, "wrong" ComCtl32 loaded until a Form is up and running... Pin
MarcDrexler7-Jun-09 5:37
MarcDrexler7-Jun-09 5:37 
GeneralRe: System.EntryPointNotFoundException, "wrong" ComCtl32 loaded until a Form is up and running... Pin
Member 393600516-Oct-09 4:15
Member 393600516-Oct-09 4:15 
QuestionCallback function Pin
melnac9-Jan-09 21:55
melnac9-Jan-09 21:55 
GeneralCongratulations & Remark Pin
Loic Berthollet9-Oct-08 1:51
Loic Berthollet9-Oct-08 1:51 
Hi,

Firstly, congratulations for that great job.

I have a remark: When you
- Remove the Verify Checkbox Text
- Define 2 custom buttons (Do it, Don't do it)
- Use Command Links
- Use none of predefined button
- Remove Expanded Info text

you'll got a grey strip at the botton of the window, at the top of the footer text area.

Kind Regards.
Loïc

Loïc Berthollet

GeneralRe: Congratulations & Remark Pin
blindwaves9-Oct-08 3:44
blindwaves9-Oct-08 3:44 
GeneralUnable to open the solution in VS2005 Pin
ken.rountree6-Oct-08 14:20
ken.rountree6-Oct-08 14:20 
GeneralRe: Unable to open the solution in VS2005 Pin
blindwaves7-Oct-08 5:15
blindwaves7-Oct-08 5:15 
QuestionDark colors? Pin
Emil - Gabriel5-Oct-08 22:46
Emil - Gabriel5-Oct-08 22:46 
AnswerRe: Dark colors? Pin
blindwaves7-Oct-08 5:14
blindwaves7-Oct-08 5:14 
GeneralRe: Dark colors? Pin
Emil - Gabriel7-Oct-08 20:20
Emil - Gabriel7-Oct-08 20:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.