How to Add a Custom ArcObjects C# Command to a Standard ArcMap Layer Context Menu

 

This simplified “How To” example shows how to write an ESRI ArcGIS ArcObjects custom command and extension using C#. The purpose is to demonstrate how to write an ArcObjects command so that it will appear when the user right clicks on a layer in the ArcMap table of contents (TOC). A custom extension is used to automatically add the custom command to the “right click” context menu in the ArcMap TOC for feature layers (vector data), raster layers (image data), and group layers (container for other layers).

 

Links are included to pertinent documentation on other sites.

 

Code can be downloaded by following the links at the end of this file.

 

Eicher GIS, LLC

 

http://www.eicher-gis.com

 

 

HOW IT WORKS

 

1. Implement the custom command, let’s call it LayerContextCommand

 

        public void OnCreate(object hook)

        // nothing fancy here, just get a hook to the MxDocument

  {

            if (hook != null)

            {

                    m_app = (IApplication)hook;

                    m_mxDoc = m_app.Document as IMxDocument;

 

                    m_enabled = true;

            }

  }

 

        public void OnClick()

  // use IMxDocument.ContextItem to know where the command is being called from

        {

            object MyLayer = m_mxDoc.ContextItem;

 

            if (MyLayer is FeatureLayer)

                MessageBox.Show("Feature Layer");

            else

            {

                if (MyLayer is RasterLayer)

                    MessageBox.Show("Raster Layer");

                else

                {

                    if (MyLayer is GroupLayer)

                        MessageBox.Show("Group Layer");

                    else

                        MessageBox.Show("Other layer type");

                }

              }

        }

 

2.       Implement the custom extension. In the extension, add logic to add our custom command (LayerContextCommand) to several of ArcMap’s layer context menus (e.g. feature layer context menu, raster layer context menu, group layer context menu).

 

To access a standard ArcMap context menu, we’d use IDocument.CommandBars.Find, but we can’t do this in IExtension.Startup (see http://forums.esri.com/Thread.asp?c=93&f=993&t=70199#183772). So, instead in .Startup just get a hook to the MxDocument and set up to listen for map document events (NewDocument, OpenDocument specifically).

 

        public void Startup(ref object initializationData)

        {

            // hook into the mxdocument and setup event listening

            IApplication ThisApp = initializationData as IApplication;

            m_MxDoc = ThisApp.Document as IMxDocument;

 

            // setup event listening

// for details see:

// http://edndoc.esri.com/arcobjects/9.1/default.asp?url=/arcobjects/9.1/ArcGISDevHelp/DevelopmentEnvs/DotNet/Walkthrough3CS.htm

         SetupEvents();

         }

 

Then, when we handle map document events, use IDocument.CommandBars.Find to find each layer context menu, and use ICommandBar.Add to add our custom command to a given menu. Repeat to add our command to the feature layer, raster layer, and group layer context menus.

 

        private void OnNewOpenDoc()

        {

             IDocument ThisDoc = m_MxDoc as IDocument;

             ICommandBars CommandBars = ThisDoc.CommandBars as ICommandBars;

 

// ProgIDs and GUIDs for ArcMap toolbars, menus, and commands are listed here:    

// http://edndoc.esri.com/arcobjects/9.1/default.asp?url=/arcobjects/9.1/ArcGISDevHelp/TechnicalDocuments/Guids/ArcMapIds.htm

           

             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);

            

       

 

DOWNLOAD THE SOURCE CODE

 

Custom Command (CustomClassExtension.cs)

 

Custom Extension (LayerContextCommand.cs)                    

 

 

Eicher GIS, LLC

 

http://www.eicher-gis.com