ArcMap工具栏中嵌入自定义按钮

ArcMap工具栏中嵌入自定义按钮

 
 

2011-12-15     www.cxybl.com     标签: ArcMap工具栏自定义按钮

 
在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工具栏中嵌入自定义按钮的实现方式,如下代码:

  1. using System; 
  2. using System.Drawing; 
  3. using System.Runtime.InteropServices; 
  4. using ESRI.ArcGIS.ADF.BaseClasses; 
  5. using ESRI.ArcGIS.ADF.CATIDs; 
  6. using ESRI.ArcGIS.Framework; 
  7. using ESRI.ArcGIS.ArcMapUI; 
  8. using ESRI.ArcGIS.Carto; 
  9. using System.Collections; 
  10.  
  11. namespace Symbol 
  12.     [Guid("54153087-cf9c-47ce-be76-518a26cec8ea")] 
  13.     [ClassInterface(ClassInterfaceType.None)] 
  14.     [ProgId("Symbol.btCommand")] 
  15.     public sealed class btCommand : BaseCommand 
  16.     { 
  17.         #region COM Registration Function(s) 
  18.         [ComRegisterFunction()] 
  19.         [ComVisible(false)] 
  20.         static void RegisterFunction(Type registerType) 
  21.         { 
  22.             // Required for ArcGIS Component Category Registrar support 
  23.             ArcGISCategoryRegistration(registerType); 
  24.         } 
  25.  
  26.         [ComUnregisterFunction()] 
  27.         [ComVisible(false)] 
  28.         static void UnregisterFunction(Type registerType) 
  29.         { 
  30.             ArcGISCategoryUnregistration(registerType); 
  31.         } 
  32.  
  33.         #region ArcGIS Component Category Registrar generated code 
  34.         private static void ArcGISCategoryRegistration(Type registerType) 
  35.         { 
  36.             string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID); 
  37.             MxCommands.Register(regKey); 
  38.  
  39.         } 
  40.         private static void ArcGISCategoryUnregistration(Type registerType) 
  41.         { 
  42.             string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID); 
  43.             MxCommands.Unregister(regKey); 
  44.  
  45.         } 
  46.  
  47.         #endregion 
  48.         #endregion 
  49.  
  50.         private IApplication m_application; 
  51.         public btCommand() 
  52.         { 
  53.             base.m_category = "btCommand"//localizable text 
  54.             base.m_caption = "btCommand";  //localizable text 
  55.             base.m_message = "btCommand";  //localizable text 
  56.             base.m_toolTip = "btCommand";  //localizable text 
  57.             base.m_name = "btCommand";   //unique id, non-localizable (e.g. "MyCategory_ArcMapCommand") 
  58.  
  59.             try 
  60.             { 
  61.                 string bitmapResourceName = GetType().Name + ".bmp"
  62.                 base.m_bitmap = new Bitmap(GetType(), bitmapResourceName); 
  63.             } 
  64.             catch (Exception ex) 
  65.             { 
  66.                 System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap"); 
  67.             } 
  68.         } 
  69.         #region Overriden Class Methods 
  70.         public override void OnCreate(object hook) 
  71.         { 
  72.             if (hook == null
  73.                 return
  74.  
  75.             m_application = hook as IApplication; 
  76.  
  77.             //Disable if it is not ArcMap 
  78.             if (hook is IMxApplication) 
  79.                 base.m_enabled = true
  80.             else 
  81.                 base.m_enabled = false
  82.  
  83.         } 
  84.  
  85.         public override void OnClick() 
  86.         { 
  87.             // TODO: Add btCommand.OnClick implementation 
  88.             ArrayList pList = new ArrayList(); 
  89.             IMap m_map = GetMapFromArcMap(m_application); 
  90.             IEnumLayer layers = m_map.get_Layers(null,false); 
  91.             layers.Reset(); 
  92.             ILayer layer = layers.Next(); 
  93.             while(layer != null
  94.             { 
  95.                 IFeatureLayer fLayer = (IFeatureLayer)layer; 
  96.                 pList.Add(fLayer); 
  97.                 layer = layers.Next(); 
  98.             } 
  99.             TOCForm tocForm = new TOCForm(); 
  100.             tocForm.getItems(pList); 
  101.             IMxDocument m_MxDocument = GetMxDocumentFromArcMap(m_application); 
  102.             IActiveView m_ActiveView = m_MxDocument.ActiveView; 
  103.             tocForm.getActiveView(m_ActiveView); 
  104.             tocForm.ShowDialog(); 
  105.  
  106.         } 
  107.         
  108.         #region"Get MxDocument from ArcMap" 
  109.         public ESRI.ArcGIS.ArcMapUI.IMxDocument GetMxDocumentFromArcMap(ESRI.ArcGIS.Framework.IApplication application) 
  110.         { 
  111.  
  112.             if (application == null
  113.             { 
  114.                 return null
  115.             } 
  116.  
  117.             ESRI.ArcGIS.Framework.IDocument document = application.Document; 
  118.             ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = (ESRI.ArcGIS.ArcMapUI.IMxDocument)(document); // Explicit Cast 
  119.  
  120.             return mxDocument; 
  121.  
  122.         } 
  123.         #endregion 
  124.         #region"Get Map from ArcMap" 
  125.         public ESRI.ArcGIS.Carto.IMap GetMapFromArcMap(ESRI.ArcGIS.Framework.IApplication application) 
  126.         { 
  127.               if (application == null
  128.               { 
  129.                     return null
  130.               } 
  131.               ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = ((ESRI.ArcGIS.ArcMapUI.IMxDocument)(application.Document)); // Explicit Cast 
  132.               ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView; 
  133.               ESRI.ArcGIS.Carto.IMap map = activeView.FocusMap; 
  134.  
  135.               return map; 
  136.           
  137.         } 
  138.         #endregion 
  139.  
  140.         #endregion 
  141.     } 

posted on 2013-04-24 10:29  大胡子青松  阅读(757)  评论(0编辑  收藏  举报

导航