您的位置:知识库 » 其他分类

JumpList中Recent类别和自定义类型

作者: 桂素伟  来源: 博客园  发布时间: 2009-10-30 11:37  阅读: 1512 次  推荐: 0   原文链接   [收藏]  

在我们使用windows系统时,我们常看到系统有很多类型,比如word的文档类型,它是以doc扩展名标识的,还有pdf,html,aspx等等,一但我们安装某些程序,相应类型程序的文档就可以打开进行编辑了。今天,我们也创建自己的一个类型,并结合JumpList的Recent来开发我们的应用。

如何让windows系统认识自己的类型,其实就是把我们的类型注册到注册表里的HKEY_CLASSES_ROOT下,具体注册信息,详看下面代码。

代码如下:

 1//注册应用程序文件和图标
 2RegistryKey classesRoot = Registry.ClassesRoot;
 3  private static void RegisterProgId(string progId, string appId,
 4            string openWith, string IcoPath)
 5        {
 6           
 7            RegistryKey progIdKey = classesRoot.CreateSubKey(progId);
 8            progIdKey.SetValue("FriendlyTypeName""@shell32.dll,-8975");
 9            progIdKey.SetValue("DefaultIcon""@shell32.dll,-47");
10            progIdKey.SetValue("CurVer", progId);
11            progIdKey.SetValue("AppUserModelID", appId);
12
13            RegistryKey shell = progIdKey.CreateSubKey("shell");
14            shell.SetValue(String.Empty, "Open");
15            shell = shell.CreateSubKey("Open");
16            shell = shell.CreateSubKey("Command");
17            shell.SetValue(String.Empty, openWith);
18
19            RegistryKey iconKey = progIdKey.CreateSubKey("DefaultIcon");
20            iconKey.SetValue("", IcoPath);
21
22            shell.Close();
23            progIdKey.Close();
24        }
25//注册类型
26      private static void RegisterFileAssociation(string progId, string extension)
27        {
28            RegistryKey openWithKey = classesRoot.CreateSubKey(
29                Path.Combine(extension, "OpenWithProgIds"));
30            openWithKey.SetValue(progId, String.Empty);
31            openWithKey.Close();
32        }
33

在这个方法中,后两个参数是比较重要的,openWith参数应用程序所以在路径和附加参数,IcoPath是应用程对应的图标。通过这一步,我们就能把自己的类型注册到系统中,具体的类型依照extension参数来提供。

这样,如果在系统下建立一个extension实参为类型的文件时,我们看到的将是以对应图标替换的文件,双击,调用的是我们刚才注册的应用程序。

比如,我们现在注册的是diar,在系统下,所有以diar为扩展名的文件,都成为可执行文件了。

但怎么通过双击把文件的内容加载到应用程序中呢?

代码如下,在应用程序的加载时执行:

1string[] parameters = Environment.GetCommandLineArgs();
2if (parameters.Length > 1)
3{
4    filePath = parameters[1];
5    //filePath传过来的就是双击的文件的路径,这样我们就可以通过IO来操作这个文件了
6}
7

其实上面这些知识不是Windows7 JumpList所特有的,怎么和JumpList中的知识关联呢?

在JumpList中,有一个Recent类别,就是最近打开的文件。其实系统有一个RecentList,会保存最近打开的文档,这个列表只有在两种情况下向其中添加子项,第一种就是上面我们在注册完类型后,双击文档时会添加到RecentList中。另一种情部下面说明。

看下面代码:

 1private void OpenDiaryFile()
 2        {
 3            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
 4            dialog.Title = "Select a diary document";
 5            dialog.Filters.Add(new CommonFileDialogFilter("Text files (*.diar)""*.diar"));
 6            CommonFileDialogResult result = dialog.ShowDialog();
 7            if (result == CommonFileDialogResult.OK)
 8            {
 9                filePath = dialog.FileName;
10                Content_TB.Text = File.ReadAllText(dialog.FileName, Encoding.Default);
11                jumplist.AddToRecent(dialog.FileName);
12                jumplist.KnownCategoryToDisplay = JumpListKnownCategoryType.Recent;//最近                // jumplist.KnownCategoryToDisplay = JumpListKnownCategoryType.Frequent ;//常用            
13                jumplist.Refresh();
14            }
15

这段代码不难理解,就是用一个定义好的CommonOpenFileDialog对话框来打开一个文件。这里的CommonOpenFileDialog是Windows 7 Training Kit For Developers的一个类,必需调用这个类,我们才能用jumplist.AddToRecent(dialog.FileName)把最近文件添加到RecentList中。

0
0
标签:Windows 7

其他分类热门文章

    其他分类最新文章

      最新新闻

        热门新闻