Friday, April 1, 2011

Create a JumpList Using C#

The JumpList in Windows 7 is the context menu of an application in the taskbar, which pops up after right cloking the application symbol.
In this menu often opened files are visible as well as options for the program.
For the C# studio my JumpList looks for example as follows:



In C# we now can create and edit JumpLists for own application. Although there is already one class for JumpLists in .Net, for Windows Forms Application we still need the Windows API Code Pack, since the provided class is only accessible for WPF applications.
The installation of the Windows API Code Pack I described here, the 2 created dll files have to be included afterwards (Project - Add Reference - Browse). The needed files are "Microsoft.WindowsAPICodePack.dll" and "Microsoft.WindowsAPICodePack.Shell.dll", they are contained in the installation folder of the code pack in Shell\bin\Debug.
The class JumpList used for this post is contained in Microsoft.WindowsAPICodePack.Taskbar.
In a JumpList by default 2 categories are present: "Tasks" and "Recent" (recently opened files).
These categories can be filled with the functions AddUserTasks() and AddToRecent()
As entries I use in the post objects of the class JumpListLink, which contain a name or a target, which can be an executable program or a file.
The category "Recent" though cannot be filled that easily, since Windows allways only files with endings associated to the program. With a little trick this difficulty can be avoided though. You can add own categories in JumpLists - if you know create the category "Recently Used" for example, the result is the same.
The sample code creates 2 new entries in "Tasks" in the JumpList - one entry for Paint and one for the Windows calculator. For these programs, also icons are provided - which are set as IconReference directly from the original program.
Furthermore this code creates a new category and creates a link to a file on my desktop.
The concluding Refresh() is important to execute the changes.
The JumpList is also visible in the same form when starting the program the next time - if it was not deleted or change.
The result looks as follows:



Now the code, at the beginning a
using Microsoft.WindowsAPICodePack.Taskbar; is needed:

            JumpList CustomJumpList = JumpList.CreateJumpList(); // create new JumpList

            // Add Link to Paint to Tasks
            JumpListLink EntryPaint = new JumpListLink(@"C:\Windows\System32\mspaint.exe""Paint");
            EntryPaint.IconReference = new Microsoft.WindowsAPICodePack.Shell.IconReference(@"C:\Windows\System32\mspaint.exe", 0);
            CustomJumpList.AddUserTasks(EntryPaint);

            // Add link to Calculator
            JumpListLink EntryCalc = new JumpListLink(@"C:\Windows\System32\calc.exe""Rechner");
            EntryCalc.IconReference = new Microsoft.WindowsAPICodePack.Shell.IconReference(@"C:\Windows\System32\calc.exe", 0);
            CustomJumpList.AddUserTasks(EntryCalc);

            // create custom category
            JumpListCustomCategory CustomCategory = new JumpListCustomCategory("Neue Kategorie");
            // create link to file in this
            CustomCategory.AddJumpListItems(new JumpListLink(@"C:\Users\User\Desktop\C#.txt""C# Links"));
            CustomJumpList.AddCustomCategories(CustomCategory);

            // update changes
            CustomJumpList.Refresh();

No comments:

Post a Comment