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

How to draw a rectangle on the screen (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with the map display > Working with the map display > How to draw a rectangle on the screen

How to draw a rectangle on the screen

This topic shows how to draw a polygon using IRubberBand.

Drawing a rectangle on the screen

The following code draws a rectangle on the screen in the ActiveView where the mouse is clicked. The x,y coordinates come from a mousedown click event when the user interacts with the application. In this example, the envelope is not persisted on the screen but is removed after the next screen refresh.
Public Sub DrawRectangle(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
    Dim color As ESRI.ArcGIS.Display.IColor=rgbColor ' Implicit cast.
    Dim simpleFillSymbol As ESRI.ArcGIS.Display.ISimpleFillSymbol=New ESRI.ArcGIS.Display.SimpleFillSymbolClass
    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.RubberEnvelopeClass
    Dim geometry As ESRI.ArcGIS.Geometry.IGeometry=rubberBand.TrackNew(screenDisplay, symbol)
    screenDisplay.DrawRectangle(TryCast(geometry, ESRI.ArcGIS.Geometry.IEnvelope)) ' Dynamic cast.
End Sub
public void DrawRectangle(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();

    ESRI.ArcGIS.Display.IColor color=rgbColor; // Implicit cast.
    ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol=new

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

See Also:

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

Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine