visual-studio-2022visual-studio-extensions

Extend Document Window Context Menu in Visual Studio


I'm working on extending Visual Studio 2022 to add a button to the context menus.

I've figured out how to add buttons to the context menus inside the Code Window & the Solution Explorer, although I really want to be able to put a button inside the Document Window Tab context menu i.e. the menu that appears when you right click an opened file tab in VS.

Below is the code I'm currently using to put a button in the solution explorer / code window.

<Group guid="guidCopyUEIncludeCommandPackageCmdSet" id="HeaderGroup" priority="0x0600">
  <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>
</Group>

<Group guid="guidCopyUEIncludeCommandPackageCmdSet" id="DocumentGroup" priority="0x0600">
  <Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_CODEWIN"/>
</Group>

Could someone let me know what id= I need to get a button in the tab context? Thanks.


Solution

  • If you want to put a button inside the Document Window Tab context menu i.e. the menu that appears when you right click an opened file tab in VS, you can follow the steps below.

    Test Result

    enter image description here

    Looks like there is no similar IDM_VS_CTXT_ITEMNODE id to get the id of Document Window after searching the following documents.

    GUIDs and IDs of Visual Studio menus

    GUIDs and IDs of Visual Studio toolbars

    VsMenus Class

    However, i found a workaround: Using EnableVSIPLogging to identify menus and commands and then put a button to a specific location.

    1.Enable VSIPLogging in the registry and the EnableVSIPLogging value would be located in a key that will look like this (17.0 will be something different, specific to your setup):

    Computer\HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\17.0_3d1fbd52\General
    

    enter image description here

    2.Once this is done you can CTRL+SHIFT click on document window tab and it will show you this:

    enter image description here

    The important values are the GUID and the CommandID.

    3.Add the Guid and Command ID under Symbols like this to register the command set mapping the Guid to the CommandSet and the CommandId to the context menu values.

     <GuidSymbol name="DocumentWindowCommandSet" value="{D309F791-903F-11D0-9EFC-00A0C911004F}">
              <IDSymbol name="DocumentContextMenu" value="0x042b"/>
     </GuidSymbol>
    

    Note :the value maps to the CommandID represented as a hex value.

    4.Then reference this group as a parent for your command group (MyMenuGroup) in the Groups section:

    <Groups>
          <Group guid="guidVSIXProject1PackageCmdSet" id="MyMenuGroup" priority="0x0600">
            <Parent guid="DocumentWindowCommandSet" id="DocumentContextMenu"/>
          </Group>
        </Groups>
    

    Docs Referred:

    EnableVSIPLogging

    Adding Visual Studio Commands (Buttons) to specific locations