Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleDraw rubberband lines in VB .NET using DrawReversibleLine
Keywordsrubberband, VB.NET, NET, redraw, AutoRedraw, DrawReversibleLine
CategoriesGraphics
 
In the MouseDown event handler, use Control.MousePosition to save the mouse's position in screen coordinates. In the MouseMove event handler, use Control.MousePosition again to find the mouse's position. Use DrawReversibleLine to erase the previous line and draw the new one.

In the MouseUp event handler, draw the final line over the most recent reversible line. If you don't want to draw over the line, use DrawReversibleLine one last time to erase the most recent line.

 
' True while we are drawing the new line.
Private m_Drawing As Boolean

' Buffer for erasing rubberband lines.
Private m_BufferBitmap As Bitmap
Private m_BufferGraphics As Graphics

' The mouse position.
Private m_Pos1 As Point
Private m_Pos2 As Point

' Start drawing a reversible line.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    MyBase.MouseDown
    ' Do nothing if this isn't the left mouse button.
    If e.Button <> MouseButtons.Left Then Exit Sub
    m_Drawing = True

    ' Save the current mouse position.
    m_Pos1 = Control.MousePosition
    m_Pos2 = m_Pos1
End Sub

' Continue drawing the reversible line.
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    MyBase.MouseMove
    ' Do nothing if we're not drawing.
    If Not m_Drawing Then Exit Sub

    ' Erase the previous line.
    ControlPaint.DrawReversibleLine( _
        m_Pos1, m_Pos2, Me.BackColor)

    ' Save the new point.
    m_Pos2 = Control.MousePosition

    ' Draw the new line.
    ControlPaint.DrawReversibleLine( _
        m_Pos1, m_Pos2, Me.BackColor)
End Sub

' Finish drawing the new line.
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.MouseEventArgs) Handles _
    MyBase.MouseUp
    ' Do nothing if we're not drawing.
    If Not m_Drawing Then Exit Sub
    m_Drawing = False

    ' Draw the new line permanently on the buffer.
    ' Note that the points used by DrawReversibleLine
    ' are in screen coordinates so we need to use
    ' PointToClient to convert that to form coordinates.
    m_BufferGraphics.DrawLine(Pens.Blue, _
        Me.PointToClient(m_Pos1), _
        Me.PointToClient(m_Pos2))

    ' Redraw to show the new line.
    DrawForm(Me.CreateGraphics())
End Sub

' Redraw the saved buffer.
Private Sub DrawForm(ByVal gr As Graphics)
    If Not (m_BufferBitmap Is Nothing) Then _
        gr.DrawImage(m_BufferBitmap, 0, 0)
End Sub

' Redraw the form.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    DrawForm(e.Graphics)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make a new bitmap that fits the form.
    m_BufferBitmap = New Bitmap(Me.Size.Width, _
        Me.Size.Height, Me.CreateGraphics())
    m_BufferGraphics = Graphics.FromImage(m_BufferBitmap)

    ' Clear the new bitmap.
    m_BufferGraphics.Clear(Me.BackColor)
End Sub
 
Note that DrawReversibleLine draws in screen coordinates while controls forms use their own coordinate systems.

Note also that DrawReversibleLine draws on the screen not just the form. That means you can use it to draw outside of the form. It also means it will draw on top of other forms if they sit above your form. For example, if a topmost window ovelaps yours, the reversible lines will draw over it. If you do not use DrawReversibleLine to erase the line, it will remain on the topmost window.

Thanks to C.A. "Bear" Smith for suggesting using DrawReversibleLine and DrawReversibleFrame.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated