理解shell命名空间扩展

时间:2022-02-20 11:20:48

原文地址:

Understanding Shell Namespace Extensions

Windows Explorer provides a graphical representation of the Shell namespace combined with tools that allow users to interact with Shell objects. With a namespace extension, you can take any body of data and have Windows Explorer present it to the user as a virtual folder. When a user browses into this folder, your data is presented as a tree-structured hierarchy of folders and files, much like the rest of the Shell namespace. Users and applications are able to interact with the contents of this virtual folder in much the same way as with any other namespace object. This document discusses how to create a namespace extension.


windows的资源管理器提供了一个shell命名空间的图形化表现方式,该图形界面表现形式和一些小工具捆绑在一起,这些小工具可以允许用户和shell的对象进行交互。使用shell命名空间扩展,你可以携带任何形式的数据,并且,让windows资源管理器把它作为一个虚拟文件夹的形式呈现给用户。当用户浏览到这个目录的时候,你的数据以一个文件夹和文件构成的树结构呈现,非常类似于shell命名空间的其他(文件夹)。用户和应用程序能够以一种类似于和其他命名空间对象交互的方式,来和虚拟文件夹的内容交互。本文讨论如何创建一个命名空间扩展。


1) How a Namespace Extension Works

命名空间扩展如何工作

Behind the scenes, every folder that Windows Explorer displays is represented by a Component Object Model (COM) object called a folder object. Each time the user interacts with a folder or its contents, the Shell communicates with the associated folder object through one of a number of standard interfaces. The folder object then does whatever is necessary to respond to the user's action, and the Shell updates the Windows Explorer display.

每一个资源管理器呈现的文件夹在幕后都是以叫做文件夹对象的COM组件形式提供的。每次用户和文件夹以及它的内容交互的时候,Shell通过一些标准的接口和关联的文件夹对象通信。文件夹对象完成所有工作来响应用户的各种动作,并且由shell本身来更新windows资源管理器的显示。

The majority of the files and folders that users interact with are part of the file system or a system virtual folder such as the Recycle Bin. Other documentation has discussed how you can customize the behavior of these standard folders to meet the requirements of your application by modifying the registry or by implementing Shell extension handlers. However, extending the Shell in these ways is most useful when your information can be readily packaged in the form of normal file system files or folders.

用户交互的大部分文件和文件夹都是文件系统或者一个虚拟文件夹系统(比如回收站)的一部分。其他一些文档已经讨论过如何通过修改注册表,或者实现一些shell扩展处理器,来定制这些标准文件夹的行为来满足你的应用程序的需求。不过,只有当你的信息可以打包成标准的文件或者文件夹的时候,通过这种方式来扩展shell的功能是最有用的。

There are a number of situations where storing data as a collection of file-system folders and files might be undesirable or even impossible. Some examples of this type of data include:

有些情形下,把数据存储称文件系统的文件夹和文件的集合不是希望的,或者根本不可能。比如,这些类型的数据类型包括:

1) A collection of items that is most effectively packaged in a single file, such as a database.

很多项目的集合,并且已经被有效的打包成单个文件,例如一个数据库(文件)。
2) A collection of items stored on a remote computer that does not have a standard Windows file system. An example of such data is the information stored on a personal digital assistant (PDA) or digital camera.

存储在远程计算机上的项目的集合,但是这个远程系统不是一个标准的windows文件系统,这种数据的一个例子就是存储在PDA或者数码相机上的数据。

3) A collection of items that does not represent stored data. An example of such data is the printer links contained by the standard Printers folder.

一些不表现为存储数据的项目集合。一个例子就是包含在标准打印机文件夹里面的打印机链接。


One way to present this kind of data to a user is to write an application to allow users to view and interact with the various items. However, if your data can be presented as a folder/file hierarchy, much of the functionality you will need to implement might be user interface services that are already provided by Windows Explorer. A much more efficient approach could be to write a namespace extension and let Windows Explorer become your GUI.

一种给用户呈现这种数据的方式就是写一个应用程序让用户来查看和与这些数据交互。不过,如果你的数据可以以文件夹/文件的结构来呈现,你需要实现的大部分功能可能是用户接口服务,而这些服务已经被windows资源管理器提供了。(所以),一个更有效的方针可能是写一个命名空间扩展并且让windows资源管理器作为你的程序的GUI。


To implement a namespace extension, your information must be organized as a tree-structured namespace. Yournamespace root is presented as a virtual folder in the Shell namespace. The root folder, and all its subfolders and data items, becomes part of the Shell namespace, and Windows Explorer becomes your user interface. You can thus present your information to the user in a familiar and readily accessible way with much less UI programming than would be required for a custom application.

为了实现一个命名空间扩展,你的信息必须以一个树形结构名字空间来组织。你的命名空间的根节点以一个shell命名空间的虚拟文件夹的方式呈现。根文件夹,以及他的子文件夹和数据项,(也)成为shell命名空间的一部分,并且,windows资源管理器成为你的(程序的)用户接口。你可以以一个熟悉的已经可用的方式来为用户提供你的信息,这个方式(只)需要很少的ui编程用于定制(你的)应用程序。


A namespace extension consists of two basic components:
1) A data manager
2) An interface between the data manager and Windows Explorer

命名空间扩展由两个基本组件构成:

1)数据管理器。

2)一个在数据管理器和windows资源管理器之间(通信的)接口。


The first component on the list is entirely up to you. You can store and manage your data in whatever way is most effective. The second component is the code needed to package your data as folder objects and handle the interaction with Windows Explorer. Windows Explorer can then call these objects to allow users to view and interact with your data as if it were a collection of folders and files. Your namespace extension's folder objects must interact with Windows Explorer as if they were normal folders. Before attempting to implement a namespace extension, you must first understand how Windows Explorer handles a folder object.

这第一个组件需要你来完成。你可以以你认为最有效的方式来存储和管理你的数据。第二个组件主要是这样一些代码,这些代码(1)把你的数据打包成文件夹对象并且(2)处理和windows资源管理器之间的交互。windows资源管理器然后可以调用这些对象来允许用户查看和和操作你的数据,就好像你的数据是标准的文件夹和文件。你的命名空间扩展的文件夹对象必须和windows资源管理器交互,就好像他们是标准的文件夹。在视图实现一个命名空间扩展之前,你必须首先理解windows资源管理器是如何处理一个文件夹对象的。

2) The Default System Folder View Object (DefView)

默认的系统文件夹视图对象

The Shell provides a default implementation of the folder view, colloquially known as DefView, so that you can avoid much of the work of implementing your own namespace extension. Because some view features cannot be achieved through custom views, it is often recommended that the default system folder view object is used in place of a custom view. For more information, see SHCreateShellFolderView.

shell提供了一个默认的文件夹视图实现,习惯叫做DefView,这样的话,你就可以避免很多实现你自己的命名空间扩展的工作。因为有一些视图特征不能通过定制视图来达到,推荐使用默认的系统文件夹视图来代替定制视图。更多参考,请看SHCreateShellFolderView.


3) How Windows Explorer Interacts with a Namespace Extension

windows资源管理器如何与命名空间扩展交互

Windows Explorer provides users with a GUI that allows them to do a variety of tasks, including:

  • Navigating the namespace hierarchy and viewing the contents of folders.
  • Managing the contents of the namespace by moving, deleting, and copying objects.
  • Retrieving a variety of information about objects.
  • Launching applications.

The Windows Explorer GUI has five basic components. The following illustration names the components and shows where they are typically displayed within Windows Explorer.

理解shell命名空间扩展

When a user displays a folder that belongs to a namespace extension in Windows Explorer, the folder object has at least partial control over the contents of all five areas.

windows资源管理器给用户提供了一个GUI来让他们完成许多任务,包括:

× 导航 在命名空间里面的导航并且查看文件夹的内容。

× 管理 管理命名空间的内容,移动,删除,复制对象。

× 获取 获取对象的一些列信息。

× 启动 启动应用程序。


windows资源管理器的GUI有5个基本组成部分。下图阐述了组件的名字并且显示了它们通常在windows资源管理器里面是如何展示的。

理解shell命名空间扩展

当一个用户显示了一个文件夹,这个文件夹属于命名空间扩展的时候,这个文件夹对象至少能部分控制5个组件的内容。


3.1) Tree View

树形视图

The tree view provides a high-level view of the namespace. This area hosts a tree view control that can display every namespace folder and the folder's position in the namespace hierarchy. A user can perform several operations with the tree view area, including:

  • Displaying or hiding the next level in the namespace.
  • Copying, moving, or deleting folders.
  • Right-clicking a folder to display a shortcut menu.
  • Selecting a folder and viewing its contents in the folder view.

The tree view communicates with folder objects primarily through their IShellFolder interface. For example, when a user clicks the plus sign (+) next to the folder's icon, Windows Explorer expands the display to show the folder's subfolders. To obtain the information needed to update the tree view, the Shell makes several calls to the folder object's IShellFolderinterface to:

  • Request the folder's attributes.
  • Enumerate the contents of the folder.
  • Request display names for each subfolder.
  • Request an icon to display next to each folder.

Windows Explorer then updates the tree view to show the selected folder's subfolders. If the subfolders have subfolders, a '+' character is displayed next to their folder icon. There are a number of more sophisticated tasks that a user can also perform with the tree view, including:

  • Using the Clipboard to cut or copy a folder and paste it into another folder.
  • Using drag-and-drop to cut or copy a folder and drop it on another folder.
  • Using a search engine to search for items in a folder or its subfolders.
  • Modifying the folder's properties.

For a more detailed discussion of how a namespace extension handles these user actions, see Implementing the Basic Folder Object Interfaces.

树形视图提供了一个命名空间的高层次视图。该区域放置了一个《树形视图控件》,它能够显示每一个命名空间的文件夹和文件夹在命名空间整体中的位置。一个用户可以在树形视图上完成以下操作,包括:

× 显示和隐藏命名空间的下一个级别。

× 复制,移动和删除文件夹。

× 右击一个文件夹来显示一个上下文菜单。

× 选择一个文件夹并且在文件夹视图(译者按:在右侧的区域)查看它的内容。

windows资源管理器更新树形视图来显示选中的文件夹的子文件夹。如果那个子文件夹有子文件夹,那么,就会在它的文件夹图标的旁边显示一个+号。用户还可以在树形控件上完成很多复杂的任务,包括:

× 使用剪贴簿来剪切或者复制一个文件夹,并且把它粘贴到另一个文件夹。

× 使用拖拽来剪切或者复制一个文件夹,并把它放到另一个文件夹。

× 使用所搜引擎来搜索它的项目和它的子文件夹的项目。

× 修改文件夹的树形。

要想得到更多关于命名空间扩展如何处理这些用户动作方面的细节讨论,参考实现基本的文件夹对象接口( Implementing the Basic Folder Object Interfaces)。


3.2) Folder View

文件夹视图

When a user selects a folder, the contents of the folder are displayed in the folder view. To some extent, the normal functionality of the folder view overlaps with the tree view. Users can move or copy folders, change folder properties, view the contents of a subfolder, display a shortcut menu for a folder, and so on. However, there are some distinct differences between tree view and folder view:

  • Folder view displays only the contents of a single folder, not part or all of the namespace hierarchy.
  • Folder view displays file objects as well as folder objects.
  • Folder view can display much more information about objects than tree view.
  • Folder view allows namespace extensions to have almost complete control over what information is displayed and how. Only minor aspects of the tree view, such as folder icons, can be modified.

Unlike the tree view, Windows Explorer does not directly control the contents of the folder view. The folder view is an area that Windows Explorer provides to folder objects. Displaying and managing the contents of a folder in the folder view are the responsibility of the folder object. Although most folder views follow a fairly standard format, there are actually few limitations on what can be displayed or how. An extreme case is the Internet folder, which is a full-featured browser.

When a user selects a folder that belongs to your namespace extension, you create a window and pass its handle to Windows Explorer. This window becomes a child of the folder view window. Windows Explorer provides the dimensions of the folder view window but places no restrictions on the content of your child window. You can then use the child window to display the folder's folder view.

Namespace extensions use one of two approaches for creating a folder view:

  • Use your child window to host a list view control. This control allows you to display the contents of a folder in much the same way as the Windows Explorer classic view.
  • Use your child window to host a WebBrowser control and use a Dynamic HTML (DHTML) document to display the contents of the folder.

Both approaches display a folder view that looks very much like that displayed for system folders. However, if you want to use a different display scheme, you are free to do so.

当用户选择一个文件夹的时候,文件夹的内容显示在文件夹视图里面。额外补充一下,正常的文件夹视图的功能和树形视图的功能有重叠。用户可以移动或者复制文件夹,改变文件夹的属性,查看子文件夹的内容,显示文件夹的快捷菜单(上下文菜单,右击)等等。不过在树形视图和文件夹视图之间还有一些显著的不同:

× 文件夹视图只显示一个文件夹的内容,不是部分或者全部命名空间整体。

× 文件夹视图显示文件对象和文件夹对象。

× 文件夹视图可以比树形空间显示更多的对象信息。

× 文件夹视图允许命名空间扩展几乎完全的控制信息的显示方式和如何显示。树形视图的很少一部分,例如文件夹图标,是可以被修改的。

不同于树形视图,windows资源管理器不直接控制文件夹视图的显示内容。文件夹视图是一个windows资源管理器提供给文件夹对象的一个区域。在文件夹视图里面显示和管理文件夹的内容是文件夹对象的责任。尽管大多数文件夹视图遵守相当标准的格式,实际上,关于显示什么和如何显示,几乎没与什么限制。极端的一个例子就是,显示一个互联网文件夹,这个时候,(文件夹视图)它就是一个完成特性的浏览器。

当用户选择一个属于的命名空间的文件夹的时候,你创建一个窗口并且把(窗口)句柄传递给windows资源管理器。这个窗口就成为了一个文件夹视图窗口的子窗口。windows资源管理器提供文件夹视图窗口的大小尺寸,但是不限制你的子窗口的内容。你可以使用子窗口来显示这个文件夹的文件夹视图。

命名空间扩展使用以下2个指导意见中的一个来创建一个文件夹视图:

× 使用你的子窗口来放置一个《列表控件》。这个控件允许你以windows资源管理器的经典视图的方式来显示一个文件夹的内容。

× 使用你的子窗口来放置一个网页浏览器控件,并且使用一个动态html文档来显示文件夹的内容。

以上2中方针都是以非常类似于系统文件夹的方式显示文件夹视图。不过,你可以随意使用一个不同的方式来显示。

3.3) Menu Bar and Toolbars

菜单栏和工具栏

Like most Windows applications, Windows Explorer provides the user with a collection of tools. A complete selection of tools is available through the menu bar. The more commonly used tools are also represented by buttons or edit boxes on a toolbar. Unlike many Windows applications, the Windows Explorer menu bar is actually a toolbar control that has been customized to behave like a conventional menu. Both the menu bar and the toolbar are incorporated into a rebar controlto allow users to organize the individual controls to suit their needs.

和其他windows应用程序类似,windows资源管理器给用户提供了一组工具:一组完整的工具选集在菜单栏可用。更常用的工具通过按钮和编辑框的形式放置在工具栏上。不同于其他windows应用程序,windows资源管理器菜单栏实际是一个工具栏控件,它被定制并表现的像一个传统菜单。菜单栏和工具栏都被合并到一个rebar控件,这使得用户可以组织各个空间来适应他们的需要。

By default, Windows Explorer supports a standard set of buttons and menu items, such as Copy and Properties. Your namespace extension can customize the menu bar and toolbars by deleting standard tools and adding custom tools. When your folder view object is initialized, Windows Explorer passes a pointer to its IShellBrowser interface. This interface supports several methods that you can call to customize the menu bar and toolbar. When the user selects one of your custom menu items or toolbar buttons, Windows Explorer forwards WM_COMMAND messages for custom menu and toolbar items to your child window's window procedure.

默认的,windows资源管理器提供了一组标准按钮和菜单,例如复制和属性。你的命名空间扩展可以定制菜单栏和工具栏,这需要你删除一些标准工具和添加一些定制工具。当你的文件夹视图对象初始化以后,windows资源管理器传递一个IShellBrowser 接口的指针(给文件夹视图对象)。这个接口提供了一些防范使得你可以定制菜单栏和工具栏。当用户选择你定制的菜单选项或者工具栏按钮的时候,windows资源管理器为你的菜单项或者工具栏按钮转发一个WM_COMMAND消息给你的子窗口窗口处理过程。


3.4) Status Bar

状态栏

The Windows Explorer status bar displays information about the currently selected object. Your namespace extension can use the status bar to display status information, such as a text string. You can customize the status bar by calling IShellBrowser.

windows资源管理器的状态栏显示当前选中对象的信息。你的命名空间扩展可以使用状态栏来显示状态信息,例如,一个字符串。你可以通过调用IShellBrowser定制状态栏。