Windos7下JumpList的实现

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

对于Windows7 TaskBar的开发功能是基于COM组件来实现的,这些组织提供了操作Windos7特性的一些功能。开发人员只要对COM操作就可以,但更为幸福的是,微软已经开发出一些kit,我们直接用这些kit,就可以用C#Windos7的新功能进行编程了。

对于这个kit,可以从

http://www.microsoft.com/downloads/details.aspx?familyid=1C333F06-FADB-4D93-9C80-402621C600E7&displaylang=en下载获得。

其中的WindowsAPICodePackRegistrationHelper是封装TaskBar操作的项目,我们直接用他们的dllexe就可以。

在做JumpList时,我们用到Microsoft.WindowsAPICodePack.dllMicrosoft.WindowsAPICodePack.Shell.dllWindows7.DesktopIntegration.Registration.exe

当新建一个WPF应用程序时,需要在引用中添加这三个可执行文件。

在对任务栏编和前,先来认识一下ApplicationID,在Win7中,ApplicationID不是窗口的唯一标识,也不是它的GUIDApplicationID只是一串用来标识窗体的字符串。它最大长度为128个字符,我们来命名ApplicationID时,遵循的约定为“Company.Product.SubProduct.Version”。这个ApplicationID可以和进程,程序的快捷方式,窗体,JumpList,文档注册类型等关联起来。

在用ApplicationID以前,必需先注册它,本质上这个注册是对注册表的操作。

具体代如下:

 

 1 static  RegistryKey classesRoot;
 2          private   static   void  RegisterProgId( string  progId,  string  appId,
 3              string  openWith)
 4 ExpandedBlockStart.gifContractedBlock.gif         {
 5            RegistryKey progIdKey = classesRoot.CreateSubKey(progId);
 6            progIdKey.SetValue("FriendlyTypeName""@shell32.dll,-8975");
 7            progIdKey.SetValue("DefaultIcon""@shell32.dll,-47");
 8            progIdKey.SetValue("CurVer", progId);
 9            progIdKey.SetValue("AppUserModelID", appId);
10            RegistryKey shell = progIdKey.CreateSubKey("shell");
11            shell.SetValue(String.Empty, "Open");decimal 
12            shell = shell.CreateSubKey("Open");
13            shell = shell.CreateSubKey("Command");
14            shell.SetValue(String.Empty, openWith);
15            shell.Close();
16            progIdKey.Close();
17}

18

 

关于Win7TaskBar有几种效果,下面分别来说一下。

JumpList

效果图如下。

在图中,下方的三个选项是系统默认就有的,常用和任务,则是必需写代码来完成的。其实JumpList就是提供了一组快键方式。并且对快键方式进行分组分类。

首先来说一下添加和清除任务项,任务就是应用程序外的其他小工具的便键调用。

首先要注册一下ApplicationID,名称为

TaskbarManager.Instance.ApplicationId = "MS.TaskBarDemo.JumpList.1.0";       

要有一个JumpList对象

 

1   private  Microsoft.WindowsAPICodePack.Taskbar.JumpList jumplist  =  Microsoft.WindowsAPICodePack.Taskbar.JumpList.CreateJumpList();
2     jumplist.Refresh();
3

 

现在来实现添加任务列表

 

1 string  systemFolder  =  Environment.GetFolderPath(Environment.SpecialFolder.System);
2 // 创建计算器
3   IJumpListTask calcTask  =   new  JumpListLink(Path.Combine(systemFolder,  " calc.exe " ),  " 打开计算器 " )
4 ExpandedBlockStart.gifContractedBlock.gif      {
5       IconReference = new IconReference(Path.Combine(systemFolder, "calc.exe"), 0)
6  }
;
7      jumplist.AddUserTasks(calcTask,  new  JumpListSeparator());
8  jumplist.Refresh();
9

 

清除任务列表如下

 

1 jumplist.ClearAllUserTasks();
2         jumplist.Refresh();
3

 

上面这些类,都是Microsoft.WindowsAPICodePack.dllMicrosoft.WindowsAPICodePack.Shell.dll封装的,这两个项目都是开源的。其实真正添加任务的工作(包括后面自定义Category)都是jumplist.Refresh()这个方法完成的。

不防我们来看一下,运用VS地“转到定义”会转到一个名为“TaskbarCOMInterfaces”的一个页面。

 

 1 [ComImportAttribute()]
 2     [GuidAttribute( " 6332DEBF-87B5-4670-90C0-5E57B408A49E " )]
 3     [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
 4      internal   interface  ICustomDestinationList
 5 ExpandedBlockStart.gifContractedBlock.gif     {
 6       ……
 7        [PreserveSig]
 8        HRESULT AddUserTasks(
 9            [MarshalAs(UnmanagedType.Interface)] IObjectArray poa);
10       ……
11    }

12

 

可以看到,在win7中,JumpList的编程是通过COM组件来实现的。

我为简单,建议开发时用MS封装好的Kit,这样编程更高效。

自定义Category,通常是把自己的类型或系统识别的类型添加成快捷方式。操作代码如下:

 

 1 // 创建自己定义Category
 2         JumpListCustomCategory myCategory;
 3          private   void  AddCategory_But_Click( object  sender, RoutedEventArgs e)
 4 ExpandedBlockStart.gifContractedBlock.gif         {
 5            myCategory = new JumpListCustomCategory(“Category名称”);
 6            jumplist.AddCustomCategories(myCategory);            
 7            jumplist.Refresh();
 8        }

 9
10          // 创建子类型
11          private   void  subCategory_BUT_Click( object  sender, RoutedEventArgs e)
12 ExpandedBlockStart.gifContractedBlock.gif         {
13            string path =  Assembly.GetExecutingAssembly().Location;
14            JumpListItem jli = new JumpListItem(path);  
15            myCategory.AddJumpListItems(jli);    
16            jumplist.Refresh();
17        }

18
19          // 创建子连接
20          private   void  addLink_BUT_Click( object  sender, RoutedEventArgs e)
21 ExpandedBlockStart.gifContractedBlock.gif         {
22            string path = @"F://a.wmv";
23            JumpListLink jll = new JumpListLink(path, "连接");
24            myCategory.AddJumpListItems(jll);
25            jumplist.Refresh();            
26        }

27

 

 

转载于:https://www.cnblogs.com/axzxs2001/archive/2009/10/20/1586955.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值