.NET Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Using The Windows 7/8 Taskbar Progress Indicator Outside The WPF Stack

10.15.2012
| 4809 views |
  • submit to reddit

Have you seen that progress indicator in the Windows 7/8 taskbar? If you used Windows 7 or 8 at least for a day, chances are you did. Did you like it? I surely did. It is handy to see the progress of an operation right in the taskbar, without having to switch to a different window.

Implementing this in a .NET desktop application isn’t hard if you are using the Windows API Code Pack for .NET Framework (otherwise, you would have to directly use the WinAPI), given that you are working outside the boundaries of the WPF stack.

First of all, you either need to reference the Core and Shell projects (included in the Windows API Code Pack) in your application (by adding the projects to the solution and then creating proper references) or separately open the above mentioned projects and compile the DLLs. I prefer the second way, since you won’t have another two projects floating in your solution and it will be easier to handle the references later. Once you set the references, you are ready to go.

In your application, add the proper references to the Windows API Code Pack namespaces, used for this example:

using Microsoft.WindowsAPICodePack.Taskbar; 
using Microsoft.WindowsAPICodePack.Shell; 

I will put some test code in a temporary event handler - my code went in a Button_Click event in a WinForms application.

A good practice in this situation is to check whether the client machine supports the new taskbar capabilities, because chances are the application is running on an older Windows release. To do this, use the following snippet:

if (TaskbarManager.IsPlatformSupported) 
{ 
// Set of events if taskbar features are supported 
} 
else 
{
// Set of events if the taskbar features are not supported 
} 

If the taskbar is supported, you could use this snippet to test the progress indicator:

TaskbarManager tbManager = TaskbarManager.Instance; 

tbManager.SetProgressValue(50, 100); 

The first line will instantiate the taskbar, so that it can be accessed from the application. In this example, I am putting it inside the event handler. If you plan to use it somewhere else in your application, I would recommend placing it inside the class, but outside an event handler.

The second line sets the progress value to 50, also showing that the maximum value is 100. It is a static progress bar, since it is not tied to a specific event. Now, you can also set various progress bar states, depending on the state of the specific activity performed.

The Error progress state is used to represent an error with the current action:

tbManager.SetProgressState(TaskbarProgressBarState.Error); 

The Indeterminate state is used to show that there is an action either pending for some reason or awaiting user input. It is a constantly moving green bar:  

tbManager.SetProgressState(TaskbarProgressBarState.Indeterminate); 

There is also a NoProgress state, that will hide the progress. Generally, a developer can use it to hide the current progress because an action failed or because there is another reason to do so.

tbManager.SetProgressState(TaskbarProgressBarState.NoProgress); 

The Normal state is used to show the progress in its normal (green) color. Used to reset the progress bar to its default color, if that was changed, since it is the default choice anyway (if you did not change it initially.

tbManager.SetProgressState(TaskbarProgressBarState.Normal); 

And last but not least, there is the Paused state, that is a yellow progress bar, that is used to show the user that the current action is paused, either by choice or by an event.

tbManager.SetProgressState(TaskbarProgressBarState.Paused); 

As this doesn’t seem very complicated, I would like to mention that this became even easier in .NET Framework 4.0, since the taskbar handling capabilities are integrated in WPF.

Remember: you can always rely on the native APIs as well.