Take advantage of the Windows 7 TaskBar in your next project

Introduction

Windows 7 provides many new features, the most visible of these features is the new TaskBar. Just as the TaskBar has a new look it also provides a significant increase in functionality over past versions. You may have noticed the area for each application icon provides new functionality. The icon background can act as a progress bar with multiple modes including indeterminate, normal, paused and error. In addition, the icon itself can be updated with overlays. While subtle, these two combined can provide the user with valuable status and/or progress information. The next time you download a file using Internet Explorer 8, take a look at the icon and you'll see an example of the background progress bar. Another example of the significant improvements is the right-click menu, known as the Jump List. The Jump List provides applications the ability to list recent or favorite documents and include user tasks.

To get started with the TaskBar, first you need to download the Windows 7 API Code Pack from MSDN. The Code Pack provides managed code access to many of the enhancements within Windows 7, including TaskBar, Libraries, etc. For this article, we will focus on the TaskBar section of the code pack. After downloading and unpacking, open the Windows 7 API Code Pack solution in Visual Studio and compile it. We will need the dlls from the code pack in the examples below. The examples in the article are created using Windows Presentation Foundation (WPF); however, WPF is not a prerequisite for using the TaskBar and Jump List features. The functions described below can also be used in projects using WinForms.

Getting Started

To get started we will need to add references to the Windows 7 API Code Pack. The dlls we will need to reference for using the Toolbar are:

Next, we will need to use the following namespaces in the classes where we use the Taskbar:

Microsoft.WindowsAPICodePack

Microsoft.WindowsAPICodePack.Shell

Microsoft.WindowsAPICodePack.Taskbar

TaskBar Progress Bar

The TaskBar Progress Bar capabilities are very easy to start using and they incorporate into your application. The Progress Bar functions can be accessed by using the class TaskbarManager. The TaskbarManager.Instance static member is an easy way to access the TaskBar methods as it will create or return an existing reference. With a reference to the TaskBarManager it is easy to set the Progress Bar value as shown below.

TaskbarManager.Instance.SetProgressValue(15, 100)

The SetProgressValue accepts 2 parameters, the value and the max value. In addition to setting the current value, you can also change the state using the method: TaskbarManager.Instance.SetProgressState. The SetProgressState accepts a single enum parameter. The TaskbarProgressBarState enum provides the following values: Normal, NoProgress, Error, Paused and Indeterminate. To set the ProgressBar in an indeterminate state you would use the following statement:

This will produce the same affect as with a standard Progress Bar, it will provide a green bar slowly moving left to right and repeating. The Error mode will turn the progress bar red. Similarly, Paused will turn the progress bar yellow. The Normal state acts like a standard green progress bar and is turned on when you set the progress bar value. The NoProgress state hides the progress bar.

TaskBar Icon Overlays

Similar to the Progress Bar, you need to use the TaskbarManager.Instance to change overlays. You can change the overlay icon by using the following method:

TaskbarManager.Instance.SetOverlayIcon(Icon, "");

The SetOverlayIcon method accepts two parameters, System.Drawing.Icon object and accessibility text. So you can create your own icon resources and use them as necessary; however, the Windows API Code Pack provides a handy method for pulling stock icons. The example below shows a simple use for the StockIcon class.

The StockIcon constructor accepts 4 parameters, a StockIconIdentifier enum value, an icon size, an overlay icon boolean value and a selected icon boolean value. The SockIconIdentifier enum provides access to a large number of stock icons, making it a very handy tool to accompany the overlay icon feature. At some point in your application it will become necessary to hide the overlay icon. To hide it you need to pass null for icon into the SetOverlayIcon method as shown below.