標籤:windows 7 工作列
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);
/// <summary> /// 添加User Tasks /// </summary> 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(); } 在上面程式中,通過JumpListTask 介面建立了“程式連結”(JumpListLink,其中IconReference 為連結表徵圖)和“分割線”(JumpListSeparator);使用AddUserTasks 方法時注意每個連結的位置關係;最後必須使用Refresh 方法對JL 進行重新整理才能顯示出最新的JL 內容。
建立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{ /// <summary> /// 註冊檔案關聯的應用程式的輔助類 /// </summary> public class FileAssociationsHelper { private static RegistryKey classesRoot; // 註冊表的根目錄 private static void Process(string[] args) { if (args.Length < 6) { string error = ("Usage: <ProgId> <Register in HKCU: true|false> <AppId> <OpenWithSwitch> <Unregister: true|false> <Ext1> [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<string> argList = new List<string>(); 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) { } } /// <summary> /// 註冊類別識別項 /// </summary> /// <param name="progId">類別識別項</param> /// <param name="appId">應用程式Id</param> /// <param name="openWith">開啟檔案的進程全路徑</param> 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(); } /// <summary> /// 登出類別識別項 /// </summary> /// <param name="progId">類別識別項</param> private static void UnregisterProgId(string progId) { try { classesRoot.DeleteSubKeyTree(progId); } catch { } } /// <summary> /// 註冊檔案關聯 /// </summary> private static void RegisterFileAssociation(string progId, string extension) { RegistryKey openWithKey = classesRoot.CreateSubKey(Path.Combine(extension, "OpenWithProgIds")); openWithKey.SetValue(progId, String.Empty); openWithKey.Close(); } /// <summary> /// 登出檔案關聯 /// </summary> 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) { } } /// <summary> /// 類別識別項註冊操作 /// </summary> /// <param name="unregister">註冊或登出</param> /// <param name="progId">類別識別項</param> /// <param name="registerInHKCU">是否在HKCU中註冊檔案關聯 -- false</param> /// <param name="appId">應用程式Id</param> /// <param name="openWith">開啟檔案的進程全路徑</param> /// <param name="extensions">檔案關聯列表</param> 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:使用者操作被取消。 { // 該操作已經被使用者取消 } } } /// <summary> /// 判斷類別識別項是否註冊 /// </summary> /// <param name="progId">類別識別項</param> /// <returns>註冊了返回true</returns> public static bool IsApplicationRegistered(string progId) { return (Registry.ClassesRoot.OpenSubKey(progId) != null); } /// <summary> /// 註冊類別識別項的檔案關聯 /// </summary> /// <param name="progId">類別識別項</param> /// <param name="registerInHKCU">是否在HKCU中註冊檔案關聯 -- false /// <param name="appId">應用程式Id</param> /// <param name="openWith">開啟檔案的進程全路徑</param> /// <param name="extensions">檔案關聯列表</param> public static void RegisterFileAssociations(string progId,bool registerInHKCU, string appId, string openWith, params string[] extensions) { InternalRegisterFileAssociations(false, progId, registerInHKCU, appId, openWith, extensions); } /// <summary> /// 登出類別識別項的檔案關聯 /// </summary> /// <param name="progId">類別識別項</param> /// <param name="registerInHKCU">是否在HKCU中註冊檔案關聯 -- false /// <param name="appId">應用程式Id</param> /// <param name="openWith">開啟檔案的進程全路徑</param> /// <param name="extensions">檔案關聯列表</param> public static void UnregisterFileAssociations(string progId, bool registerInHKCU, string appId, string openWith, params string[] extensions) { InternalRegisterFileAssociations(true, progId, registerInHKCU, appId, openWith, extensions); } }}
/// <summary>/// 添加Known Tasks/// </summary>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方法,讓銜接的路徑可以傳入應用程式,以便開啟應用程式關聯的檔案:
/// <summary>/// 應用程式的主進入點。/// </summary>[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 });}
/// <summary>/// 應用程式檔案/// </summary>public string FilePath{ get { return (_strFilePath); } set { _strFilePath = value; if (!string.IsNullOrEmpty(_strFilePath)) { _pictureBox.ImageLocation = _strFilePath; } }}
建立Custom Category 列表
如同上文建立JumpList 的方式:
1. 通過JumpListCustomCategory類建立“自訂分類”列表執行個體。
2. 由JumpListCustomCategory(string categoryName) 方法為列表命名。
3. 使用AddJumpListItems方法將連結加入到分類中。如下代碼所示:
/// <summary>/// 添加Custom Tasks/// </summary>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(); }}
C# Windows 7工作列開發之捷徑清單(Jump Lists)