About the Custom UI elements using add-ins Sample
[C#]
AddGraphicsTool.cs
using System; using System.Collections.Generic; using System.Text; using System.IO; using ESRI.ArcGIS.ArcMapUI; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geometry; namespace CustomUIElements { public class AddGraphicsTool : ESRI.ArcGIS.Desktop.AddIns.Tool { public AddGraphicsTool() { } protected override void OnUpdate() { Enabled = ArcMap.Application != null; } protected override void OnMouseDown(MouseEventArgs arg) { //Get the active view from the AecMap static class IActiveView activeView = ArcMap.Document.ActiveView; //if polyline object then get from the user's mouse clicks. IPolyline polyline = GetPolylineFromMouseClicks(activeView); //Make a color to draw the polyline. IRgbColor rgbColor = new RgbColorClass(); rgbColor.Red = 255; //Add the user's drawn graphics as persistent on the map. AddGraphicToMap(activeView.FocusMap, polyline, rgbColor, rgbColor); //Best practice: Only redraw the portion of the active view that contains graphics. activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); } #region "Add Graphic to Map" ///<summary>Draw a specified graphic on the map using the supplied colors.</summary> /// ///<param name="map">An IMap interface.</param> ///<param name="geometry">An IGeometry interface. It can be of the geometry type: esriGeometryPoint, esriGeometryPolyline, or esriGeometryPolygon.</param> ///<param name="rgbColor">An IRgbColor interface. The color to draw the geometry.</param> ///<param name="outlineRgbColor">An IRgbColor interface. For those geometry's with an outline it will be this color.</param> /// ///<remarks>Calling this function will not automatically make the graphics appear in the map area. Refresh the map area after calling this function with Methods like IActiveView.Refresh or IActiveView.PartialRefresh.</remarks> public void AddGraphicToMap(IMap map, IGeometry geometry, IRgbColor rgbColor, IRgbColor outlineRgbColor) { IGraphicsContainer graphicsContainer = (IGraphicsContainer)map; // Explicit Cast IElement element = null; if ((geometry.GeometryType) == esriGeometryType.esriGeometryPoint) { // Marker symbols ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass(); simpleMarkerSymbol.Color = rgbColor; simpleMarkerSymbol.Outline = true; simpleMarkerSymbol.OutlineColor = outlineRgbColor; simpleMarkerSymbol.Size = 15; simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle; IMarkerElement markerElement = new MarkerElementClass(); markerElement.Symbol = simpleMarkerSymbol; element = (IElement)markerElement; // Explicit Cast } else if ((geometry.GeometryType) == esriGeometryType.esriGeometryPolyline) { // Line elements ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass(); simpleLineSymbol.Color = rgbColor; simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid; simpleLineSymbol.Width = 5; ILineElement lineElement = new LineElementClass(); lineElement.Symbol = simpleLineSymbol; element = (IElement)lineElement; // Explicit Cast } else if ((geometry.GeometryType) == esriGeometryType.esriGeometryPolygon) { // Polygon elements ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass(); simpleFillSymbol.Color = rgbColor; simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSForwardDiagonal; IFillShapeElement fillShapeElement = new PolygonElementClass(); fillShapeElement.Symbol = simpleFillSymbol; element = (IElement)fillShapeElement; // Explicit Cast } if (!(element == null)) { element.Geometry = geometry; graphicsContainer.AddElement(element, 0); } } #endregion #region "Get Polyline From Mouse Clicks" ///<summary> ///Create a polyline geometry object using the RubberBand.TrackNew method when a user click the mouse on the map control. ///</summary> ///<param name="activeView">An ESRI.ArcGIS.Carto.IActiveView interface that will user will interact with to draw a polyline.</param> ///<returns>An ESRI.ArcGIS.Geometry.IPolyline interface that is the polyline the user drew</returns> ///<remarks>Double click the left mouse button to end tracking the polyline.</remarks> public IPolyline GetPolylineFromMouseClicks(IActiveView activeView) { IScreenDisplay screenDisplay = activeView.ScreenDisplay; IRubberBand rubberBand = new RubberLineClass(); IGeometry geometry = rubberBand.TrackNew(screenDisplay, null); IPolyline polyline = (IPolyline)geometry; return polyline; } #endregion } }
[Visual Basic .NET]
AddGraphicsTool.vb
Imports Microsoft.VisualBasic Imports System Imports System.Collections.Generic Imports System.Text Imports System.IO Imports ESRI.ArcGIS.ArcMapUI Imports ESRI.ArcGIS.Framework Imports ESRI.ArcGIS.Geodatabase Imports ESRI.ArcGIS.SystemUI Imports ESRI.ArcGIS.Display Imports ESRI.ArcGIS.Carto Imports ESRI.ArcGIS.Geometry Namespace CustomUIElements Public Class AddGraphicsTool Inherits ESRI.ArcGIS.Desktop.AddIns.Tool Public Sub New() End Sub Protected Overrides Sub OnUpdate() Enabled = My.ArcMap.Application IsNot Nothing End Sub Protected Overrides Sub OnActivate() MyBase.OnActivate() End Sub Protected Overrides Sub OnMouseDown(ByVal arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs) MyBase.OnMouseDown(arg) 'Get the active view from the AecMap static class Dim activeView As IActiveView = My.ArcMap.Document.ActiveView 'if polyline object then get from the user's mouse clicks. Dim polyline As IPolyline = GetPolylineFromMouseClicks(activeView) 'Make a color to draw the polyline. Dim rgbColor As IRgbColor = New RgbColor rgbColor.Red = 255 'Add the user's drawn graphics as persistent on the map. AddGraphicToMap(activeView.FocusMap, polyline, rgbColor, rgbColor) 'Best practice: Only redraw the portion of the active view that contains graphics. activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing) End Sub #Region "Add Graphic to Map" '''<summary>Draw a specified graphic on the map using the supplied colors.</summary> ''' '''<param name="map">An IMap interface.</param> '''<param name="geometry">An IGeometry interface. It can be of the geometry type: esriGeometryPoint, esriGeometryPolyline, or esriGeometryPolygon.</param> '''<param name="rgbColor">An IRgbColor interface. The color to draw the geometry.</param> '''<param name="outlineRgbColor">An IRgbColor interface. For those geometry's with an outline it will be this color.</param> ''' '''<remarks>Calling this function will not automatically make the graphics appear in the map area. Refresh the map area after after calling this function with Methods like IActiveView.Refresh or IActiveView.PartialRefresh.</remarks> Public Sub AddGraphicToMap(ByVal map As IMap, ByVal geometry As IGeometry, ByVal rgbColor As IRgbColor, ByVal outlineRgbColor As IRgbColor) Dim graphicsContainer As IGraphicsContainer = CType(map, IGraphicsContainer) ' Explicit Cast Dim element As IElement = Nothing If (geometry.GeometryType) = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint Then ' Marker symbols Dim simpleMarkerSymbol As ISimpleMarkerSymbol = New SimpleMarkerSymbolClass() simpleMarkerSymbol.Color = rgbColor simpleMarkerSymbol.Outline = True simpleMarkerSymbol.OutlineColor = outlineRgbColor simpleMarkerSymbol.Size = 15 simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle Dim markerElement As IMarkerElement = New MarkerElementClass() markerElement.Symbol = simpleMarkerSymbol element = CType(markerElement, IElement) ' Explicit Cast ElseIf (geometry.GeometryType) = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline Then ' Line elements Dim simpleLineSymbol As ISimpleLineSymbol = New SimpleLineSymbolClass() simpleLineSymbol.Color = rgbColor simpleLineSymbol.Style = ESRI.ArcGIS.Display.esriSimpleLineStyle.esriSLSSolid simpleLineSymbol.Width = 5 Dim lineElement As ILineElement = New LineElementClass() lineElement.Symbol = simpleLineSymbol element = CType(lineElement, IElement) ' Explicit Cast ElseIf (geometry.GeometryType) = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon Then ' Polygon elements Dim simpleFillSymbol As ISimpleFillSymbol = New SimpleFillSymbolClass() simpleFillSymbol.Color = rgbColor simpleFillSymbol.Style = ESRI.ArcGIS.Display.esriSimpleFillStyle.esriSFSForwardDiagonal Dim fillShapeElement As IFillShapeElement = New PolygonElementClass() fillShapeElement.Symbol = simpleFillSymbol element = CType(fillShapeElement, IElement) ' Explicit Cast End If If Not (element Is Nothing) Then element.Geometry = geometry graphicsContainer.AddElement(element, 0) End If End Sub #End Region #Region "Get Polyline From Mouse Clicks" '''<summary> '''Create a polyline geometry object using the RubberBand.TrackNew method when a user click the mouse on the map control. '''</summary> '''<param name="activeView">An ESRI.ArcGIS.Carto.IActiveView interface that will user will interace with to draw a polyline.</param> '''<returns>An ESRI.ArcGIS.Geometry.IPolyline interface that is the polyline the user drew</returns> '''<remarks>Double click the left mouse button to end tracking the polyline.</remarks> Public Function GetPolylineFromMouseClicks(ByVal activeView As IActiveView) As IPolyline Dim screenDisplay As IScreenDisplay = activeView.ScreenDisplay Dim rubberBand As IRubberBand = New RubberLineClass() Dim geometry As IGeometry = rubberBand.TrackNew(screenDisplay, Nothing) Dim polyline As IPolyline = CType(geometry, IPolyline) Return polyline End Function #End Region End Class End Namespace