在ArcMap工具栏中嵌入自定义按钮的实现方式,如下代码: using System; using System.Drawing; using System.Runtime.InteropServices; using ESRI.ArcGIS.ADF.BaseClasses; using ESRI.ArcGIS.ADF.CATIDs; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.
在ArcMap工具栏中嵌入自定义按钮的实现方式,如下代码:
- using System;
- using System.Drawing;
- using System.Runtime.InteropServices;
- using ESRI.ArcGIS.ADF.BaseClasses;
- using ESRI.ArcGIS.ADF.CATIDs;
- using ESRI.ArcGIS.Framework;
- using ESRI.ArcGIS.ArcMapUI;
- using ESRI.ArcGIS.Carto;
- using System.Collections;
- namespace Symbol
- {
- [Guid("54153087-cf9c-47ce-be76-518a26cec8ea")]
- [ClassInterface(ClassInterfaceType.None)]
- [ProgId("Symbol.btCommand")]
- public sealed class btCommand : BaseCommand
- {
- #region COM Registration Function(s)
- [ComRegisterFunction()]
- [ComVisible(false)]
- static void RegisterFunction(Type registerType)
- {
- // Required for ArcGIS Component Category Registrar support
- ArcGISCategoryRegistration(registerType);
- }
- [ComUnregisterFunction()]
- [ComVisible(false)]
- static void UnregisterFunction(Type registerType)
- {
- ArcGISCategoryUnregistration(registerType);
- }
- #region ArcGIS Component Category Registrar generated code
- private static void ArcGISCategoryRegistration(Type registerType)
- {
- string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
- MxCommands.Register(regKey);
- }
- private static void ArcGISCategoryUnregistration(Type registerType)
- {
- string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
- MxCommands.Unregister(regKey);
- }
- #endregion
- #endregion
- private IApplication m_application;
- public btCommand()
- {
- base.m_category = "btCommand"; //localizable text
- base.m_caption = "btCommand"; //localizable text
- base.m_message = "btCommand"; //localizable text
- base.m_toolTip = "btCommand"; //localizable text
- base.m_name = "btCommand"; //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand")
- try
- {
- string bitmapResourceName = GetType().Name + ".bmp";
- base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
- }
- catch (Exception ex)
- {
- System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
- }
- }
- #region Overriden Class Methods
- public override void OnCreate(object hook)
- {
- if (hook == null)
- return;
- m_application = hook as IApplication;
- //Disable if it is not ArcMap
- if (hook is IMxApplication)
- base.m_enabled = true;
- else
- base.m_enabled = false;
- }
- public override void OnClick()
- {
- // TODO: Add btCommand.OnClick implementation
- ArrayList pList = new ArrayList();
- IMap m_map = GetMapFromArcMap(m_application);
- IEnumLayer layers = m_map.get_Layers(null,false);
- layers.Reset();
- ILayer layer = layers.Next();
- while(layer != null)
- {
- IFeatureLayer fLayer = (IFeatureLayer)layer;
- pList.Add(fLayer);
- layer = layers.Next();
- }
- TOCForm tocForm = new TOCForm();
- tocForm.getItems(pList);
- IMxDocument m_MxDocument = GetMxDocumentFromArcMap(m_application);
- IActiveView m_ActiveView = m_MxDocument.ActiveView;
- tocForm.getActiveView(m_ActiveView);
- tocForm.ShowDialog();
- }
- #region"Get MxDocument from ArcMap"
- public ESRI.ArcGIS.ArcMapUI.IMxDocument GetMxDocumentFromArcMap(ESRI.ArcGIS.Framework.IApplication application)
- {
- if (application == null)
- {
- return null;
- }
- ESRI.ArcGIS.Framework.IDocument document = application.Document;
- ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = (ESRI.ArcGIS.ArcMapUI.IMxDocument)(document); // Explicit Cast
- return mxDocument;
- }
- #endregion
- #region"Get Map from ArcMap"
- public ESRI.ArcGIS.Carto.IMap GetMapFromArcMap(ESRI.ArcGIS.Framework.IApplication application)
- {
- if (application == null)
- {
- return null;
- }
- ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ((ESRI.ArcGIS.ArcMapUI.IMxDocument)(application.Document)); // Explicit Cast
- ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView;
- ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap;
- return map;
- }
- #endregion
- #endregion
- }
- }