How to popup a new context menu


This MxDocument_OnContextMenu function displays a new context menu when you right click on the Data View in ArcMap. This function creates the context menu on the fly, adds some built in ArcMap commands and a sub-menu to it, and then pops up the menu.

How to use

  1. Paste this code into the Project.ThisDocument in the Visual Basic Editor for ArcMap.
  2. In ArcMap, right click on the Data View to popup your new context menu.
[VBA]
Private Function MxDocument_OnContextMenu(ByVal X As Long, ByVal Y As Long) _
                                          As Boolean
    ' Create a shortcut menu.
    Dim pContextMenu As ICommandBar
    Set pContextMenu = CommandBars.Create("MyContextMenu", _
                       esriCmdBarTypeShortcutMenu)
    
    ' Add 3 built in commands to the new context menu using the built in
    ' ArcID module.
    pContextMenu.Add ArcID.PanZoom_Down
    pContextMenu.Add ArcID.PanZoom_FullExtent
    pContextMenu.Add ArcID.PanZoom_PageLeft
    
    ' Create a sub-menu called "MySubMenu" on the new context menu.
    Dim pSubMenu As ICommandBar
    Set pSubMenu = pContextMenu.CreateMenu("MySubMenu", 3)
    
    
    ' Add a few items to the sub-menu.
    pSubMenu.Add ArcID.PanZoom_Down
    pSubMenu.Add ArcID.PanZoom_FullExtent
    pSubMenu.Add ArcID.PanZoom_PageLeft
    
    ' Popup the menu.
    pContextMenu.Popup
    
    ' Let the application know that the OnContextMenu event was handled.
    MxDocument_OnContextMenu = True
    
End Function