Adding SubMenu to Visual Studio Project Item Node

2020-07-09 08:50发布

问题:

How can I add a menu and sub menu items when right click on a file item visual studio solution explorer?

I have one menu and three sub menu items which will be displayed when I right click on a file in solution explorer like the below picture.

I tried using .vsct buttons but it will display on context menu and Iam unable to add sub menus

回答1:

Authoring VSCT files is somewhat tricky; what you´ll need is a combination of a menu and buttons. First of all you need to reference the IDM_VS_CTXT_ITEMNODE group in your VSCT file.

<Group guid="guidCmdSet" id="grpIdMenuProjectItem" priority="0x0800">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE" />
</Group>

Than you create a new menu and add it to that group...

  <Menu guid="guidCmdSet" id="sampleMenu" type="Menu" priority="0x1000">
    <Parent guid="guidCmdSet" id="grpIdMenuProjectItem" />
    <CommandFlag>IconAndText</CommandFlag>
    <Strings>
      <ButtonText>Sample Menu</ButtonText>
      <CommandName>Sample Menu</CommandName>
    </Strings>
  </Menu>

For the submenu items another group is required, which will be added to the menu...

<Group guid="guidCmdSet" id="sampleMenuGroup" priority="0x1000">
    <Parent guid="guidCmdSet" id="sampleMenu"/>
</Group>

At least you define your submenu items using buttons and add them to the submenu group...

<Button guid="guidCmdSet" id="sampleMenuItem" priority="0x1000" type="Button">
    <Parent guid="guidCmdSet" id="sampleMenuGroup"/>
    <CommandFlag>TextOnly</CommandFlag>
    <Strings>
        <ButtonText>Sample Menu Item 1</ButtonText>
        <CommandName>sampleMenuItem1</CommandName>
    </Strings>
</Button>

Don´t forget to define all symbols, otherwise the resource won´t compile.

<IDSymbol name="grpIdMenuProjectItem" value="0x1020"/>
<IDSymbol name="sampleMenu" value="0x1021"/>
<IDSymbol name="sampleMenuGroup" value="0x1022"/>
<IDSymbol name="sampleMenuItem" value="0x1023"/>

And this is what you get...