Windows7中JumpList的实现







Windows7就要发布了,近期,就要和MS组织一次社区Win7发布活动,正好这次也讲Win7TaskBar开发,所以就把要讲的东西组织成Blog,分享给出来,以供参考。

对于 Windows7 TaskBar 的开发功能是基于 COM 组件来实现的,这些组织提供了操作 Windos7 特性的一些功能。开发人员只要对 COM 操作就可以,但更为幸福的是,微软已经开发出一些 kit ,我们直接用这些 kit ,就可以用 C# Windos7 的新功能进行编程了。
对于这个 kit ,可以从
其中的 WindowsAPICodePack RegistrationHelper 是封装 TaskBar 操作的项目,我们直接用他们的 dll exe 就可以。
在做 JumpList 时,我们用到 Microsoft.WindowsAPICodePack.dll Microsoft.WindowsAPICodePack.Shell.dll Windows7.DesktopIntegration.Registration.exe
当新建一个 WPF 应用程序时,需要在引用中添加这三个可执行文件。
在对任务栏编和前,先来认识一下 ApplicationID ,在 Win7 中, ApplicationID 不是窗口的唯一标识,也不是它的 GUID ApplicationID 只是一串用来标识窗体的字符串。它最大长度为 128 个字符,我们来命名 ApplicationID 时,遵循的约定为“ Company.Product.SubProduct.Version ”。这个 ApplicationID 可以和进程,程序的快捷方式,窗体, JumpList ,文档注册类型等关联起来。
在用 ApplicationID 以前,必需先注册它,本质上这个注册是对注册表的操作。
具体代如下:
        static RegistryKey classesRoot;
        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");decimal
            shell = shell.CreateSubKey("Open");
            shell = shell.CreateSubKey("Command");
            shell.SetValue(String.Empty, openWith);
            shell.Close();
            progIdKey.Close();
}
 
关于 Win7 TaskBar 有几种效果,下面分别来说一下。
JumpList
效果图如下。
 
在图中,下方的三个选项是系统默认就有的,常用和任务,则是必需写代码来完成的。其实 JumpList 就是提供了一组快键方式。并且对快键方式进行分组分类。
首先来说一下添加和清除任务项,任务就是应用程序外的其他小工具的便键调用。
首先要注册一下 ApplicationID ,名称为
TaskbarManager.Instance.ApplicationId = "MS.TaskBarDemo.JumpList.1.0";       
要有一个 JumpList 对象
     private Microsoft.WindowsAPICodePack.Taskbar.JumpList jumplist = Microsoft.WindowsAPICodePack.Taskbar.JumpList.CreateJumpList();
    jumplist.Refresh();
现在来实现添加任务列表
string systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);
// 创建计算器
  IJumpListTask calcTask = new JumpListLink(Path.Combine(systemFolder, "calc.exe"), " 打开计算器 ")
     {
       IconReference = new IconReference(Path.Combine(systemFolder, "calc.exe"), 0)
  };
     jumplist.AddUserTasks(calcTask, new JumpListSeparator());
 jumplist.Refresh();
清除任务列表如下
            jumplist.ClearAllUserTasks();
        jumplist.Refresh();
上面这些类,都是 Microsoft.WindowsAPICodePack.dll Microsoft.WindowsAPICodePack.Shell.dll 封装的,这两个项目都是开源的。其实真正添加任务的工作(包括后面自定义 Category )都是 jumplist.Refresh() 这个方法完成的。
不防我们来看一下,运用 VS 地“转到定义”会转到一个名为“ TaskbarCOMInterfaces ”的一个页面。
其中有如下代码:
[ComImportAttribute()]
    [GuidAttribute("6332DEBF-87B5-4670-90C0-5E57B408A49E")]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface ICustomDestinationList
    {
        ……
        [PreserveSig]
        HRESULT AddUserTasks(
            [MarshalAs(UnmanagedType.Interface)] IObjectArray poa);
        ……
    }
可以看到,在 win7 中, JumpList 的编程是通过 COM 组件来实现的。
我为简单,建议开发时用 MS 封装好的 Kit ,这样编程更高效。
自定义 Category ,通常是把自己的类型或系统识别的类型添加成快捷方式。操作代码如下:
  // 创建自己定义 Category
        JumpListCustomCategory myCategory;
        private void AddCategory_But_Click(object sender, RoutedEventArgs e)
        {
            myCategory = new JumpListCustomCategory(“Category 名称 ”);
            jumplist.AddCustomCategories(myCategory);            
            jumplist.Refresh();
        }
 
        // 创建子类型
        private void subCategory_BUT_Click(object sender, RoutedEventArgs e)
        {
            string path =  Assembly.GetExecutingAssembly().Location;
            JumpListItem jli = new JumpListItem(path); 
            myCategory.AddJumpListItems(jli);   
            jumplist.Refresh();
        }
 
        // 创建子连接
        private void addLink_BUT_Click(object sender, RoutedEventArgs e)
        {
            string path = @"F://a.wmv";
            JumpListLink jll = new JumpListLink(path, " 连接 ");
            myCategory.AddJumpListItems(jll);
            jumplist.Refresh();           
        }




















本文转自桂素伟51CTO博客,原文链接: http://blog.51cto.com/axzxs/214292,如需转载请自行联系原作者








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值