How to draw a polyline on the screen


Summary
This topic shows how to draw a polyline using IRubberBand.

Drawing a polyline on the screen

The following code draws a polyline 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 polyline is not persisted on the screen but is removed after the next screen refresh.
[VB.NET]
Public Sub DrawPolyline(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 simpleLineSymbol As ESRI.ArcGIS.Display.ISimpleLineSymbol = New ESRI.ArcGIS.Display.SimpleLineSymbolClass
    simpleLineSymbol.Color = color
    
    Dim symbol As ESRI.ArcGIS.Display.ISymbol = CType(simpleLineSymbol, ESRI.ArcGIS.Display.ISymbol) ' Explicit cast.
    Dim rubberBand As ESRI.ArcGIS.Display.IRubberBand = New ESRI.ArcGIS.Display.RubberLineClass
    Dim geometry As ESRI.ArcGIS.Geometry.IGeometry = rubberBand.TrackNew(screenDisplay, symbol)
    screenDisplay.SetSymbol(symbol)
    screenDisplay.DrawPolyline(geometry)
    screenDisplay.FinishDrawing()
    
End Sub
[C#]
public void DrawPolyline(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.ISimpleLineSymbol simpleLineSymbol = new
        ESRI.ArcGIS.Display.SimpleLineSymbolClass();
    simpleLineSymbol.Color = color;

    ESRI.ArcGIS.Display.ISymbol symbol = (ESRI.ArcGIS.Display.ISymbol)
        simpleLineSymbol; // Explicit cast.
    ESRI.ArcGIS.Display.IRubberBand rubberBand = new
        ESRI.ArcGIS.Display.RubberLineClass();
    ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay,
        symbol);
    screenDisplay.SetSymbol(symbol);
    screenDisplay.DrawPolyline(geometry);
    screenDisplay.FinishDrawing();
}


See Also:

How to draw a point on the screen
How to draw a polygon on the screen
How to draw a rectangle 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