How to draw a digitized line onscreen


This sample uses a rubber banding line to obtain a digitized line geometry. With the geometry created, a symbol is created. The symbol is set as the current display symbol and the line is drawn. The color thickness and the style of the line symbol are set.
The code samples in this section show the fundamentals of programming with ArcObjects. A careful reading of them gives you all the important concepts you need for developing with ArcObjects, as well as an introduction to the most important ArcObjects components.
The code can be typed or copied into the VBA environment in ArcMap or ArcCatalog, after which you can follow through with the VBA debugger.

How to use

  1. Add the code to the MouseDown event of a tool in ArcMap.
[VBA]
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pScreen As IScreenDisplay
Set pScreen = pMxDoc.ActiveView.ScreenDisplay

Dim pPolyline As IPolyline
Dim pRubber As IRubberBand
Set pRubber = New RubberLine
Set pPolyline = pRubber.TrackNew(pScreen, Nothing)

Dim pRGBColor As IRgbColor
Set pRGBColor = New RgbColor
With pRGBColor
    .Red = 255
    .Green = 128
    .Blue = 128
End With

Dim pLineSymbol As ISimpleLineSymbol
Set pLineSymbol = New SimpleLineSymbol
With pLineSymbol
    .Width = 2
    .Color = pRGBColor
    .Style = esriSLSSolid
End With

With pScreen
    .StartDrawing pScreen.hDC, esriNoScreenCache
    .SetSymbol pLineSymbol
    .DrawPolyline pPolyline
    .FinishDrawing
End With