名为 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 用于此目的。有两种可能的方法:
  • 注册Shell Extension处理文件夹背景
    你自己

  • 或者
  • 修改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";
    

    同样在大 switch 语句的 ServerRegistrationManager.cs 方法中的文件 CreateClassNamesForAssociations 中添加一个新案例,如下所示:
              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/

    10-11 23:17
    查看更多