How to show a context menu in ArcGIS for Desktop applications


Summary
This topic explains how to show a custom context menu in ArcGIS for Desktop.

Showing a context menu in ArcGIS for Desktop applications

A context menu is a menu that appears upon user interaction, typically a right mouse click. Context menus are implemented following a straightforward pattern: use ICommandBar.Popup to open a particular menu in response to an event. ArcObjects exposes two OnContextMenu events (ITool.OnContextMenu and IDocumentEvents.OnContextMenu) that automatically fire when a user right-clicks in the active view. To show a context menu only when your tool is active, use ITool.OnContextMenu. To show a context menu regardless of the active tool, use an extension that responds to IDocumentEvents.OnContextMenu.
If you're working outside the active view—such as in a dialog box or a control, including the ArcGIS controls (for example, MapControl)—use the OnMouseUp or OnMouseDown events.
After deciding which event to respond to, follow these steps to show a particular menu:
  1. Get a reference to the context menu (ICommandBar interface) using one of the following methods:
  2. Determine the menu location (absolute screen location)
    • Use the default x,y value of 0, or leave the optional parameter blank (Visual Basic [VB] .NET).
    • Optional: Listen to the control mouse events and convert the location with the System.Windows.Forms.Control.PointToScreen method or use the System.Windows.Forms.Control.MousePosition static property.
  3. Show the menu using ICommandBar.Popup.
See the following code example:
[C#]
protected override bool OnContextMenu(int x, int y)
{
    ESRI.ArcGIS.esriSystem.UIDClass uid = new ESRI.ArcGIS.esriSystem.UIDClass();
    uid.Value = "AcmeContextMenu";

    IDocument document = ArcMap.Document as IDocument;
    ICommandBar menu = document.CommandBars.Find(uid, false, false)as ICommandBar;

    if (menu == null)
        return false;

    menu.Popup(0, 0); // Use 0,0 to display the menu at the current mouse location.
    return true;
}
[VB.NET]
Protected Overloads Overrides Function OnContextMenu(ByVal x As Integer, ByVal y As Integer) As Boolean
Dim uid As New ESRI.ArcGIS.esriSystem.UIDClass()
uid.Value = "AcmeContextMenu"

Dim document As IDocument = TryCast(ArcMap.Document, IDocument)
Dim menu As ICommandBar = TryCast(document.CommandBars.Find(uid, False, False), ICommandBar)

If menu Is Nothing Then
    Return False
End If

menu.Popup(0, 0) ' Use 0,0 to display the menu at the current mouse location.
Return True
End Function


See Also:

Creating toolbars and menus
Sample: Simple logging dockable window with a custom context menu




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced