.NET Shell Extensions - Shell Info Tip Handlers (信息提示处理程序)
使用 .NET 快速创建 Shell 信息提示扩展
引言
Shell 信息提示处理程序是注册到系统中用于自定义 Shell 中项目工具提示的 DLL。本文中,我将向您展示如何使用 .NET 和一个名为 SharpShell 的库来创建一个信息提示处理程序扩展。
系列文章
本文是 '.NET Shell Extensions' 系列的一部分,该系列包括
- .NET Shell Extensions - Shell Context Menus (右键菜单扩展)
- .NET Shell Extensions - Shell Icon Handlers (图标处理程序)
- .NET Shell Extensions - Shell Info Tip Handlers (信息提示处理程序)
- .NET Shell Extensions - Shell Drop Handlers (拖放处理程序)
- .NET Shell Extensions - Shell Preview Handlers (预览处理程序)
- .NET Shell Extensions - Shell Icon Overlay Handlers (图标叠加处理程序)
- .NET Shell Extensions - Shell Thumbnail Handlers (缩略图处理程序)
- .NET Shell Extensions - Shell Property Sheets (属性表处理程序)
我们的目标
为了展示 SharpShell 和 Shell 信息提示处理程序的工作原理,我们将创建一个 DLL,用于更改文件夹显示的工具提示。在我们的自定义工具提示中,我们将显示文件夹的名称和文件夹中项目的数量。实现此功能的实际代码微不足道,为了展示 SharpShell 将如何使其变得简单,下面是最终的类。
/// <summary>
/// The FolderInfoTip handler is an example SharpInfoTipHandler that provides an info tip
/// for folders that shows the number of items in the folder.
/// </summary>
[ComVisible(true)]
[COMServerAssociation(AssociationType.Directory)]
public class FolderInfoTipHandler : SharpInfoTipHandler
{
/// <summary>
/// Gets info for the selected item (SelectedItemPath)
/// </summary>
/// <param name="infoType">Type of info to return.</param>
/// <param name="singleLine">if set to <c>true</c>, put the info in a single line.</param>
/// <returns>
/// Specified info for the selected file
/// </returns>
protected override string GetInfo(RequestedInfoType infoType, bool singleLine)
{
// Switch on the tip of info we need to provide
switch (infoType)
{
case RequestedInfoType.InfoTip:
// Format the formatted info tip
return string.Format(singleLine
? "{0} - {1} Items"
: "{0}" + Environment.NewLine + "Contains {1} Items",
Path.GetFileName(SelectedItemPath),
Directory.GetFiles(SelectedItemPath).Length);
case RequestedInfoType.Name:
// Return the name of the folder
return string.Format("Folder '{0}'", Path.GetFileName(SelectedItemPath));
default:
// We won't be asked for anything else, like shortcut paths,
// for folders, so we
// can return an empty string in the default case.
return string.Empty;
}
}
}
步骤 1:创建项目
首先,创建一个新的 C# 类库项目。
提示:您可以使用 Visual Basic 而不是 C# - 在本文中,源代码是 C#,但创建 Visual Basic Shell Extension 的方法是一样的。
在这个例子中,我们将项目命名为“FolderInfoTipHandler
”。将“Class1.cs”文件重命名为“FolderInfoTipHandler.cs”。
现在添加以下引用
System.Windows.Forms
System.Drawing
这些引用包含了 SharpShell
库的其他部分所需的各种有用的小部件,例如图标和上下文菜单。
提示:如果您使用 Nuget 安装 SharpShell(参见“步骤 2”),则无需添加这些引用——它们将自动添加。
步骤 2:引用 SharpShell
现在我们需要添加对核心 SharpShell
库的引用。你可以通过几种不同的方式做到这一点:
添加引用
下载文章顶部的“SharpShell Core Library”zip 文件,并添加对 SharpShell.dll 文件的引用。
提示:本文中的下载在撰写时是正确的——如果您需要最新版本,请使用 Nuget(如下所述)或从 sharpshell.codeplex.com 获取库。
使用 Nuget
如果您已经安装了 Nuget,只需快速搜索 SharpShell 并直接安装即可——或者在 https://nuget.net.cn/packages/SharpShell 获取软件包详细信息。
使用 CodePlex
除了从本页面获取库(这可能不是最新版本)之外,您还可以随时从 CodePlex 获取最新版本的库——在 SharpShell 主页 sharpshell.codeplex.com 上。Nuget 将始终提供最新的稳定版本——CodePlex 可能提供测试版,而 CodeProject 文章将提供撰写时可用的版本。
步骤 3:从 SharpInfoTipHandler 派生
现在我们实际上将创建信息提示处理程序的功能。从 SharpInfoTipHandler
派生 FolderInfoTipHandler
类。
/// <summary>
/// The FolderInfoTip handler is an example SharpInfoTipHandler that provides an info tip
/// for folders that shows the number of items in the folder.
/// </summary>
public class FolderInfoTipHandler : SharpInfoTipHandler
{
}
SharpInfoTipHandler
只有一个您必须覆盖的函数——GetInfo
。此函数用于获取信息提示。在 MSDN 文档中,详细介绍了通过信息提示处理程序可能请求的其他类型的信息,例如项目的名称、快捷方式的详细信息(如果它是项目的快捷方式)或项目是快捷方式目标时的详细信息。请求的信息提示在 infoType
参数中,如果详细信息应仅在一行中返回,则 singleLine
参数将设置为 true
。以下是我们如何为我们的类实现此函数:
/// <summary>
/// Gets info for the selected item (SelectedItemPath)
/// </summary>
/// <param name="infoType">Type of info to return.</param>
/// <param name="singleLine">if set to <c>true</c>, put the info in a single line.</param>
/// <returns>
/// Specified info for the selected file
/// </returns>
protected override string GetInfo(RequestedInfoType infoType, bool singleLine)
{
// Switch on the tip of info we need to provide
switch (infoType)
{
case RequestedInfoType.InfoTip:
// Format the formatted info tip
return string.Format(singleLine
? "{0} - {1} Items"
: "{0}" + Environment.NewLine + "Contains {1} Items",
Path.GetFileName(SelectedItemPath),
Directory.GetFiles(SelectedItemPath).Length);
case RequestedInfoType.Name:
// Return the name of the folder
return string.Format("Folder '{0}'", Path.GetFileName(SelectedItemPath));
default:
// We won't be asked for anything else, like shortcut paths, for folders,
// so we can return an empty string in the default case.
return string.Empty;
}
}
在这个简单的例子中,我们只为对象的名称及其工具提示提供信息。
提示:据我所知,对象的名称不会显示在 Shell 中,通过信息提示处理程序请求名称时返回的值未使用。
如果系统要求工具提示显示在一行中,请务必避免换行!
步骤 4:处理 COM 注册
只剩下几件事情要做。首先,我们必须为我们的类添加 ComVisible
属性。这是因为我们的类是一个 COM 服务器,必须对尝试使用它的其他代码可见。
[ComVisible(true)]
public class FolderInfoTipHandler : SharpInfoTipHandler
接下来,我们必须为程序集指定一个强名称。虽然有绕过此要求的方法,但通常这是最好的方法。为此,右键单击项目并选择“属性”。然后转到“签名”。选择“签署程序集”,为密钥指定“新建”,然后选择一个密钥名称。如果您愿意,可以为密钥设置密码保护,但这并非必需。
最后,我们需要将我们的扩展与我们想要使用它的某些类型的 Shell 项目关联起来。我们可以使用 COMServerAssociation
属性来完成此操作。
[ComVisible(true)]
[COMServerAssociation(AssociationType.Directory)]
public class FolderInfoTipHandler : SharpInfoTipHandler
那么我们在这里做了什么?我们告诉 SharpShell,在注册服务器时,我们希望将其与系统中的文件夹关联。
您可以与文件、文件夹、类、驱动器等关联——关于使用关联属性的完整文档可在 CodePlex 网站上的 COM Server Associations 中找到。
我们完成了!构建项目会创建 FolderInfoTipHandler
程序集,该程序集可以注册为 COM 服务器,以将扩展添加到系统中,为文件夹的工具提示添加更多详细信息。
调试 Shell Extension
如果您看过我关于 .NET Shell 扩展的其他文章,您可能会认识“服务器管理器”工具。这是 SharpShell 源代码中的一个工具,可用于帮助调试 Shell 扩展。
提示:如果你想要最新版本的工具,它们可以从 CodePlex 网站预先构建好。
打开服务器管理器工具,使用 文件 > 加载服务器 来加载 FolderInfoTipHandler.dll 文件。您也可以将服务器拖到主窗口中。选择服务器将显示一些详细信息。选择服务器。
现在按“测试服务器”或使用“服务器 > 测试...”。这将打开测试 Shell,它将模拟对服务器进行的调用,就像 Windows Shell 正在进行调用一样——然而,由于这是一个托管应用程序,您可以快速附加调试器并查看您的服务器如何运行。它还允许您在不安装或注册到系统的情况下测试服务器,这将为您节省大量时间(在真实 Explorer 中测试时,您必须反复重启它才能解锁文件以便更新)。
安装和注册 Shell Extension
您可以查看 .NET Shell 扩展 - Shell 上下文菜单的“安装和注册 Shell 扩展”部分,了解如何安装和注册这些扩展的详细信息——过程是相同的。
有用资源
创建 Shell 扩展处理程序:MSDN 上关于 Shell 扩展的主页面。关于信息提示处理程序的详细信息很少。
CodePlex 上的 SharpShell:SharpShell 项目的主页——包括文档、讨论以及最新的源代码和发布。
下一步?
SharpShell 最终将提供一种机制,用于使用 .NET 创建所有可用的 Shell 扩展。到目前为止,完全支持上下文菜单扩展、图标处理程序和信息提示处理程序——请关注 CodePlex 项目,以随时了解新功能的添加。
历史
- 2013 年 4 月 6 日:初始版本