Creating Windows 7 Jump Lists With The API Code Pack and Visual Studio 2008

Introduction

Now that Windows 7 is readily available at every computer
store, it’s only a matter of time until the new operating
system becomes mainstream in both homes and corporations.
Especially this will happen once new computers start
replacing old ones.

From a developer perspective, Windows 7 brings many
possibilities. One of the most visible and prominent new
features is the improved taskbar, through which applications
are launched and even managed. In this article, the focus is
on a feature called jump lists, which can be thought of as
being a mini Start menu when right-clicking an icon on the
taskbar (Figure 1).


A basic Windows 7 jump list
Click here for larger image

Figure 1. A basic jump list.

Jump lists are active both when right-clicking an icon on
the taskbar, but also when pointing at an application icon
in the Start menu’s initial view (Figure 2). Thus, there are
two ways to make jump lists visible; however these menus
have a different focus: jump lists on the Start menu are
about the recent documents, whereas the taskbar-activated
jump list contains more options to manage the application
and its windows.


Figure 2. Jump lists in the Start menu.
Click here for larger image

Figure 2. Jump lists in the Start menu.

Programming both instances of jump lists requires only a
single implementation. Under the hood, jump lists use a COM
based API, just like many other features in the Windows
shell. Although it is possible to access these COM
interfaces directly from your application, there is an
easier way. Microsoft has developed a free package for .NET
developers called the Windows API Code Pack (Figure 3),
which contains many ready-made classes to access Windows
features, such as Windows 7’s jump lists.


The Windows API Code Pack download page.
Click here for larger image

Figure 3. The Windows API Code Pack download page.

Next, you are going to see how you can use Visual Studio
2008 and C# to enable custom jump lists in your own .NET
applications with a WinForms sample application (Figure 4).
A similar method could be used with Visual Studio 2010 Beta
2. With Visual Studio 2010 however, you can also use WPF
4.0’s new support for Windows 7 shell integration. Even so,
these classes are out the scope of this article, but the
topic will likely be addressed in the near future.


The sample application
Click here for larger image

Figure 4. The sample application.

Briefly About Jump List Features

Jump lists on Windows 7 can be considered streamlined
mini versions of the Windows Start menu. Furthermore, they
are tailored for each application. Just like in all Windows
versions after Windows 95, you can right-click an icon on
the taskbar, and see a popup menu. Jump lists in Windows 7
are an evolution of this menu.

Indeed, even if you don’t do anything special, Windows 7
will display a popup menu for your application when its icon
is available on the taskbar (see again Figure 1). If you
have pinned an application to the taskbar, the default menu
will give you the option to unpin it or launch the
application. If the application is running, the default menu
contains options to pin the program to the taskbar, close
it, or launch another instance.

If an application has associated a file type (file
extension) with itself in the registry–for instance, Word
with .doc and .docx files–Windows will collect a list of
recent files opened with the application. This happens
automatically if the application uses Windows common file
dialogs to open files, or uses the
SHAddToRecentDocs API function to let Windows
know that files were opened. Windows will also keep track of
which files are used most frequently.

Files opened with an application from two so-called known
lists. These are the Recent and Frequent lists. The
difference between these two is that Recent will collect the
most recently opened files (say, five of them), and the
Frequent list will on the other hand collect files that are
most commonly used over time. The application can control
whether one of these lists is shown in the jump list, or
neither.

These known lists are said to contain destinations, or
nouns. Destinations are always files, and because of this, a
file handler for a given file type (or types) must be
registered. Windows will also make sure the files are
available before showing them on the list.

Another important part of jump lists, and the focus of
this article, is the support for verbs. Verbs allow your
application to freely add custom commands to the jump list,
and then allow users to quickly interact with your
application while it’s running. Unlike nouns, verbs do not
require you to associate a file extension with your
application.

Verbs are file based commands that point back to your
application, and instruct it to run certain commands. For
example, if you were writing a sales application, then the
jump list verbs could start a new order, find a customer,
and so on. Since these lists are customized for each
application, every application has its own set of verbs.

Using the Windows API Code Pack

The freely downloadable Windows API Code Pack (see the
Links section at the end for the URL) is a set of .NET
classes compiled into several different assemblies. You can
easily reference these from your own projects, and thus
benefit from the classes.

To get started with the code pack, you first need to download it. It comes in a regular zip file, which you should extract to a convenient location, such as under your Visual Studio projects folder. Then, navigate inside the newly extracted folders, and open a solution named Shell.sln with Visual Studio. Build the whole thing, and then note the location of the resulting DLL files, usually under the bin\Debug folder.

After a successful build, you should see two assemblies,
Microsoft.WindowsAPICodePack.Shell.dll and
Microsoft.WindowsAPICodePack.dll in the output
folder. The latter file is automatically created, as the
Shell project depends on a project called Core. The Core
project is also part of the code pack.

The next step is to start a new project (or open one, if
you already have one) in Visual Studio, and add the two code
pack DLLs as references to your project (Figure 5). With the
proper references in place, you can start writing code. The
assembly filenames already give a hint of the correct
namespaces; to use the code pack for jump lists, add the
following using statements to your C# code:


using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Taskbar;


References have been to the sample project
Click here for larger image

Figure 5. References have been to the sample project.

Inside the latter namespace, you can find a class called
JumpList, which is a great starting point to begin exploring
the features in jump lists. For instance, let’s see how you
could add a custom jump list verb to your application’s
menu.

The first step is to create an instance of the JumpList
class. After this, you need to construct an object
supporting the IJumpListTask interface for each
custom verb you wish to add to your jump list. This can be
done with the help from the JumpListLink class,
also part of the
Microsoft.WindowsAPICodePack.Taskbar
namespace.

For instance, you might want to add commands into your
application’s jump list to quickly launch Notepad or
Calculator. To do this, you could use the following
code:



JumpList list = JumpList.CreateJumpList();

string systemFolder = Environment.GetFolderPath(
Environment.SpecialFolder.System);

string notepadPath = System.IO.Path.Combine(
systemFolder, “notepad.exe”);
JumpListLink notepadTask = new JumpListLink(
notepadPath, “Open Notepad”);
notepadTask.IconReference = new IconReference(
notepadPath, 0);

string calculatorPath = System.IO.Path.Combine(
systemFolder, “calc.exe”);
JumpListLink calculatorTask = new JumpListLink(
calculatorPath, “Open Calculator”);
calculatorTask.IconReference = new IconReference(
calculatorPath, 0);

list.AddUserTasks(notepadTask, calculatorTask);
list.Refresh();
MessageBox.Show(“Custom tasks have been added!”);


Here you are creating a new jump list object, and then
constructing two verbs with the JumpListLink class. The
constructor requires a file-based command, and in this case
this is the full pathname to Notepad.exe (and Calc.exe) in
the Windows\System32 directory. The
System.IO.Path class helps in the process.
Also, menu icons are added using the
IconReference class, which takes in a full path
and an icon index. Zero means the first icon in the file,
which is commonly the main icon.

Finally, the AddUserTasks method of the
JumpList class is called to add the new verbs to the menu.
Windows 7 will create the commands under the Tasks generic
group, and thus the verbs added here can also be called
tasks. Note that Windows will keep a record of the tasks you
create. Thus, you will only need to create your tasks
once.

More by Author

Must Read