Tap into the power of the Windows 7 Taskbar

The task bar has some of the most visible improvements in Windows 7, so this article is going to get you started on how to enhance your .NET applications by supporting the new tabbed thumbnail application previews, thumbnail toolbars, and jump lists.

What's new?

Each minimised application icon can been overlaid with custom icons or alpha-blended progress bars, providing the user with visual clues to the applications state. As each icon is hovered over, it can present a thumbnail image of the current user interface. If the application has the latest multi-tabbed style of user interface, each tab can presented as its own preview thumbnail. A preview thumbnail can have a mini toolbar giving access to core application functionality without the need for the user to open the application further.

task bar

Right-clicking a minimised application taskbar icon also provides a new context menu in Windows 7. These are called Jump Lists. A jump list will contain a list of recently used files if the application has a file type associated with it. The application developer can add to the jump list custom task items to provide more short-cuts to application functionality.

task bar jump lists

These small application enhancements soon become second nature to users, who quickly expect modern applications to deliver them. For the .NET managed code application this is easily achieved by using the Windows API Code Pack.

The Windows 7 API Code Pack makes it easy to implement support for the key Windows 7 native features by providing a .Net managed code wrapper around the native API.

Adding thumbnail previews

With the help of the Windows 7 API Code Pack adding individual thumbnails is very trivial, so let’s look at how to implement thumbnail previews for a tabbed application UI. We’ll add to each thumbnail preview some buttons to control our application directly from the preview thumbnail. The code pack framework is provided as a static library built with .Net 3.5 SP1 to use the Windows API Code Pack.

We’ll use a simple C# Winforms project to test this all out. We need to add some code pack references:

Microsoft.WindowsAPICodePack
Microsoft.WindowsAPICodePack.Shell

The code pack framework provides integration to the Windows 7 thumbnail preview via the TaskbarManager class. The class contains four methods and five properties. The methods mainly deal with the overlay of icons onto the taskbar icon and the progress bar overlay.

To add our application specific thumbnail previews we define a TabbedThumbnail for each of our previews and add it to the TaskBar’s TabbedThumbnail collection. This collection is accessible from the Instance object of the TaskbarManager class. The Instance object represents the Windows 7 task bar.

// Add a new preview 
TabbedThumbnail preview = new TabbedThumbnail(this.Handle, newTab.Handle);

Events from the user’s interaction with the thumbnail previews are easily trapped by adding our own event handlers.

// Event handlers for this preview 
preview.TabbedThumbnailActivated += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailActivated); 
preview.TabbedThumbnailClosed += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailClosed); 
preview.TabbedThumbnailMaximized += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailMaximized); 
preview.TabbedThumbnailMinimized += new EventHandler<TabbedThumbnailEventArgs>(preview_TabbedThumbnailMinimized);

Now our preview thumbnail can be added to the task bar’s TabbedThumbnail collection.

TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);

Each of our application’s tab pages can have an individual preview window added by defining its own TabbedThumbnail and adding it to the TabbedThumbnail collection in this manner.

Adding toolbars to thumbnail previews

There is a second collection on the Instance object which enables toolbars to be added to each thumbnail preview. The ThumbnailToolbars collection accepts a toolbar definition. The definition is made by defining thumbnail toolbar buttons, their event handlers and adding them as a tool bar associated with a specified windows handle.

Thumbnail button definitions made at class level

private ThumbnailToolbarButton thumbButtonBack;
private ThumbnailToolbarButton thumbButtonForward;
private ThumbnailToolbarButton thumbButtonRefresh;

Thumbnail button handler assignment made at form initialisation

// Create our Thumbnail toolbar buttons for the Browser doc
thumbButtonBack = new ThumbnailToolbarButton(Properties.Resources.prevArrow, "Back");
thumbButtonBack.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbButtonBack_Click);
thumbButtonForward = new ThumbnailToolbarButton(Properties.Resources.nextArrow, "Forward");
thumbButtonForward.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbButtonForward_Click);
thumbButtonRefresh = new ThumbnailToolbarButton(Properties.Resources.refresh, "Refresh");
thumbButtonRefresh.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(thumbButtonRefresh_Click);

Thumbnail toolbar defined and added to the ThumbnailToolbars collection

// Add thumbnail toolbar buttons 
TaskbarManager.Instance.ThumbnailToolbars.AddButtons(newTab.Handle, thumbButtonBack, thumbButtonForward, thumbButtonRefresh);

The end result looks like this:

thumbnail toolbars

There are all sorts of additional goodies in the TabbedThumbnail class properties that allow you to clip the bitmap and effectively zoom into it as well as provide tooltips etc.

To enable applications to run on legacy Windows versions a boolean flag – IsPlatformSupported – is provided on the TaskbarManager to indicate if the current platform can support thumbnails.

Adding thumbnail previews to your applications is a small effort for achieving one of the most visible enhancements in the Windows 7 UI. The code pack API makes it easy to do. The complete application can be downloaded from here. It is a simplified version of the code pack Thumbnail sample.

Adding a jump list

After thumbnail previews the next item you’ll be looking to implement for your application is a Jumplist. A Jumplist may provide shortcuts to recent documents; frequently use documents and application functionality.

Setting up a jumplist for your application is as simple as simple can be using the code pack managed API – but there are some gotchas worth noting.

  1. The app needs a valid active window on the taskbar before you can create a jumplist. This isn’t obvious – except when you get the code pack’s error message on first run of your modified application. Remember, in working with the new Taskbar features, we are actually providing the Taskbar with details we want it to attribute to our application. If it doesn’t know about our application, it can’t take our details.
  2. The code pack API does a good job of proactive error detection. For example, it’ll test to make sure the file you want to list on your jumplist exists. If it doesn’t we get blown out before we get near the Taskbar. This is the same with file associations. The code pack tests for the file association relationship to our application. If it doesn’t we can’t list the file. For more details on this one, look at the code pack TaskBarDemo sample. You might wonder why this extreme protection. The answer lines in the third gotcha.
  3. The known category types (Recent, Frequent) are populated by the Taskbar for our application ‘automagically’. So we only have to choose to show one of these categories in your jumplist to get the information displayed. This is because Windows maintains a recent items history infrastructure when we use the current common file dialogs. If you don’t use them (why?), we can hand code this mechanism by using the shell API SHAddToRecentDocs or the code pack’s jumplist.AddToRecent method.
  4. The code pack’s jumplist does a lot of good stuff. This includes maintaining our jumplist against the removed items list created by the user. We may want to have all the items in the jumplist but the user can selectively remove file references from our list. It’s considered bad form to keep putting the item back when the user has removed it; jumplist ensures we don’t make this mistake. If we don’t use jumplist we have to check the removedDestinations property and modify the list ourself.

Now let us look at the code. Below are the specific parts of a C# Winforms application showing the creation of a jumplist.

At class level we define the key elements of our jumplist – the jumplist variable itself and two custom categories.

public partial class Form1 : Form 
{ 
    private JumpList jumpList; 

    private JumpListCustomCategory category1 = new JumpListCustomCategory("Custom Stuff"); 
    private JumpListCustomCategory category2 = new JumpListCustomCategory("Custom Stuff2");

All the work is kicked off in the form’s Shown event with the creation of a jumplist. Two custom categories are added to it then some tasks are added that open other applications. The jumplist is ‘published’ by calling jumplist.refresh();

    private void Form1_Shown(object sender, EventArgs e) 
    { 
        // create a new taskbar jump list for the main window 
        jumpList = JumpList.CreateJumpList(); 

        // Add custom categories 
        jumpList.AddCustomCategories(category1, category2); 

        AddTasks(); 
        jumpList.Refresh(); 
    }

The helper routine AddTasks is used to contain all the task definition code.

    private void AddTasks() 
    { 
        // Path to Windows system folder 
        string systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System); 

        // Add our user tasks 
        jumpList.AddUserTasks(new JumpListLink(Path.Combine(systemFolder, "notepad.exe"), "Open Notepad") 
        { 
            IconReference = new IconReference(Path.Combine(systemFolder, "notepad.exe"), 0) 
        }); 

        jumpList.AddUserTasks(new JumpListLink(Path.Combine(systemFolder, "mspaint.exe"), "Open Paint") 
        { 
            IconReference = new IconReference(Path.Combine(systemFolder, "mspaint.exe"), 0) 
        }); 

        jumpList.AddUserTasks(new JumpListSeparator()); 

        jumpList.AddUserTasks(new JumpListLink(Path.Combine(systemFolder, "calc.exe"), "Open Calculator") 
        { 
            IconReference = new IconReference(Path.Combine(systemFolder, "calc.exe"), 0) 
        }); 


    }

Our jump list now looks like this:

jumplists

Remember the Jumplist is created by the Taskbar for your application. Unlike the Thumbnail preview toolbars there doesn’t appear to be a mechanism of capturing jumplist events into your application. The only event raised by jumplist is when items have been removed from the jumplist since the last jumplist refresh occurred. This means achieving something similar to Internet Explorer 8’s Open new tab jumplist task, requires an ‘external’ communication to occur with our application.

Conclusion

Jumplists and Thumbnail previews are some of the Windows 7 features you can easily add to applications to make them come alive on Windows 7. For further examples, download the Windows 7 API Code Pack and examine the great range of samples and documentation provided.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debuggers don't remove bugs. They only show them in slow motion.”