Embedded Custom button in the ArcMap Toolbar

Source: Internet
Author: User
Tags imap
Embedded Custom button in the ArcMap Toolbar

2011-12-15 www.cxybl.com label: Custom button In ArcMap Toolbar

Use the following code to embed a Custom button in the ArcMap toolbar: 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.

The following code embeds a Custom button in the ArcMap toolbar:

  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. {
  13. [Guid ("54153087-cf9c-47ce-be76-518a26cec8ea")]
  14. [ClassInterface (ClassInterfaceType. None)]
  15. [ProgId ("Symbol. btCommand")]
  16. Public sealed class btCommand: BaseCommand
  17. {
  18. # Region COM Registration Function (s)
  19. [ComRegisterFunction ()]
  20. [ComVisible (false)]
  21. Static void RegisterFunction (Type registerType)
  22. {
  23. // Required for ArcGIS Component Category Registrar support
  24. ArcGISCategoryRegistration (registerType );
  25. }
  26.  
  27. [ComUnregisterFunction ()]
  28. [ComVisible (false)]
  29. Static void UnregisterFunction (Type registerType)
  30. {
  31. ArcGISCategoryUnregistration (registerType );
  32. }
  33.  
  34. # Region ArcGIS Component Category Registrar generated code
  35. Private static void ArcGISCategoryRegistration (Type registerType)
  36. {
  37. String regKey = string. Format ("HKEY_CLASSES_ROOT \ CLSID \\{{ 0 }}", registerType. GUID );
  38. MxCommands. Register (regKey );
  39.  
  40. }
  41. Private static void ArcGISCategoryUnregistration (Type registerType)
  42. {
  43. String regKey = string. Format ("HKEY_CLASSES_ROOT \ CLSID \\{{ 0 }}", registerType. GUID );
  44. MxCommands. Unregister (regKey );
  45.  
  46. }
  47.  
  48. # Endregion
  49. # Endregion
  50.  
  51. Private IApplication m_application;
  52. Public btCommand ()
  53. {
  54. Base. m_category = "btCommand"; // localizable text
  55. Base. m_caption = "btCommand"; // localizable text
  56. Base. m_message = "btCommand"; // localizable text
  57. Base. m_toolTip = "btCommand"; // localizable text
  58. Base. m_name = "btCommand"; // unique id, non-localizable (e.g. "MyCategory_ArcMapCommand ")
  59.  
  60. Try
  61. {
  62. String bitmapResourceName = GetType (). Name + ". bmp ";
  63. Base. m_bitmap = new Bitmap (GetType (), bitmapResourceName );
  64. }
  65. Catch (Exception ex)
  66. {
  67. System. Diagnostics. Trace. WriteLine (ex. Message, "Invalid Bitmap ");
  68. }
  69. }
  70. # Region Overriden Class Methods
  71. Public override void OnCreate (object hook)
  72. {
  73. If (hook = null)
  74. Return;
  75.  
  76. M_application = hook as IApplication;
  77.  
  78. // Disable if it is not ArcMap
  79. If (hook is IMxApplication)
  80. Base. m_enabled = true;
  81. Else
  82. Base. m_enabled = false;
  83.  
  84. }
  85.  
  86. Public override void OnClick ()
  87. {
  88. // TODO: Add btCommand. OnClick implementation
  89. ArrayList pList = new ArrayList ();
  90. IMap m_map = GetMapFromArcMap (m_application );
  91. IEnumLayer layers = m_map.get_Layers (null, false );
  92. Layers. Reset ();
  93. ILayer layer = layers. Next ();
  94. While (layer! = Null)
  95. {
  96. IFeatureLayer fLayer = (IFeatureLayer) layer;
  97. PList. Add (fLayer );
  98. Layer = layers. Next ();
  99. }
  100. TOCForm tocForm = new TOCForm ();
  101. TocForm. getItems (pList );
  102. IMxDocument m_MxDocument = GetMxDocumentFromArcMap (m_application );
  103. IActiveView m_ActiveView = m_MxDocument.ActiveView;
  104. TocForm. getActiveView (m_ActiveView );
  105. TocForm. ShowDialog ();
  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. # Endregion
  138.  
  139. # Endregion
  140. }
  141. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.