This document is archived and information here might be outdated.  Recommended version.


Draw Polygon Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Draw Polygon Snippet

Draws a polygon on the screen in the map where the mouse is clicked.

[C#]
///<summary>Draws a polygon on the screen in the ActiveView where the mouse is clicked.</summary>
///
///<param name="activeView">An IActiveView interface</param>
/// 
///<remarks>Ideally, this function would be called from within the OnMouseDown event that was created with the ArcGIS base tool template.</remarks>
public void DrawPolygon(ESRI.ArcGIS.Carto.IActiveView activeView)
{

  if (activeView == null)
  {
    return;
  }
  
  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);
  screenDisplay.SetSymbol(symbol);
  screenDisplay.DrawPolygon(geometry);
  screenDisplay.FinishDrawing();
}
[Visual Basic .NET]
'''<summary>Draws a polygon on the screen in the ActiveView where the mouse is clicked.</summary>
'''
'''<param name="activeView">An IActiveView interface</param>
''' 
'''<remarks>Ideally, this function would be called from within the OnMouseDown event that was created with the ArcGIS base tool template.</remarks>
Public Sub DrawPolygon(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView)

  If activeView Is Nothing Then
        Return
  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)
  screenDisplay.SetSymbol(symbol)
  screenDisplay.DrawPolygon(geometry)
  screenDisplay.FinishDrawing()

End Sub

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Carto
  • ESRI.ArcGIS.Display
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System