Home > Code Samples, Plug-in Development > How to add new menu entries to specific Visual Studio dropdown or context menus

How to add new menu entries to specific Visual Studio dropdown or context menus

May 3rd, 2011

Adding a new menu entry can be easily achieved using the Action DXCore control. Just set the ParentMenu property to the name of the menu, where you would like the new item to appear. As an alternative, you can set the CommonMenu to one of the suggested values. The CommonMenu property has the DevExpress.CodeRush.Menus.VSCommonBar enumeration type, which enumerates most of common Visual Studio menu names, such as File, Edit, View, Tools, DevExpress, Help, etc.

You can use names from the VsCommonBar enumeration for the ParentMenu, because usually they are the same names you see inside Visual Studio. However, it is tricky to identify the specific popup or context menus, because they don’t have any titles or captions, such as Solution Explorer menus for the file, folder, assembly items. To identify these kinds of menus, you can use the “EnableVSIPLogging” Visual Studio registry hack:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\%VS_VERSION%\General]
"EnableVSIPLogging"=dword:00000001

where the %VS_VERSION% is the version of Visual Studio, e.g. 8.0 – Visual Studio 2005, 9.0 – Visual Studio 2008, 10.0 – Visual Studio 2010.

To modify the registry, you can create a new “EnableVSIPLogging.reg” file, paste the above content and execute it, to merge the value into the registry. The key is valid for the Visual Studio version starting from VS2005 with SP1 installed.

After modifying the registry key, you can use the Ctrl+Shift shortcut when clicking over any menu item. If you hold Ctrl+Shift and click the Tools menu, the following dialog appears:

DXCore VSDebug - Tools menu

The NameLoc property can be used to identify the menu. Hold the shortcut and right-click, when the context menu is invoked, to see its properties, for example clicking on an item in Solution Explorer will result in the following dialog being displayed:

DXCore VSDebug - Item click

Don’t forget to remove the registry key or edit the value (set it to 0), to disable these dialogs.

Here are a few items that might be useful:

  • Item – the popup menu that appears when you right-click an item of a project.
  • Folder – the popup menu that appears when you right-click a folder item of a project.
  • Project – the popup menu that appears when you right-click a project.
  • Solution – the popup menu that appears when you right-click a solution.
  • Reference Root – the popup menu that appears when you right-click the “References” folder of a project.
  • Reference Item – the popup menu that appears when you right-click an assembly reference item inside the “References” folder of a project.
  • App Designer Folder – the popup menu that appears when you right-click the “Properties” folder where the AssemblyInfo file resides.

An alternative way to add new menu entries is adding them directly, using the Menus DXCore service APIs. Here’s the sample code (pasted into the InitializePlugIn method) on how to achieve that. The following code will add a new “My New Button” menu entry to the context menu that appears when right-clicking on an item in the Solution Explorer.

public override void InitializePlugIn()
{
  base.InitializePlugIn();

  if (CodeRush.Menus.Bars.Exists("Item"))
  {
    MenuBar menuBar = CodeRush.Menus.Bars["Item"];
    if (menuBar != null)
    {
      IMenuButton button = menuBar.AddButton();
      button.Caption = "My New Button";
      button.TooltipText = "Button description";
      button.Click += button_Click;
      button.Visible = true;
      button.BeginGroup = true;

      IMenuControl control = menuBar[menuBar.Count - 1];
      if (control != null)
        control.BeginGroup = true;
    }
  }
}

void button_Click(object sender, MenuButtonClickEventArgs e)
{
  // Do something...
}

Show Visual Basic code… »

Public Overrides Sub InitializePlugIn()
 MyBase.InitializePlugIn()

 If CodeRush.Menus.Bars.Exists("Item") Then
 Dim menuBar As MenuBar = CodeRush.Menus.Bars("Item")
 If menuBar IsNot Nothing Then
 Dim button As IMenuButton = menuBar.AddButton()
 button.Caption = "My New Button"
 button.TooltipText = "My New Button description"
 AddHandler button.Click, AddressOf button_Click
 button.Visible = True
 button.BeginGroup = True
 Dim control As IMenuControl = menuBar(menuBar.Count - 1)
 If control IsNot Nothing Then
 control.BeginGroup = True
 End If
 End If
 End If
End Sub

Sub button_Click(ByVal sender As Object, ByVal e As MenuButtonClickEventArgs)
 ' Do something...
End Sub

 

—–
Products: DXCore
Versions: 10.2 and up
VS IDEs: any
Updated: May/04/2011
ID: D080

Similar Posts: