Use this subroutine to draw a Polygon on the screen. With this example, the polygon will not persist on the screen; it will disappear when the screen next refreshes. See How to draw polygon buffers for an example showing how drawings can be persisted on the display.
How to use
- Add a new UITool control onto any toolbar.
- Paste the code below into its OnMouseDown event.
- Mind the name of the control, the sample assumes it is called UIToolControl1.
- Completely close down VBA so mouse events will fire.
- Select this tool, click on the map to create a polygon; double-click to stop.
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 pActiveView As IActiveView
Dim pScreenDisplay As IScreenDisplay
Dim pRubberPolygon As IRubberBand
Dim pFillSymbol As ISimpleFillSymbol
Dim pRgbColor As IRgbColor
Dim pPolygon As IPolygon
Set pMxDoc = Application.Document
Set pActiveView = pMxDoc.FocusMap
Set pScreenDisplay = pActiveView.ScreenDisplay
Set pRubberPolygon = New RubberPolygon
Set pFillSymbol = New SimpleFillSymbol
Set pRgbColor = New RgbColor
pRgbColor.Red = 255
pFillSymbol.Color = pRgbColor
Set pPolygon = pRubberPolygon.TrackNew(pScreenDisplay, pFillSymbol)
With pScreenDisplay
.StartDrawing pScreenDisplay.hdc, esriNoScreenCache
.SetSymbol pFillSymbol
.DrawPolygon pPolygon
.FinishDrawing
End With
End Sub