Jun 3, 2010

taskbar_figure1

Want your new Windows 7 applications to take advantage of the new operating system? Here’s how to add functionality to the Windows 7 Task Bar, using the Windows 7 API Code Pack, .NET, and very little effort.

When I first started with .NET programming back in its version 1.0 days, I noticed how shielded we Windows programmers were from the good old Win32 API. On the one hand, that was a great thing; no longer did developers have to fuss with enormous calls to CreateWindowEx and the related RegisterClass function. While functions like these and the message handler functions were the lifeblood of our applications, with .NET our programming became substantially easier.

But on the other hand, at times our hands were tied. Despite the size of the .NET framework, there were some API features that the .NET creators seemed to skip. As .NET moved forward and included WPF features, Windows Vista come along, and now Windows 7, both of which had lots of features that were absent in .NET.

The way around this was to call directly into the Win32 API. That’s certainly possible in both C# and VB.NET. But it’s not fun, not by any means. A better way, however, is a .NET library that encapsulates the missing features. And that’s exactly what Microsoft did with the Windows API Code Pack for Microsoft .NET Framework.

The API Code Pack includes classes for accessing the Windows Shell, the Windows Taskbar, task dialogs, the sensor platform APIs, the Extended Linguistic Services APIs, the power management APIs, Application Restart and Recovery APIs, Network List Manager APIs, Command Link control and System defined Shell icons, DirectX, and even the Aero Glass system.

The classes are very clean, and are more than just a wrapper around a bunch of functions. (Remember the early days of MFC, which was little more than a basic wrapper? This is much better.) Simply put, programming with the API Code Pack feels like .NET programming.

To get started, I decided to try the Taskbar features. The Taskbar is one aspect of Windows 7 that is substantially different from earlier versions of Windows, even Windows Vista.

Exploring the Taskbar

As I got started, I realized that I wasn’t totally aware of the Windows 7 taskbar features. I had vaguely noticed them but hadn’t recognized that they could be programmed. Even if you know the TaskBar well, it’s worth a moment to review a few user capabilities before we look at the programmer’s options.

There are two main features that you can access programmatically.

First, when a program is “pinned” to the start menu or the task bar (by right-clicking on its shortcut and choosing Pin to Start Menu or Pin to Taskbar), the program can include a submenu beside it on the start menu..

taskbar_figure1

It also can have a popup menu-sort-of thingamajig on the taskbar.

taskbar_figure2

I had noticed these menus in various programs such as Internet Explorer, but didn’t really consider how I could program them myself (or that they have a name: jump lists). But you can program these jump lists through the Win32 API for Windows 7, and, in .NET, using the API Code Pack.

Programs can display multiple icons in the Taskbar, representing various pages. For example, if you have multiple tabs in Internet Explorer open, you see a thumbnail version of each page in a popup above IE’s icon on the taskbar. These thumbnails represent separate tabs inside a single IE window—not separate IE windows. Again, that’s all programmable; the following figure shows what these things look like in a sample that ships with the API Code Pack.

taskbar_figure3

Programming the Jump Lists

Rather than talk you line-by-line through the code, in the rest of this article I point you to the examples that ship with the API Code Pack, and explain how they work. The examples are contrived in that they don’t serve any real purpose beyond demonstrating the features; but they do a find job of helping you explore the classes in the API.

If you look at the earlier image for a jump list, you see there’s a Recent list, a Tasks list, and a list called Custom Category 1. The first list can be configured to show Recent, Frequent, or nothing at all. The next list is a set of tasks that you can add, which are really just shortcuts to programs. Then you can have custom lists, each with whatever name you like.

Before you aim to show items in the Recent (or Frequent) list and the custom lists, there’s a caveat you need to be aware of: You can only show file types (that is, file name extensions) that are registered with your application. The sample app shows .txt files, and as such, must associate .txt files with the sample app. (The sample includes code to show how to do this, and such code makes use of shell features in the API Code Pack.)

Note: I got a little bit confused after I asked the sample to register itself with .txt files. I double-clicked several .txt files, and Notepad opened. Then I remembered that any file type can be registered with multiple applications, and one can be set as the default application. The way to choose which registered application is to right-click the .txt file in Explorer, and in the popup menu, click Open With, and then click the name of the application.

After your application has registered itself with a particular file type, it can use classes in the Microsoft.WindowsAPICodePack.Taskbar namespace for manipulating the jump list. First, you create a new instance of the JumpList class; then you set the JumpList instance’s KnownCategoryToDisplay member to show either Recent, Frequent, or nothing for the first list.

To add items to the Recent list, call the JumpList instance’s AddToRecent class, passing the file name. Easy enough. Then call the Refresh method to commit the change.

To add your own custom categories, call AddCustomCategories, passing instances of a class called JumpListCustomCategory, each of which contains a category name. Then, to add items to a category, call a JumpListCustomCategory instance’s AddJumpListItems method. Again, easy enough. After a call to Refresh, the list is created.

Adding tasks is equally easy; you call the JumpList instance’s AddUserTasks method, adding instances of classes derived from IJumpListTask.

Creating Custom Thumbnails

Creating the thumbnails that appear above the items in the taskbar is about as easy as adding to the jump lists. First, you need to get an instance of a class called TaskbarManager. That class has a member that’s an instance of ThumbnailToolbarManager, which in turn has a method called AddButtons. You call AddButtons to add thumbnails above the taskbar.

In general, you don’t need to be aware of the underlying Win32 API when using these classes. The AddButtons class is an exception, but only slightly. The call to AddButtons needs a window handle (a number that uniquely identifies a single Window) so that it can take a snapshot of the window and display its thumbnail version. However, in the Win32 API world, a “window” isn’t automatically a single window that holds an application; rather, every control within a window is itself a window. In the sample that comes with the API Code Pack, the main window contains individual tabs much like those you see in Internet Explorer. These tabs are individual Win32 API windows, and as such can appear as thumbnails.

That said, when you call AddButtons, you add a handle for each window (or control, or tab, and so on) that should appear as a thumbnail. Fortunately, in .NET, you still have access to the handles, as each control contains a Handle property inherited from the System.Windows.Forms.Control class. Just pass that handle, and you’re good to go.

Running a Taskbar Progress Bar

Another useful and programmable Taskbar feature is the ability to have a moving progress bar that runs across the icons in the taskbar. This is hard to demonstrate with a simple screen capture, but in the little image below you can see the left third or so of the taskbar item is colored green.

taskbar_figure4

The progress bar works like other progress bars in that it doesn’t automatically increase. Instead, your program sets the value, and changes the value as needed, increasing it over time to make it look like the progress bar is growing.

To set the value, you start with the same TaskbarManager instance you used with the custom thumbnails, and call its SetProgressValue method. (The method takes two parameters: a value, and a maximum, which the system divides to get a percentage.) Additionally, you call the TaskbarManager instance’s SetProgressState, specifying an enumeration of type TaskbarProgressBarState; this enumeration contains values Normal (to make the progress color green), Error (to make the progress color red), Paused (for color yellow), or NoProgress to hide the progress bar. (Another enumeration, Indeterminate, had no apparent effect on my system for some reason. But according to the Win32 API documentation, not the API Code Pack documentation, the indicator should cycle “repeatedly along the length of the taskbar button.”)

What About Earlier Windows Versions?

When I set out exploring the API Code Pack, I wondered what would happen if you created an application that uses Windows 7 features and tried to open the application under Windows Vista, for example. Fortunately, the API Code Pack includes a set of methods for checking the current version of Windows that’s running. Using these methods, you can first check if Windows 7 is running, and only then use the Windows 7 Taskbar features, skipping over the code otherwise.

The API Code Pack makes it incredibly easy to program for the taskbar, and I have only scratched the surface on its features. Since you can test whether Windows 7 is present, you might as well include Taskbar support in your software to add that extra professional touch – and to impress the users who want to see what advantages Windows 7 might have for their work.


Want more like this? Sign up for the weekly IT Expert Voice newsletter so you don’t miss a thing!

Want more like this? Sign up for the weekly IT Expert Voice Newsletter so you don't miss a thing!

COMMENTS

DELL
FM IT Expert Voice is a partnership between Dell and Federated Media. Privacy Statement