Windows 7 and taskbar icon progress indicators

Posted: (EET/GMT+2)

 

Windows 7 is a popular topic among developers, and the improved taskbar is easily programmable. A common question seems to be: how do I create an application that can show a progress indication on its own taskbar icon? Luckily, the answer is very simple: by using the Windows API Code Pack, you only need two lines of code. One line sets the state ("color") of the taskbar progress indicator (none, normal=green, error=red, etc.) and another the actual progress as a fraction.

Here is a simple example:

TaskbarManager.Instance.SetProgressState(
  TaskbarProgressBarState.Normal);
TaskbarManager.Instance.SetProgressValue(
  40, 100); // 40/100 = 40%

That's all there is to it! The above code will naturally require Windows 7, and if you try to run the code in, say, Windows XP, you will get a PlatformNotSupportedException exception. However, it is very easy to check whether the taskbar features are supported: just check the TaskbarManager.IsPlatformSupported property. Then, the total code could be:

if (TaskbarManager.IsPlatformSupported)
{
  TaskbarManager.Instance.SetProgressState(
    TaskbarProgressBarState.Normal);
  TaskbarManager.Instance.SetProgressValue(
    40, 100);
}

PS. If you can read Finnish, I wrote a simple tips article about Windows 7 development with Windows API Code Pack. Check it out on the Finnish CodeZone.