How To Customize Context Menu in VS .NET
- 03 Feb 2006 11:26
You already know that you can customize main menu and toolbars and you can place commands or macros on them. Unfortunately you cannot customize context (shortcut, right-click) menu this way in VS .NET 2002 and 2003. You simply cannot invoke context menu in Customize mode. You can do it easily in VS 2005.
For VS 2002/2003 we must use small trick. We create exact copy of context menu in VS main menu, right after the Help menu. This "mirror" menu is fully customizable. After we make changes to this copy, we copy them back to the context menu.
In more detail:
- Create two simple macros contextMenuCreateEditable and contextMenuUpdateLastEdited. See their source code below.
Run contextMenuCreateEditable macro. This will create editable copy of context menu on main menu bar named *Code Window*.
Customize this menu in standard way. For example I want to add Comment and Uncomment selected lines commands to context menu as I use them a lot. Note that you may see more menu entries in this mirror menu then in original context menu. These are entries that are usually not shown in all contexts (for example breakpoint related).
- You can update context menu with your changes after you are finished. Just run contextMenuUpdateLastEdited macro. It will also delete the temporary mirror menu.
That's all. It should work without any problems. You can still restore original context menu in the case you somehow destroy its contents. Simply run VS with /setup switch:
It will run in silent mode, no IDE will be shown.
Caution: Doing this will delete all customizations you have made to Visual Studio .NET menus, toolbars, and command groups. They are rebuilt according to the loaded add-ins and packages. And here is the code:
Private editedContextMenu As Microsoft.Office.Core.CommandBar = Nothing Private tmpContextMenu As Microsoft.Office.Core.CommandBar = Nothing '''<summary>Creates editable mirror context menu '''on main menu bar.</summary> Sub contextMenuCreateEditable() Dim parent, menu, tmpMenu As Microsoft.Office.Core.CommandBar Dim tmpPopUp, contextPopUp As Microsoft.Office.Core.CommandBarPopup Dim ctrl, ctrl2 As Microsoft.Office.Core.CommandBarControl parent = DTE.CommandBars("MenuBar") menu = DTE.CommandBars("Code Window") tmpPopUp = DirectCast(parent.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, _ ), Microsoft.Office.Core.CommandBarPopup) tmpPopUp.Caption = "*" & menu.Name & "*" tmpMenu = tmpPopUp.CommandBar For Each ctrl In menu.Controls ctrl2 = ctrl.Copy(tmpMenu) ctrl2.BeginGroup = ctrl.BeginGroup Next editedContextMenu = menu tmpContextMenu = tmpMenu End Sub '''<summary>Copies edited mirror menu to context menu.</summary> Sub contextMenuUpdateLastEdited() Try Dim ctrl, ctrl2 As Microsoft.Office.Core.CommandBarControl If editedContextMenu Is Nothing Or tmpContextMenu Is Nothing Then Return End If For Each ctrl In editedContextMenu.Controls ctrl.Delete() Next For Each ctrl In tmpContextMenu.Controls ctrl2 = ctrl.Copy(editedContextMenu) ctrl2.BeginGroup = ctrl.BeginGroup Next CType(tmpContextMenu.Parent, Microsoft.Office.Core.CommandBarControl).Delete() Catch ex As System.Exception End Try End Sub
If you don't know it yet, you can learn how to create and run a macro.