右键单击桌面或目录背景,创建一个Shell ContextMenu

时间:2016-06-03 12:49:35

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

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

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

由于

2 个答案:

答案 0 :(得分:4)

下面介绍的两个解决方案有效,但与此同时我发现有一个更简单的解决方案,实际上已经在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扩展以处理文件夹背景 你自己
    1. 修改SharpShell以处理注册 文件夹背景的扩展名。
    2. 注册Shell扩展以自行处理文件夹背景

      您的shell扩展是一个COM服务器,因此通过GUID向系统标识。然后,在注册表中的位置使用此GUID来注册COM扩展以用于不同目的。当我们手动想要注册扩展名以扩展文件夹背景的上下文菜单时,最好是我们的扩展名具有固定的GUID。

      目前您的班级如下:

       [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,而是通过菜单工具>在Visual Studio中创建自己的唯一GUID。创建GUID。为您编写的每个shell扩展使用不同的GUID。

      然后重新编译dll并重新安装并重新注册(使用regasm或SharpShell服务器管理器工具。

      然后使用以下内容创建名为“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中添加一个新的私有字符串常量:

      /// <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";
      

      同样在big 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
       {
      

答案 1 :(得分:1)

我前段时间使用过SharpShell,从那时起就忘了它(因为它完美无缺)。我在文件和文件夹上使用它,所以你的问题引起了我的兴趣。对该工具的一点研究使我得到了答案(不幸的是)。

绑定是通过SharpShell上的com服务器关联完成的。通过查看com服务器关联的documentation,我看不到您想要的功能。

PS:我建议您在文档页面上发表评论,或直接与图书馆作者联系。他似乎真的很有帮助(我以前曾联系过他)。