visual-studio - 如何嵌套 VS 扩展的工具菜单按钮?

标签 visual-studio menu menuitem visual-studio-extensions

我正在构建我的第一个 VS 扩展,所以我目前在这方面的技能相当于遵循教程和提出问题。该扩展用于加密/解密 Web 应用程序项目的 web.config 文件的一部分。我有 2 个命令,目前按钮在 .vsct 文件中设置如下:

<Buttons>
  <Button guid="guidEncryptConfigCommandPackageCmdSet" id="EncryptConfigCommandId" priority="0x0100" type="Button">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="MyMenuGroup" />
    <Strings>
      <ButtonText>Encrypt Mail Settings</ButtonText>
    </Strings>
  </Button>
  <Button guid="guidEncryptConfigCommandPackageCmdSet" id="cmdidDecryptConfigCommand" priority="0x0100" type="Button">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="MyMenuGroup" />
    <Strings>
      <ButtonText>Decrypt Mail Settings</ButtonText>
    </Strings>
  </Button>
</Buttons>

这为我提供了 Tools 菜单中的 2 个按钮,如下所示:

Encrypt Mail Settings
Decrypt Mail Settings

我想在 Tools 菜单中只有一个顶级按钮,有 2 个嵌套按钮,每个操作一个,例如:

Config Encryptor
...Encrypt Mail Settings
...Decrypt Mail Settings

如何实现我想要的结果?

最佳答案

关于这个问题的相关文档:

Add submenu to menu , Add menu to menu bar , GUIDs and IDs for the VS Menus .

我们想要的:

点击VS中的Tools菜单=>显示Config Encryptor子菜单,点击Config Encryptor菜单会显示Encrypt Mail Settings 解密邮件设置 命令。

我的xx.vsct中的结构:

Tools menu in IDE
  --SubMenuGroup
     --SubMenu1
        --ButtonsGroup
           --EncryptConfigCommandId(Encrypt Mail Settings)
           --DecryptConfigCommandId(Decrypt Mail Settings)

Commands部分的真实内容:

<Commands package="guidEncryptConfigCommandPackage">
    <Menus>
      <Menu guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenu1" priority="0x0100" type="Menu">
        <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenuGroup"/>
        <Strings>
          <ButtonText>Config Encryptor</ButtonText>
          <CommandName>Config Encryptor</CommandName>
        </Strings>
      </Menu>
    </Menus>

    <Groups>
      <Group guid="guidEncryptConfigCommandPackageCmdSet" id="ButtonsGroup" priority="0x0600">
        <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenu1"/>
      </Group>
      <Group guid="guidEncryptConfigCommandPackageCmdSet" id="SubMenuGroup" priority="0x0600">
        <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
      </Group>
    </Groups>

    <Buttons>
      <Button guid="guidEncryptConfigCommandPackageCmdSet" id="EncryptConfigCommandId" priority="0x0100" type="Button">
        <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ButtonsGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Encrypt Mail Settings</ButtonText>
        </Strings>
      </Button>

      <Button guid="guidEncryptConfigCommandPackageCmdSet" id="DecryptConfigCommandId" priority="0x0110" type="Button">
        <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ButtonsGroup" />
        <Icon guid="guidImages" id="bmpPic1" />
        <Strings>
          <ButtonText>Decrypt Mail Settings</ButtonText>
        </Strings>
      </Button>
    </Buttons>

    <!--The bitmaps section is used to define the bitmaps that are used for the commands.-->
    <Bitmaps>
      <Bitmap guid="guidImages" href="Resources\EncryptConfigCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough"/>
    </Bitmaps>
  </Commands>

并且不要忘记在 GuidSymbol 中定义 IDSymbol:

<!-- This is the guid used to group the menu commands together -->
    <GuidSymbol name="guidEncryptConfigCommandPackageCmdSet" value="{70c1a496-88b4-4c83-8539-39decdbdb8fb}">
      <IDSymbol name="ButtonsGroup" value="0x1020" />
      <IDSymbol name="EncryptConfigCommandId" value="0x0100" />
      <IDSymbol name="DecryptConfigCommandId" value="0x0110" />
      <IDSymbol name="SubMenu1" value="0x1100"/>
      <IDSymbol name="SubMenuGroup" value="0x1150"/>
    </GuidSymbol>

根据上面这三个文档:

1.我们可以根据第一个文档在现有菜单或自定义菜单中添加子菜单。文档中没有明确描述按钮,菜单,组的结构以及它们之间的关系,但查看其代码中的内容我们可以发现 1. 要向Tools menu 添加一个submenu,我们需要将一个组设置为它的parent,然后设置工具菜单作为其父项。

2.为了将两个按钮分组到一个子菜单,我们需要将这两个按钮的父级设置为GroupB,然后将子菜单设置为GroupB的父级。

3.根据第三个文档,Visual Studio菜单栏上的菜单和组使用GUID guidSHLMainMenu。而工具菜单的ID是IDM_VS_MENU_TOOLS

这就是我编辑此结构中内容的原因,幸运的是它有效。而且由于我可能误解了文档中的某些内容,如果有什么不对的地方或者可以更好的地方,请随时纠正我:)

调试时的样子:

enter image description here

此外:

1.关于管理命令的处理程序,请参阅 this .

2.参见this document表示将子菜单添加到另一个VS菜单中,在此过程中我们需要一个组。 要将组添加到现有 Visual Studio 菜单,请将以下菜单之一设置为其父菜单。

关于visual-studio - 如何嵌套 VS 扩展的工具菜单按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57146424/

相关文章:

javascript - 在 HTML 页面中添加菜单项

eclipse - 在 Eclipse 的上下文菜单中添加行分隔符

c++ - 对重载函数 find_first_not_of 的模糊调用

c# - 如何将 Visual Studio Text Visualizer 用于自定义类型?

c# - 命名空间等中不存在类型或命名空间名称

弹出菜单的CSS在Chrome中工作不同

windows - 在 Windows 中创建菜单按钮

html - 我的固定导航栏无法正常工作

android - 工具栏菜单项错误

visual-studio - (VS2017中的SFML 2.5.1)许多 Unresolved external symbol 错误