About the Save a layer file in a MapControl application Sample
[C#]
ContextMenuClass.cs
using System; using System.Runtime.InteropServices; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.Controls; namespace MapControlSaveLayerFile { /// <summary> /// Context menu class for Engine applications. ///</summary> [Guid("94e68d22-88b1-4294-ae29-0ae80ffc8468")] [ClassInterface(ClassInterfaceType.None)] [ProgId("MapControlSaveLayerFile.ContextMenuClass")] public class ContextMenuClass { private IToolbarMenu2 m_toolbarMenu = null; private bool m_beginGroupFlag = false; public ContextMenuClass() { } /// <summary> /// Instantiate the underlying ToolbarMenu and set the hook object to be /// passed into the OnCreate event of each command item. /// </summary> public void SetHook(object hook) { m_toolbarMenu = new ToolbarMenuClass(); m_toolbarMenu.SetHook(hook); // // TODO: Define context menu items here // //AddItem("esriControls.ControlsMapZoomOutFixedCommand", -1); //AddItem("esriControls.ControlsMapZoomInFixedCommand", -1); //BeginGroup(); //Separator //AddItem("{380FB31E-6C24-4F5C-B1DF-47F33586B885}", -1); //undo command //AddItem(new Guid("B0675372-0271-4680-9A2C-269B3F0C01E8"), -1); //redo command //BeginGroup(); //Separator //AddItem("MyCustomCommandCLSIDorProgID", -1); } /// <summary> /// Popup the context menu at the given location /// </summary> /// <param name="X">X coordinate where to popup the menu</param> /// <param name="Y">Y coordinate where to popup the menu</param> /// <param name="hWndParent">Handle to the parent window</param> public void PopupMenu(int X, int Y, int hWndParent) { if (m_toolbarMenu != null) m_toolbarMenu.PopupMenu(X, Y, hWndParent); } /// <summary> /// Retrieve the ToolbarMenu object in case if needed to be modified at /// run time. /// </summary> public IToolbarMenu2 ContextMenu { get { return m_toolbarMenu; } } #region Helper methods to add items to the context menu /// <summary> /// Adds a separator bar on the command bar to begin a group. /// </summary> private void BeginGroup() { m_beginGroupFlag = true; } /// <summary> /// Add a command item to the command bar by an Unique Identifier Object (UID). /// </summary> private void AddItem(UID itemUID) { m_toolbarMenu.AddItem(itemUID.Value, itemUID.SubType, -1, m_beginGroupFlag, esriCommandStyles.esriCommandStyleIconAndText); m_beginGroupFlag = false; //Reset group flag } /// <summary> /// Add a command item to the command bar by an identifier string /// and a subtype index /// </summary> private void AddItem(string itemID, int subtype) { UID itemUID = new UIDClass(); try { itemUID.Value = itemID; } catch { //Add an empty guid to indicate something's wrong "Missing" itemUID.Value = Guid.Empty.ToString("B"); } if (subtype > 0) itemUID.SubType = subtype; AddItem(itemUID); } /// <summary> /// Add a command item to the command bar by the Guid /// and a subtype index. /// </summary> private void AddItem(Guid itemGuid, int subtype) { AddItem(itemGuid.ToString("B"), subtype); } /// <summary> /// Add a command item to the command bar by a type /// and a subtype index. /// </summary> private void AddItem(Type itemType, int subtype) { if (itemType != null) AddItem(itemType.GUID, subtype); } #endregion } }
[Visual Basic .NET]
ContextMenuClass.vb
Imports Microsoft.VisualBasic Imports System Imports System.Runtime.InteropServices Imports ESRI.ArcGIS.esriSystem Imports ESRI.ArcGIS.SystemUI Imports ESRI.ArcGIS.Controls ''' <summary> ''' Context menu class for Engine applications. '''</summary> <Guid("94e68d22-88b1-4294-ae29-0ae80ffc8468"), ClassInterface(ClassInterfaceType.None), ProgId("MapControlSaveLayerFile.ContextMenuClass")> _ Public Class ContextMenuClass Private m_toolbarMenu As IToolbarMenu2 = Nothing Private m_beginGroupFlag As Boolean = False Public Sub New() End Sub ''' <summary> ''' Instantiate the underlying ToolbarMenu and set the hook object to be ''' passed into the OnCreate event of each command item. ''' </summary> Public Sub SetHook(ByVal hook As Object) m_toolbarMenu = New ToolbarMenuClass() m_toolbarMenu.SetHook(hook) ' ' TODO: Define context menu items here ' 'AddItem("esriControls.ControlsMapZoomOutFixedCommand", -1); 'AddItem("esriControls.ControlsMapZoomInFixedCommand", -1); 'BeginGroup(); //Separator 'AddItem("{380FB31E-6C24-4F5C-B1DF-47F33586B885}", -1); //undo command 'AddItem(new Guid("B0675372-0271-4680-9A2C-269B3F0C01E8"), -1); //redo command 'BeginGroup(); //Separator 'AddItem("MyCustomCommandCLSIDorProgID", -1); End Sub ''' <summary> ''' Popup the context menu at the given location ''' </summary> ''' <param name="X">X coordinate where to popup the menu</param> ''' <param name="Y">Y coordinate where to popup the menu</param> ''' <param name="hWndParent">Handle to the parent window</param> Public Sub PopupMenu(ByVal X As Integer, ByVal Y As Integer, ByVal hWndParent As Integer) If Not m_toolbarMenu Is Nothing Then m_toolbarMenu.PopupMenu(X, Y, hWndParent) End If End Sub ''' <summary> ''' Retrieve the ToolbarMenu object in case if needed to be modified at ''' run time. ''' </summary> Public ReadOnly Property ContextMenu() As IToolbarMenu2 Get Return m_toolbarMenu End Get End Property #Region "Helper methods to add items to the context menu" ''' <summary> ''' Adds a separator bar on the command bar to begin a group. ''' </summary> Private Sub BeginGroup() m_beginGroupFlag = True End Sub ''' <summary> ''' Add a command item to the command bar by an Unique Identifier Object (UID). ''' </summary> Private Sub AddItem(ByVal itemUID As UID) m_toolbarMenu.AddItem(itemUID.Value, itemUID.SubType, -1, m_beginGroupFlag, esriCommandStyles.esriCommandStyleIconAndText) m_beginGroupFlag = False 'Reset group flag End Sub ''' <summary> ''' Add a command item to the command bar by an identifier string ''' and a subtype index ''' </summary> Private Sub AddItem(ByVal itemID As String, ByVal subtype As Integer) Dim itemUID As UID = New UIDClass() Try itemUID.Value = itemID Catch 'Add an empty guid to indicate something's wrong "Missing" itemUID.Value = Guid.Empty.ToString("B") End Try If subtype > 0 Then itemUID.SubType = subtype End If AddItem(itemUID) End Sub ''' <summary> ''' Add a command item to the command bar by the Guid ''' and a subtype index. ''' </summary> Private Sub AddItem(ByVal itemGuid As Guid, ByVal subtype As Integer) AddItem(itemGuid.ToString("B"), subtype) End Sub ''' <summary> ''' Add a command item to the command bar by a type ''' and a subtype index. ''' </summary> Private Sub AddItem(ByVal itemType As Type, ByVal subtype As Integer) If Not itemType Is Nothing Then AddItem(itemType.GUID, subtype) End If End Sub #End Region End Class