Jump Lists可以使用戶方便快捷的找到想要浏覽的文件(文檔、圖片、音頻或視頻等)以及應用程序的鏈接或快捷方式。以IE 浏覽器為例看看Jump Lists 都具備哪些功能:

“Taskbar Tasks” 放置了應用程序的一些默認任務:“打開IE 浏覽器”、“從任務欄取消固定”、“關閉程序”。無論是否對Jump Lists 做過開發,“Taskbar Tasks” 列表都會出現在所有的應用程序中。
“User Tasks” 包含了應用程序本身提供的一些功能,通過這些鏈接可以直接對應用程序進行操作。例如,打開一個新IE 標簽。
“Known Category” 這個列表是Windows 7 默認類別,其中包含三種模式:“Recent”(近期浏覽)、“Frequent”(經常浏覽)、“Neither”。它的功能是將經常浏覽的網頁內容記錄下來以便日後再次浏覽,隨著時間的流逝該列表中的網頁鏈接會隨之變化或消失。除了“Known Category” 列表外同樣也以創建“Custom Category”(下文將會慢慢講到)。
“Pinned Category” 正如上面所講“Frequent Category” 列表中的網頁會經常變化,通過右鍵將網頁“釘”在列表中可使其永久保存。
創建User Tasks 列表
現在是不是也想為自己的程序添加一個JL,下面先來介紹如何創建User Tasks 列表。
1. 通過JumpList 類創建一個JL 實例。
2. 使用JumpListLink(string pathValue, string titleValue) 方法(pathValue:應用程序路徑,titleValue:鏈接名稱),可以將“記事本”、“畫板”這樣的Windows 應用程序,以及“網站地址”創建為User Tasks 鏈接。
3. 再使用AddUserTasks(params IJumpListTask[] tasks) 方法將這些鏈接添加到JL 中。如下代碼所示:
private JumpList _jumpList;
_jumpList = JumpList.CreateJumpListForIndividualWindow("Windows.TaskBar.WinFormJumpList", this.Handle);
///在上面程序中,通過JumpListTask 接口創建了“程序鏈接”(JumpListLink,其中IconReference 為鏈接圖標)和“分割線”(JumpListSeparator);使用AddUserTasks 方法時注意每個鏈接的位置關系;最後必須使用Refresh 方法對JL 進行刷新才能顯示出最新的JL 內容。/// 添加User Tasks /// private void AddUserTasks() { string systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System); // 程序鏈接 JumpListTask notepadTask = new JumpListLink(Path.Combine(systemPath, "notepad.exe"), "Notepad") { IconReference = new IconReference(Path.Combine(systemPath, "notepad.exe"), 0) }; JumpListTask paintTask = new JumpListLink(Path.Combine(systemPath, "mspaint.exe"), "Paint") { IconReference = new IconReference(Path.Combine(systemPath, "mspaint.exe"), 0) }; // 分割線 JumpListTask jlSeparator = new JumpListSeparator(); JumpListTask linkTask = new JumpListLink("http://blog.csdn.net/aoshilang2249", "langya's Blog") { IconReference = new IconReference("C:\\Program Files\\Internet Explorer\\iexplore.exe", 0) }; // 添加 User Tasks _jumpList.AddUserTasks(notepadTask, paintTask, jlSeparator, linkTask); // 對JumpList 進行刷新 _jumpList.Refresh(); }

創建Known Category 列表
在使用Known Category 功能前,需要先為程序注冊文件類型,隨後可通過KnownCategoryToDisplay 屬性將Known Category 預設為“Recent”、“Frequent”、“Neither” 中的任意一種類型,當測試程序打開某個的文件時,相應的文件鏈接就會顯示在Known Category 列表中。如下代碼所示:
文件關聯注冊輔助類:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using Microsoft.Win32;
namespace LangYa.Net.Utils.File
{
///
/// 注冊文件關聯的應用程序的輔助類
///
public class FileAssociationsHelper
{
private static RegistryKey classesRoot; // 注冊表的根目錄
private static void Process(string[] args)
{
if (args.Length < 6)
{
string error = ("Usage: [Ext2 [Ext3] ...]");
throw new ArgumentException(error);
}
try
{
string progId = args[0];
bool registerInHKCU = bool.Parse(args[1]);
string appId = args[2];
string openWith = args[3];
bool unregister = bool.Parse(args[4]);
List argList = new List();
for (int i = 5; i < args.Length; i++)
{
argList.Add(args[i]);
}
string[] associationsToRegister = argList.ToArray(); // 文件列表
if (registerInHKCU)
{
classesRoot = Registry.CurrentUser.OpenSubKey(@"Software\Classes");
}
else
{
classesRoot = Registry.ClassesRoot;
}
// 注銷
Array.ForEach(associationsToRegister, assoc => UnregisterFileAssociation(progId, assoc));
UnregisterProgId(progId);
// 注冊
if (!unregister)
{
RegisterProgId(progId, appId, openWith);
Array.ForEach(associationsToRegister, assoc => RegisterFileAssociation(progId, assoc));
}
}
catch (Exception e)
{
}
}
///
/// 注冊類標識符
///
///類標識符
///應用程序Id
///打開文件的進程全路徑
private static void RegisterProgId(string progId, string appId, string openWith)
{
RegistryKey progIdKey = classesRoot.CreateSubKey(progId);
progIdKey.SetValue("FriendlyTypeName", "@shell32.dll,-8975");
progIdKey.SetValue("DefaultIcon", "@shell32.dll,-47");
progIdKey.SetValue("CurVer", progId);
progIdKey.SetValue("AppUserModelID", appId);
RegistryKey shell = progIdKey.CreateSubKey("shell");
shell.SetValue(String.Empty, "Open");
shell = shell.CreateSubKey("Open");
shell = shell.CreateSubKey("Command");
shell.SetValue(String.Empty, openWith + " %1"); // " %1"表示將被雙擊的文件的路徑傳給目標應用程序
shell.Close();
progIdKey.Close();
}
///
/// 注銷類標識符
///
///類標識符
private static void UnregisterProgId(string progId)
{
try
{
classesRoot.DeleteSubKeyTree(progId);
}
catch { }
}
///
/// 注冊文件關聯
///
private static void RegisterFileAssociation(string progId, string extension)
{
RegistryKey openWithKey = classesRoot.CreateSubKey(Path.Combine(extension, "OpenWithProgIds"));
openWithKey.SetValue(progId, String.Empty);
openWithKey.Close();
}
///
/// 注銷文件關聯
///
private static void UnregisterFileAssociation(string progId, string extension)
{
try
{
RegistryKey openWithKey = classesRoot.CreateSubKey(Path.Combine(extension, "OpenWithProgIds"));
openWithKey.DeleteValue(progId);
openWithKey.Close();
}
catch (Exception e)
{
}
}
///
/// 類標識符注冊操作
///
///注冊或注銷
///類標識符
///是否在HKCU中注冊文件關聯 -- false
///應用程序Id
///打開文件的進程全路徑
///文件關聯列表
private static void InternalRegisterFileAssociations(bool unregister,
string progId, bool registerInHKCU,string appId, string openWith,
string[] extensions)
{
string Arguments = string.Format("{0} {1} {2} \"{3}\" {4} {5}",
progId, // 0
registerInHKCU, // 1
appId, // 2
openWith,
unregister,
string.Join(" ", extensions));
try
{
Process(Arguments.Split(' '));
}
catch (Win32Exception e)
{
if (e.NativeErrorCode == 1223) // 1223:用戶操作被取消。
{
// 該操作已經被用戶取消
}
}
}
///
/// 判斷類標識符是否注冊
///
///類標識符
/// 注冊了返回true
public static bool IsApplicationRegistered(string progId)
{
return (Registry.ClassesRoot.OpenSubKey(progId) != null);
}
///
/// 注冊類標識符的文件關聯
///
///類標識符
///是否在HKCU中注冊文件關聯 -- false
///應用程序Id
///打開文件的進程全路徑
///文件關聯列表
public static void RegisterFileAssociations(string progId,bool registerInHKCU, string appId, string openWith,
params string[] extensions)
{
InternalRegisterFileAssociations(false, progId, registerInHKCU, appId, openWith, extensions);
}
///
/// 注銷類標識符的文件關聯
///
///類標識符
///是否在HKCU中注冊文件關聯 -- false
///應用程序Id
///打開文件的進程全路徑
///文件關聯列表
public static void UnregisterFileAssociations(string progId, bool registerInHKCU, string appId, string openWith,
params string[] extensions)
{
InternalRegisterFileAssociations(true, progId, registerInHKCU, appId, openWith, extensions);
}
}
}
////// 添加Known Tasks /// private void AddKnownTasks(JumpListKnownCategoryType knowsType, int knownCategoryOrdinalPosition) { _jumpList.KnownCategoryToDisplay = knowsType; _jumpList.KnownCategoryOrdinalPosition = knownCategoryOrdinalPosition; // 相對於Custom的位置 if (!FileAssociationsHelper.IsApplicationRegistered(TaskbarManager.Instance.ApplicationId)) { FileAssociationsHelper.RegisterFileAssociations(TaskbarManager.Instance.ApplicationId, false, TaskbarManager.Instance.ApplicationId, Assembly.GetExecutingAssembly().Location, ".jpg", ".png", ".gif", ".JPG", ".PNG", ".GIF"); } _jumpList.Refresh(); }
為了文件正常打開,還需要修改Main方法,讓銜接的路徑可以傳入應用程序,以便打開應用程序關聯的文件:
////// 應用程序的主入口點。 /// [STAThread] static void Main(string[] args) { string filePath = ""; if ((args != null) && (args.Length > 0)) { for (int i = 0; i < args.Length; i++) { // 對於路徑中間帶空格的會自動分割成多個參數傳入 filePath += " " + args[i]; } filePath.Trim(); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Main() { FilePath = filePath }); }
////// 應用程序文件 /// public string FilePath { get { return (_strFilePath); } set { _strFilePath = value; if (!string.IsNullOrEmpty(_strFilePath)) { _pictureBox.ImageLocation = _strFilePath; } } }

如同上文創建JumpList 的方式:
1. 通過JumpListCustomCategory 類創建“自定義分類”列表實例。
2. 由JumpListCustomCategory(string categoryName) 方法為列表命名。
3. 使用AddJumpListItems 方法將鏈接加入到分類中。如下代碼所示:
////// 添加Custom Tasks /// private void AddCustomTasks(string categoryName) { if (categoryName.Length > 0) { JumpListCustomCategory customCategory = new JumpListCustomCategory(categoryName); _jumpList.AddCustomCategories(customCategory); // Arguments需要打開的文件類型的參數(如文件路徑等) JumpListLink jlItem = new JumpListLink(Assembly.GetExecutingAssembly().Location, "Chrysanthemum.jpg") { IconReference = new IconReference(Assembly.GetEntryAssembly().Location, 0), Arguments = @"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg" }; customCategory.AddJumpListItems(jlItem); _jumpList.Refresh(); } }
