How to draw a polygon on the screen

SummaryThis article shows how to draw a polygon using IRubberBand. In this example, the polygon will not persist on the screen, but will be removed after the next screen refresh.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

Drawing a polygon on the screen

The following code draws a polygon on the screen in the map where the mouse is clicked. The x,y coordinates come from a mousedown click event when the user interacts with the application.

Public Sub DrawPolygon(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView)

If activeView Is Nothing Then
End If

Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay

' Constant.
screenDisplay.StartDrawing(screenDisplay.hDC, CShort(ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache))
Dim rgbColor As ESRI.ArcGIS.Display.IRgbColor = New ESRI.ArcGIS.Display.RgbColorClass
rgbColor.Red = 255

Dim color As ESRI.ArcGIS.Display.IColor = rgbColor ' Implicit cast.
Dim simpleFillSymbol As ESRI.ArcGIS.Display.ISimpleFillSymbol = New ESRI.ArcGIS.Display.SimpleFillSymbolClass
simpleFillSymbol.Color = color

Dim symbol As ESRI.ArcGIS.Display.ISymbol = TryCast(simpleFillSymbol, ESRI.ArcGIS.Display.ISymbol) ' Dynamic cast.
Dim rubberBand As ESRI.ArcGIS.Display.IRubberBand = New ESRI.ArcGIS.Display.RubberPolygonClass
Dim geometry As ESRI.ArcGIS.Geometry.IGeometry = rubberBand.TrackNew(screenDisplay, symbol)

End Sub

public void DrawPolygon(ESRI.ArcGIS.Carto.IActiveView activeView)

 if (activeView == null)

 ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

 // Constant.
 screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast
 ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
 rgbColor.Red = 255;

 ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit cast.
 ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
 simpleFillSymbol.Color = color;

 ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic cast.
 ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberPolygonClass();
 ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);

See Also:

How to draw a point on the screen
How to draw a polyline on the screen
How to draw a rectangle on the screen