c# - 通过右键单击桌面或目录背景创建 Shell ContextMenu

标签 c# contextmenu windows-shell shell-extensions sharpshell

名为 SharpShell 的 .NET Shell 扩展框架是很棒的;我开发了一个右键单击文件 Shell ContextMenu,“非常容易”,可以选择文件和目录。

现在我想通过右键单击空白区域(即在桌面上或在文件夹中的白色区域)来开发一个 Shell ContextMenu。 是否有可能仍然使用 SharpShell?或者我需要转向不同的解决方案吗?......在第二种情况下......你有什么建议?

谢谢

最佳答案

下面介绍的两个解决方案有效,但与此同时我发现有一个更简单的解决方案实际上已经在 SharpShell 附带的示例中使用了。

请参阅 CopyDirectoryLocationHandler 类作为为目录背景(和桌面)注册的上下文菜单处理程序的示例:

[ComVisible(true)]
[COMServerAssociation(AssociationType.Class, @"Directory\Background")]
public class CopyDirectoryLocationHandler : SharpContextMenu
{
   // ...
}

如果您希望处理程序只处理桌面背景上的点击,请改用此代码:

[ComVisible(true)]
[COMServerAssociation(AssociationType.Class, @"DesktopBackground")]
public class CopyDirectoryLocationHandler : SharpContextMenu
{
   // ...
}

旧的过时答案:

您可以毫无问题地将 SharpShell 用于此目的。有两种可能的方法:

  1. 注册 Shell Extension 来处理文件夹背景 你自己

  1. 修改 SharpShell 来处理注册 文件夹背景的扩展名。

注册Shell Extension 自己处理文件夹后台

您的 shell 扩展是一个 COM 服务器,因此通过 GUID 被系统识别。然后,此 GUID 在注册表中的某些位置用于为不同的目的注册 COM 扩展。当我们出于某种目的(例如扩展文件夹背景的上下文菜单)手动注册扩展时,最好让我们的扩展具有固定的 GUID。

目前你的类(class)看起来像这样:

 [ComVisible(true)]
 [COMServerAssociation(AssociationType.Directory)]
 public class MyContextMenuExtension : SharpContextMenu
 {

编译时,编译器会自动生成一个 GUID 供该类使用。但是我们可以像这样指定一个特定的使用:

 [Guid("A75AFD0D-4A63-41E3-AAAA-AD08A574B8B0")]
 [ComVisible(true)]
 [COMServerAssociation(AssociationType.Directory)]
 public class MyContextMenuExtension : SharpContextMenu
 {

不要使用与此处所示相同的 GUID,而是通过菜单工具 > 创建 GUID 在 Visual Studio 中创建您自己的唯一 GUID。为您编写的每个 shell 扩展使用不同的 GUID。

然后重新编译dll并重新安装和注册(使用regasm或SharpShell Server Manager工具。

然后创建一个名为“registry.reg”的文本文件,内容如下(使用您自己的特定 GUID)。而不是“MyContextMenuExtension”,请指定您的扩展名。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shellex\ContextMenuHandlers\MyContextMenuExtension]
@="{A75AFD0D-4A63-41E3-AAAA-AD08A574B8B0}"

双击安装“registry.reg”文件。当您打开文件夹背景或桌面的上下文菜单时,您的扩展现在应该处于事件状态。

除了使用 *.reg 文件,您还可以使用注册表编辑器手动进行更改,或者如果您有安装程序,则指示安装程序进行这些注册表更改。


修改SharpShell为你处理文件夹背景扩展名的注册

对 SharpShell 源代码进行以下更改:

在文件 AssociationType.cs 中,为 AssociationType 枚举添加一个新的枚举值:

  /// <summary>
  /// Create an association to the unknown files class.
  /// </summary>
  UnknownFiles,

  /// <summary>
  /// Create an association to the background of folders and the desktop
  /// </summary>
  DirectoryBackground

在文件 ServerRegistrationManager.cs 中添加一个新的私有(private)字符串常量:

/// <summary>
/// The 'directory' special class.
/// </summary>
private const string SpecialClass_Directory = @"Directory";

 /// <summary>
/// The 'directory background' special class.
/// </summary>
private const string SpecialClass_DirectoryBackground = @"Directory\Background";

同样在文件 ServerRegistrationManager.cs 的方法 CreateClassNamesForAssociations 中的 big switch 语句中添加一个新的 case,如下所示:

          case AssociationType.Directory:

               //  Return the directory class.
               return new[] { SpecialClass_Directory };

          case AssociationType.DirectoryBackground:

               //  Return the directory background class.
               return new[] { SpecialClass_DirectoryBackground };

最后你只需要告诉你自己的扩展类使用这个新的枚举值:

 [Guid("A75AFD0D-4A63-41E3-AAAA-AD08A574B8B0")]
 [ComVisible(true)]
 [COMServerAssociation(AssociationType.Directory)]
 [COMServerAssociation(AssociationType.DirectoryBackground)]
 public class MyContextMenuExtension : SharpContextMenu
 {

关于c# - 通过右键单击桌面或目录背景创建 Shell ContextMenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37614860/

相关文章:

windows - 如何为我的文件类型使用现有的 Png-Shell-Thumbnail?

c# - HTML - 我如何知道所有框架何时加载?

.net - WPF 任务栏图标上下文菜单

c# - .NET 中的 ShellExecute 等价物

c++ - 如何在 Windows 属性系统中创建 IconList 属性?

c# - Windows Phone 8 ListPicker 使用 ContextMenu 导航离开但返回页面完全是空的

c# - 更改按钮内的椭圆填充颜色

c# - 打印窗口闪烁

c# - 如何让这个 LINQ to XML 查询更加优雅?

wpf - 中继/委托(delegate)命令和输入手势