using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Runtime.InteropServices; using ESRI.ArcGIS.Utility.CATIDs; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.ArcMapUI; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.Geometry; namespace LayerContextCommand { [Guid("E7B41C1D-41E6-4583-A7C5-4B684BA2C7EF")] public class CustomClassExtension : IExtension, IExtensionConfig { private esriExtensionState m_extensionState; private IMxDocument m_MxDoc; private IDocumentEvents_NewDocumentEventHandler dNewDocE; private IDocumentEvents_OpenDocumentEventHandler dOpenDocE; public CustomClassExtension() { // // TODO: Add constructor logic here // } #region "Component Category Registration" [ComRegisterFunction()] static void Reg(string regKey) { MxExtension.Register(regKey); } [ComUnregisterFunction()] static void Unreg(string regKey) { MxExtension.Unregister(regKey); } #endregion private void OnNewOpenDoc() { IDocument ThisDoc = m_MxDoc as IDocument; ICommandBars CommandBars = ThisDoc.CommandBars as ICommandBars; UID CustomCommandUID = new UID(); CustomCommandUID.Value = "LayerContextNamespace.LayerContextCommand"; // or use GUID //CustomCommandUID.Value = "{F9B2E774-29A0-4eaa-B8E2-D26DA1F21CB2}"; object Missing = Type.Missing; // add command to feature layer context menu UID MenuUID = new UID(); MenuUID.Value = "esriArcMapUI.FeatureLayerContextMenu"; // or use GUID //MenuUID.Value = "{BF643199-9062-11D2-AE71-080009EC732A}"; ICommandBar FeatureLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar; ICommandItem CustomCommandItem = FeatureLayerContextMenu.Find(CustomCommandUID, false); // if not already there, add it if (!(CustomCommandItem != null)) FeatureLayerContextMenu.Add(CustomCommandUID, ref Missing); // add command to raster layer context menu MenuUID.Value = "esriArcMapUI.RasterLayerContextMenu"; // or use GUID //MenuUID.Value = "{A34B58B0-7E60-11D2-AACE-00C04FA375FB}"; ICommandBar RasterLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar; CustomCommandItem = RasterLayerContextMenu.Find(CustomCommandUID, false); // if not already there, add it if (!(CustomCommandItem != null)) RasterLayerContextMenu.Add(CustomCommandUID, ref Missing); // add command to group layer context menu MenuUID.Value = "esriArcMapUI.GroupLayerContextMenu"; // or use GUID //MenuUID.Value = "{863A0D98-73DC-4331-8658-ED0E22247E36}"; ICommandBar GroupLayerContextMenu = CommandBars.Find(MenuUID, false, false) as ICommandBar; CustomCommandItem = GroupLayerContextMenu.Find(CustomCommandUID, false); // if not already there, add it if (!(CustomCommandItem != null)) GroupLayerContextMenu.Add(CustomCommandUID, ref Missing); } private void SetupEvents() { dNewDocE = new IDocumentEvents_NewDocumentEventHandler(OnNewOpenDoc); ((IDocumentEvents_Event)m_MxDoc).NewDocument += dNewDocE; dOpenDocE = new IDocumentEvents_OpenDocumentEventHandler(OnNewOpenDoc); ((IDocumentEvents_Event)m_MxDoc).OpenDocument += dOpenDocE; } #region IExtensionConfig Members public esriExtensionState State { get { return m_extensionState; } set { m_extensionState = value; } } public string Description { get { return "Sample extension that adds sample command to layer context menu."; } } public string ProductName { get { return "Custom Class Extension"; } } #endregion #region IExtension Members public void Shutdown() { // add code to cleanup after using ArcObjects } public string Name { get { return "customClassExtension"; } } public void Startup(ref object initializationData) { // this section provides a hook into the application framework // hook into the mxdocument IApplication ThisApp = initializationData as IApplication; m_MxDoc = ThisApp.Document as IMxDocument; SetupEvents(); } #endregion } }