This sample shows how to create a tool which allows the user to digitize a new Circle using the RubberCircle coclass and IRubberBand interface. The RubberBand allows the user to drag out a circle from the center until the mouse button is released. A procedure is then called which takes the result of the rubberband operation (ICircularArc) and creates a new polygon object. The polygon is then used to create a new CircleElement which is in turn added to the Map's BasicGraphicsLayer for display.
How to use:
-
In ArcMap, select Tools | Customize...
-
Select the Commands tab and scroll down to UIControls in the left pane.
-
Click NewUIControl...
-
In the dialog which appears select UIToolControl and then click Create.
-
Drag the new tool which appears in the right pane onto a toolbar of your choosing.
-
Right-click over your tool on the toolbar and select View Source.
-
Copy and paste the code below into the VBA code pane which appears.
-
Close the VBA Window and return to the ArcMap Window.
-
Now select your tool from the toolbar and digitize an Circle. Click the mouse where the center of the circle is to be and then, holding down the mouse button, move the mouse until the desired radius is reached. Releasing the mouse button completes the operation and creates a new CircleElement.
Option Explicit
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, _
ByVal x As Long, ByVal y As Long)
Dim pMXDoc As IMxDocument
Dim pCircArc As ICircularArc
Dim pRubberCirc As IRubberBand
' QI for the MXDocument interface
Set pMXDoc = ThisDocument
' Create a new RubberCircle
Set pRubberCirc = New RubberCircle
' Return a new CircularArc from the tracker object using TrackNew
Set pCircArc = pRubberCirc.TrackNew(pMXDoc.ActiveView.ScreenDisplay, Nothing)
' Call a function that converts the ICircularArc into a Polygon and
' adds a new element to the ActiveView's BasicGraphicsLayer
AddCreateElement pCircArc, pMXDoc.ActiveView
' Refresh the ActiveView
pMXDoc.ActiveView.Refresh
End Sub
Private Sub AddCreateElement(pCircArc As ICircularArc, pAV As IActiveView)
' Takes an ICircularArc and IActiveView and creates a CircleElement
' in the ActiveView's BasicGraphicsLayer
Dim pElemFillShp As IFillShapeElement
Dim pElem As IElement
Dim pGraCont As IGraphicsContainer
Dim pSFSym As ISimpleFillSymbol
Dim pRGB As IRgbColor
Dim pSegColl As ISegmentCollection
' Create a new Polygon object and access the ISegmentCollection interface
' to add a segment
Set pSegColl = New Polygon
pSegColl.AddSegment pCircArc
' Create a new circleelement and use the IElement interface to set the
' its Geometry
Set pElem = New CircleElement
pElem.Geometry = pSegColl
' QI for the IFillShapeElement interface so that the Symbol property can be set
Set pElemFillShp = pElem
' Create a new RGBColor
Set pRGB = New RgbColor
With pRGB
.Red = 198
.Green = 255
.Blue = 214
End With
' Create a new SimpleFillSymbol and set its Color and Style
Set pSFSym = New SimpleFillSymbol
pSFSym.Color = pRGB
pSFSym.Style = esriSFSSolid
pElemFillShp.Symbol = pSFSym
' QI for the IGraphicsContainer interface from the IActiveView, allows
' access to the BasicGraphicsLayer
Set pGraCont = pAV
'Add the element at Z order zero
pGraCont.AddElement pElemFillShp, 0
End Sub