How do I create a new popup menu item on a Visual Studio .NET IDE context menu? For example, I want to add a popup menu item to the code window context menu, and then add menu items to my popup menu. First, you cannot use the AddNamedCommand method that is generated by the Add-in Wizard. That method will only add menu items to an existing popup menu. Instead, you must use a Microsoft Office Command objects and methods. This article will show the code for adding a new system of menus to an IDE context menu.
First, in the declaration section of the add-ins Connect class, you need to dimension some objects, as follows. The first two CommandBar objects will be used for two different purposes. The first will be used for the Code Window when nothing is selected. The second will be used when you have a control selected in the Form Designer window. The third will be used to add a popup menu to the IDE's Tools Menu.
| Dim cmdBarCW As Microsoft.Office.Core.CommandBar Dim cmdBarFD As Microsoft.Office.Core.CommandBar Dim cmdBarTools As Microsoft.Office.Core.CommandBar |
| Dim popCommentMenu As Microsoft.Office.Core.CommandBarPopup Public WithEvents popCommentMenuHandler As EnvDTE.CommandBarEvents |
| ' menuitems for comment popup Dim mnuBlockComment As Microsoft.Office.Core.CommandBarControl Public WithEvents mnuBlockCommentHandler As EnvDTE.CommandBarEvents Dim mnuBlockAddition As Microsoft.Office.Core.CommandBarControl Public WithEvents mnuBlockAdditionHandler As EnvDTE.CommandBarEvents |
| Private Sub mnuBlockCommentHandler_Click(ByVal _ CommandBarControl As Object, _ ByRef handled As Boolean, _ ByRef CancelDefault As Boolean) _ Handles mnuBlockCommentHandler.Click End Sub |
| ' set up tools/codewindow menu ptr cmdBarCW = oVB.CommandBars("Code Window") cmdBarTools = oVB.CommandBars("Tools") cmdBarFD = oVB.CommandBars("Selection") |
| popCommentMenu = cmdBarCW.Controls.Add(MsoControlType.msoControlPopup, _ System.Reflection.Missing.Value, _ System.Reflection.Missing.Value, _ Before:=1, Temporary:=True) popCommentMenu.Caption = "My Comment Options" |
| ' create comment menu items and sink the event handler mnuBlockComment = _ AddOfficeMenuItem(popCommentMenu, "Block Comment") mnuBlockCommentHandler = _ CType(oVB.Events.CommandBarEvents(mnuBlockComment), _ EnvDTE.CommandBarEvents) mnuBlockAddition = _ AddOfficeMenuItem(popCommentMenu, "Block Addition") mnuBlockAdditionHandler = _ CType(oVB.Events.CommandBarEvents(mnuBlockAddition), _ EnvDTE.CommandBarEvents) |
| ' This method will add an office menuitem to an existing ' office popupup menu Public Function AddOfficeMenuItem(ByVal _ Menu As Microsoft.Office.Core.CommandBarControl, _ ByVal Caption As String, _ Optional ByVal pos As Byte = 0, _ Optional ByVal sep As Boolean = False, _ Optional ByVal Bitmap As Object = Nothing) _ As Microsoft.Office.Core.CommandBarControl Dim menuItem As Microsoft.Office.Core.CommandBarControl Try If Not (Bitmap Is Nothing) Then System.Windows.Forms.Clipboard.SetDataObject(Bitmap) End If ' Add menu item to VB menu: If pos = 0 Then pos = Menu.Controls.Count + 1 menuItem = _ Menu.Controls.Add(Type:= _ Microsoft.Office.Core.MsoControlType.msoControlButton, _ Before:=pos, _ Temporary:=True) ' Set properties of menu item: menuItem.Caption = Caption If sep Then menuItem.BeginGroup = True If Not (Bitmap Is Nothing) Then menuItem.Style = _ Microsoft.Office.Core.MsoButtonStyle. _ msoButtonIconAndCaption menuItem.PasteFace() End If Return menuItem Catch e As System.Exception StructuredErrorHandler(e.ToString) Return menuItem End Try End Function |
| ' delete all menu items and popupmenu mnuBlockComment.Delete() mnuBlockAddition.Delete() popCommentMenu.Delete() |