C# Corner

Getting the Jump on Jump Lists

Windows 7 contains a number of UI improvements to the classic taskbar. One very useful feature for quickly launching an application with its associated files is Jump Lists. In this issue, we'll look at how you can utilize Jump Lists in your own Windows 7 applications.

If you're a Windows 7 user, hopefully you've already noticed Jump Lists. Maybe you've noticed them but didn't know what they were called. When you have an application running in Windows 7, you can right-click on the application's icon in the taskbar to get its jump list. Here's the Jump List for a running instance of Visual Studio 2008:


[Click on image for larger view.]
Figure 1. Visual Studio 2008 Jump List.

The three options listed (from top to bottom) allow you to:

  • Start up another instance of Visual Studio 2008.
  • Pin Visual Studio 2008 to the taskbar.
  • Close the instance of Visual Studio 2008.

Applications that are already pinned to the start menu will display an option to unpin them. Applications that have no running instances will not have the third option.

Now let's take a look at the Jump List for Visual Studio 2010:


[Click on image for larger view.]
Figure 2. Visual Studio 2010 Jump List.

Hopefully you can see the difference. There's a list of "Recent" files opened by Visual Studio 2010. This list is similar to the "My Recent Documents" list off the Start menu in previous versions of Windows, but the list is limited to just the documents handled by Visual Studio 2010! This makes the list so much more useful and presents users with a focused list of recent files they may have worked on with Visual Studio 2010.

You can take advantage of Jump Lists in your applications and even control what gets displayed (except for the bottom two or three options which are always displayed by Windows 7). In this article, we'll show how to create a categorized Jump List for your Windows 7 applications.

File Associations
The thing that makes all of this easy to do is the Windows API Code Pack. It's a free, open source managed library for the new Windows 7 features. It's not part of the .NET Framework, so you'll have to download and compile it from http://code.msdn.microsoft.com/WindowsAPICodePack. You'll need at least the 3.5 version of the .NET Framework with Service Pack 1 installed.

Another important point about how Jump Lists work is that they are tied to the "Associated Programs" feature of Windows. When you double-click on a .docx file, Microsoft Word is automatically launched and the file you clicked on is loaded. Likewise, when you double-click on a .txt file, Windows will launch Notepad. With this association of a file type to an application, Windows can keep track of "recent" files and let you customize the look of the recent files list.

For this demo, I've included a registry file (VSM_Setup.reg) which will register a new ".vsm" file type. The type is associated with our sample application. This allows us to customize how .vsm files are displayed in our Jump List. You can find a complete, detailed explanation of File Types here. Due to the way associated programs work, the registry file contains a hard-coded path to the demo code (JumpLists1.exe). Make sure to edit VSM_Setup.reg so it points to the location of where you extracted the demo code on your machine. Save your changes and then double-click on VSM_Setup.reg to import it into your registry.

Finally, the sample code disables the VS 2008 hosting process (vshost.exe). This is done so that the executable that is running (JumpLists1.exe) matches the application set up in the registry key. If the application "JumpLists1.vshost.exe" is running, the Jump List library will not work since that process is not associated with our "vsm" extension. You will lose a few benefits of the hosting process (see here for more details), but this shouldn't be a huge issue for most applications.

On to the code!
If you just want to have a simple, Windows 7-maintained list of recent files, you can use the following code in your WinForms application:

protected override void OnShown(EventArgs e)
{
base.OnShown(e);

var jumpList = JumpList.CreateJumpList();
jumpList.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;
jumpList.Refresh();
}

This tells Windows 7 to display the "Recent" list of files opened by the application (those the user double-clicked on to launch the application). While this is nice to have, creating a categorized list of files really makes your application easier to use.

For this example, I'm going to create two categories:

  • S-Files: These are .vsm files that start with the letter ‘S'.
  • C-Files: These are .vsm files that start with the letter ‘C'.
  • Our jump list will look like this once completed:


    [Click on image for larger view.]
    Figure 3. Categorized Jump List.

    Let's start by writing a utility method for creating our Jump List Category:

    private JumpListCustomCategory CreateCategory(string categoryName,
    string searchPattern)
    {
    var category = new JumpListCustomCategory(categoryName);
    var vsmPath = Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.MyDocuments), "VSM");
    var items = from f in Directory.GetFiles(vsmPath, searchPattern)
    select new JumpListItem(Path.Combine(vsmPath, f));
    category.AddJumpListItems(items.ToArray());

    return category;
    }

    This code assumes your "My Documents" folder contains a "VSM" folder with a few .vsm files (some starting with "S" and some starting with "C"). As you can see in Figure 3, I had a "sample1.vsm", "sample2.vsm" and "cfile.vsm".

    We create a JumpListCategory by providing its name. We then use LINQ to build a list of JumpListItem objects, which map to each file that matches our search pattern. Finally, we add these items to the category and return the JumpListCategory.

    Now our OnShown method can be modified to create our categorized Jump List:

    jumpList = JumpList.CreateJumpList();

    var categorySFiles = CreateCategory("S-Files", "s*.vsm");
    var categoryCFiles = CreateCategory("C-Files", "c*.vsm");
    jumpList.AddCustomCategories(categorySFiles, categoryCFiles);

    jumpList.Refresh();

    And that's it! How you categorize your jump lists is up to you. In this example I use a simple filename search. But you could have a date-based categorization ("Last Week", "Last Month", "Last Year") or size-based (">1 Megabyte", "500k – 1 Megabyte", "< 500k").

    In a future column, I'll talk about another neat Windows 7 feature: Thumbnail Toolbars.

About the Author

Patrick Steele is a senior .NET developer with Billhighway in Troy, Mich. A recognized expert on the Microsoft .NET Framework, he’s a former Microsoft MVP award winner and a presenter at conferences and user group meetings.

comments powered by Disqus

Featured

Subscribe on YouTube