Progress Bar in Windows 7 Taskbars

June 01, 2010 .net c# windows-7

I decided to add progress bar to the Windows 7 Taskbar in my Timer app.

I started by downloading and compiling the Windows API Code Pack in Release mode. I then added a reference to the Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll files to the project. After that add the lines:

using Microsoft.WindowsAPICodePack.Taskbar;

to your using statements. When the clock starts running I create the progress bar in the taskbar with:

// Initialize progress bar
if(TaskbarManager.IsPlatformSupported)
{
	TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
	TaskbarManager.Instance.SetProgressValue(0, (int)this.totalTime.TotalSeconds, this.Handle);
}

to stop the progress bar:

// Stop progress bar
if(TaskbarManager.IsPlatformSupported)
	TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);

and finally to update the progress bar on each tick:

// Update progress bar
if(TaskbarManager.IsPlatformSupported)
	TaskbarManager.Instance.SetProgressValue((int)this.totalTime.TotalSeconds - (int)this.time.TotalSeconds, (int)this.totalTime.TotalSeconds, this.Handle);

Drawing Rectangles with SpriteBatch

Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch:

public static class SpriteBatchHelper
{
	static Texture2D pixel;

	private static void LoadPixel(GraphicsDevice graphicsDevice)
	{
		if(pixel == null)
		{
			pixel = new Texture2D(graphicsDevice, 1, 1);
			pixel.SetData<Color>(new Color[] { Color.White });
		}
	}

	public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color)
	{
		LoadPixel(spriteBatch.GraphicsDevice);
		spriteBatch.Draw(pixel, rectangle, color);
	}
}

C# Extension Methods in your own Library

Normally I use extension methods in C# to extend a library that I did not write and therefore I have no control over. There are situations where it makes sense to use extension methods for a library that you yourself are writing.

For example, when you have interfaces in your library. You want to keep the number of methods in that interface as low as possible so that classes implementing the interface don't have to do a lot of heavy lifting. This means cutting out methods in an interface that are for the most part just syntactic sugar for another method in the interface.

public interface IServiceContainer
{
    void AddService(Type type, Object provider);
    object GetService(Type type);
}

public static class IServiceContainerExtensions
{
    public static void AddService<T>(this IServiceContainer services, object provider)
    {
        services.AddService(typeof(T), provider);
    }

    public static T GetService<T>(this IServiceContainer services) where T : class
    {
        return services.GetService(typeof(T)) as T;
    }

    public static T GetRequiredService<T>(this IServiceContainer services) where T : class
    {
        T service = services.GetService(typeof(T)) as T;

        if(service == null)
            throw new ServiceNotFoundException(typeof(T));

        return service;
    }
}

All of the methods in IServiceContainerExtensions are just helper methods for method in IServiceContainer. By making them extension methods in our own library though, we've made the barrier to entry lower. Other people can implement the interface and in a sense "inherit" the helper methods as well.