且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VSIX:将菜单项添加到Visual Studio编辑器上下文菜单

更新时间:2023-12-06 08:35:04

我能够为上下文菜单找出正确的命令组.事实证明,各种编辑器都使用单独的上下文ID,因此必须作为单独的菜单进行管理,以使操作变得混乱.

步骤

  1. 我使用HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\14.0\General键和EnableVSIPLogging值1启用了日志记录.
  2. 然后我进入编辑器,将鼠标放在空白区域,按CTRL-SHIFT,然后右键单击鼠标

这为信息提供了menu group的外观,如下所示:

---------------------------
VSDebug Message
---------------------------
Menu data:
    Guid = {D7E8C5E1-BDB8-11D0-9C88-0000F8040A53}
    GuidID = 358
    CmdID = 53
    Type = 0x00000400
    Flags = 0x00000000
    NameLoc = ASPX Context
---------------------------
OK   
---------------------------

重要的值是GUID和CommandID.

Symbols下添加Guid和Command ID,以注册将Guid映射到CommandSet并将CommandId映射到上下文菜单值的命令集:

 <GuidSymbol name="aspxContextCommandSet" value="{D7E8C5E1-BDB8-11D0-9C88-0000F8040A53}">
  <IDSymbol name="aspxContextMenu" value="0x0035"/>
</GuidSymbol>
 

请注意,该值映射到以十六进制值表示的CommandID.

然后在Groups部分中将该组作为您的命令组(MyMenuGroup)的父级:

 <Group guid="guidViewInBrowserPackageCmdSet" id="MyMenuGroup" priority="0x0000">
  <Parent guid="aspxContextCommandSet" id="aspxContextMenu"/>
</Group>
 

您引用为命令按钮创建的菜单组,并指向上一步中创建的上下文菜单.

如果要对多个编辑器(例如像我这样的ASPX,HTML和代码编辑器)执行此操作,则可以通过添加GuidSymbol和Group来为每个编辑器重复此过程.最终在同一个MenuGroup点上有多个Group条目,位于不同的父级,并且所有条目都将适当激活.

效果很好,但是您可能必须确保使用BeforeQueryStatus事件处理程序过滤OleMenuCommand对象,以确保菜单仅在实际可以处理的情况下显示.

I have an internal extension I'd like to add to Visual Studio that should hook up to the Editor Context menu - regardless of what type of file is open. I can handle enabling/visibility dynamically but essentially I'd like it to be accessible on any type of editor file.

I've not been able to find the right parent command/group ids to manage to get a custom button to display on the editor context menu. I suspect there's not a single Id but several but any guidance on what I should be looking at. Having a hard time figuring out what the proper parent command Id is to hook up to the editor context menu.

Specifically I need to be able to add View in Browser option to files that Visual Studio doesn't recognize as HTML/Web files (even though they are mapped to the appropriate editors).

Related: Is there anyway to reasonable way to discover the menu command and group names? Poking around in the SharedCommandPlace.vsct is as close as I've come but even that is proving to be very difficult to match to actual menu items.

I was able to figure out the right command groups for the context menu. It turns out the various editors all use separate context ids and so have to be managed as separate menus so this gets messy quick.

Steps

  1. I used the HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\14.0\General key and EnableVSIPLogging value of 1 to enable logging.
  2. I then navigated into the editor and with the mouse on an empty area press CTRL-SHIFT and then right click the mouse

This gives the info a menu group like and it looks like this:

---------------------------
VSDebug Message
---------------------------
Menu data:
    Guid = {D7E8C5E1-BDB8-11D0-9C88-0000F8040A53}
    GuidID = 358
    CmdID = 53
    Type = 0x00000400
    Flags = 0x00000000
    NameLoc = ASPX Context
---------------------------
OK   
---------------------------

The important values are the GUID and the CommandID.

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="aspxContextCommandSet" value="{D7E8C5E1-BDB8-11D0-9C88-0000F8040A53}">
  <IDSymbol name="aspxContextMenu" value="0x0035"/>
</GuidSymbol>

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

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

<Group guid="guidViewInBrowserPackageCmdSet" id="MyMenuGroup" priority="0x0000">
  <Parent guid="aspxContextCommandSet" id="aspxContextMenu"/>
</Group>

You reference the menu group you create for you command buttons and point at the context menu created in the previous step.

If you want to do this for multiple editors (ie. the ASPX, HTML, and Code editors for example as I do) you repeat this process for each of your editors by adding both the GuidSymbol and the Group.You'll end up with multiple Group entries for the same MenuGroup point at a different parent and all will activate appropriately.

Works great, but you'll probably have to make sure you filter your OleMenuCommand objects with a BeforeQueryStatus event handler to ensure the menu shows only when you actually can handle.