ArcObjects Library Reference  

TocContextMenuClass

About the Implementing a property page for an ArcGIS Engine application Sample

[C#]

TocContextMenuClass.cs

using System;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;

namespace MapControlAppPropertyPage
{
  
  /// <summary>
  /// Context menu class hosting ArcObject commands
  /// </summary>
  public class TocContextMenuClass
  {
    //class members
    //the underlying toolbarMenu that will be used
    protected IToolbarMenu2 m_toolbarMenu = null;

    //a data-structure used in order to store and manage item definitions
    private struct ItemDef
    {
      //public members
      public string itemDef;
      public bool group;
      public int subType;

      //constructor
      public ItemDef(string itd, bool grp, int subtype)
      {
        itemDef = itd;
        group = grp;
        subType = subtype;
      }
    };

    //array of item definitions with will be used to create the commends for the context menu
    private ItemDef[] m_itemDefs = {
                                     new ItemDef("SymbolSelector.FeatureLayerSymbology", false, -1)
                                   };


    /// <summary>
    /// class constructor
    /// </summary>
    /// <param name="hook"></param>
    public TocContextMenuClass(object hook)
    {
      m_toolbarMenu = new ToolbarMenuClass();
      m_toolbarMenu.SetHook(hook);

      AddItems();

    }

    /// <summary>
    /// popup the context menu at the given location
    /// </summary>
    /// <param name="X"></param>
    /// <param name="Y"></param>
    /// <param name="hWndParent"></param>
    public void PopupMenu(int X, int Y, int hWndParent)
    {
      m_toolbarMenu.PopupMenu(X, Y, hWndParent);
    }

    /// <summary>
    /// add the items from the ItemDef array to the menu
    /// </summary>
    private void AddItems()
    {
      try
      {
        object obj = null;
        foreach (ItemDef item in m_itemDefs)
        {
          try
          {
            obj = Activator.CreateInstance(Type.GetTypeFromProgID(item.itemDef));
          }
          catch
          {
            continue;
          }

          m_toolbarMenu.AddItem(obj, item.subType, -1, item.group, esriCommandStyles.esriCommandStyleIconAndText);
        }
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message);
      }
    }


    /// <summary>
    /// Incase that the user would want to add an item at runtime
    /// </summary>
    /// <param name="item"></param>
    /// <param name="beginGroup"></param>
    /// <param name="subType"></param>
    public void AddItem(object item, bool beginGroup, int subType)
    {
      m_toolbarMenu.AddItem(item, subType, -1, beginGroup, esriCommandStyles.esriCommandStyleIconAndText);
    }
  }
}

[Visual Basic .NET]

TocContextMenuClass.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.SystemUI
Imports ESRI.ArcGIS.Controls

Namespace MapControlAppPropertyPage

  ''' <summary>
  ''' Context menu class hosting ArcObject commands
  ''' </summary>
  Public Class TocContextMenuClass
	'class members
	'the underlying toolbarMenu that will be used
	Protected m_toolbarMenu As IToolbarMenu2 = Nothing

	'a data-structure used in order to store and manage item definitions
	Private Structure ItemDef
	  'public members
	  Public itemDef As String
	  Public group As Boolean
	  Public subType As Integer

	  'constructor
      Public Sub New(ByVal itd As String, ByVal grp As Boolean, ByVal subtype As Integer)
        itemDef = itd
        group = grp
        subtype = subtype
      End Sub
	End Structure

	'array of item definitions with will be used to create the commends for the context menu
	Private m_itemDefs As ItemDef() = { New ItemDef("SymbolSelector.FeatureLayerSymbology", False, -1) }


	''' <summary>
	''' class constructor
	''' </summary>
	''' <param name="hook"></param>
	Public Sub New(ByVal hook As Object)
	  m_toolbarMenu = New ToolbarMenuClass()
	  m_toolbarMenu.SetHook(hook)

	  AddItems()

	End Sub

	''' <summary>
	''' popup the context menu at the given location
	''' </summary>
	''' <param name="X"></param>
	''' <param name="Y"></param>
	''' <param name="hWndParent"></param>
	Public Sub PopupMenu(ByVal X As Integer, ByVal Y As Integer, ByVal hWndParent As Integer)
	  m_toolbarMenu.PopupMenu(X, Y, hWndParent)
	End Sub

	''' <summary>
	''' add the items from the ItemDef array to the menu
	''' </summary>
	Private Sub AddItems()
	  Try
		Dim obj As Object = Nothing
		For Each item As ItemDef In m_itemDefs
		  Try
			obj = Activator.CreateInstance(Type.GetTypeFromProgID(item.itemDef))
		  Catch
			Continue For
		  End Try

		  m_toolbarMenu.AddItem(obj, item.subType, -1, item.group, esriCommandStyles.esriCommandStyleIconAndText)
		Next item
	  Catch ex As Exception
		System.Diagnostics.Trace.WriteLine(ex.Message)
	  End Try
	End Sub


	''' <summary>
	''' Incase that the user would want to add an item at runtime
	''' </summary>
	''' <param name="item"></param>
	''' <param name="beginGroup"></param>
	''' <param name="subType"></param>
	Public Sub AddItem(ByVal item As Object, ByVal beginGroup As Boolean, ByVal subType As Integer)
	  m_toolbarMenu.AddItem(item, subType, -1, beginGroup, esriCommandStyles.esriCommandStyleIconAndText)
	End Sub
  End Class
End Namespace