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
 
 
 
 
 
TitleMake a labeled line "symbiote" to help users draw rubberband lines in VB .NET
DescriptionThis example shows how to make a line "symbiote" to help users draw rubberband lines in Visual Basic .NET.
Keywordslabeled line symbiote, rubberband line, draw, VB.NET
CategoriesGraphics, VB.NET
 
This example uses a control that I call a symbiote. It works in conjunction with a PictureBox to let the user draw a rubberband line. It also displays a label along the line. It raises events to let the main program set the label's text. In this example, the program sets the label to show the line's current length as the user sizes it.

See the example Make a line "symbiote" to help users draw rubberband lines in VB .NET for the basics.

This version adds a couple new twists. First, it has a Caption property that determines the text displayed on the line.

 
' The text we draw.
Private m_Caption As String
Public Property Caption() As String
    Get
        Return m_Caption
    End Get
    Set(ByVal Value As String)
        m_Caption = Value
    End Set
End Property
 
Second, the MouseDown MouseMove, and MouseUp event handlers that deal with drawing the lines do not draw the lines directly. Instead they call subroutine DrawLine. MouseMove raises the LineMoved event to give the main program a chance to change the caption before drawing the new line. MouseUp raises the LineSelected event.
 
Public Event Redraw()
Public Event LineMoved(ByVal x0 As Single, ByVal y0 As _
    Single, ByVal x1 As Single, ByVal y1 As Single)
Public Event LineSelected(ByVal x0 As Single, ByVal y0 As _
    Single, ByVal x1 As Single, ByVal y1 As Single)

' Start drawing.
Private Sub m_Canvas_MouseDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    m_Canvas.MouseDown
    If e.Button <> MouseButtons.Left Then Exit Sub
    m_Drawing = True

    ' Save the point.
    m_X0 = e.X
    m_Y0 = e.Y
    m_X1 = e.X
    m_Y1 = e.Y

    ' Draw the line.
    DrawLine(m_X0, m_Y0, m_X1, m_Y1)
End Sub

' Continue drawing.
Private Sub m_Canvas_MouseMove(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
    m_Canvas.MouseMove
    If Not m_Drawing Then Exit Sub

    ' Let the program change the symbiote's caption.
    RaiseEvent LineMoved(m_X0, m_Y0, e.X, e.Y)

    ' Erase the previous line.
    RaiseEvent Redraw()

    ' Save the new point.
    m_X1 = e.X
    m_Y1 = e.Y

    ' Draw the line.
    DrawLine(m_X0, m_Y0, m_X1, m_Y1)
End Sub

' Finish drawing.
Private Sub m_Canvas_MouseUp(ByVal sender As Object, ByVal _
    e As System.Windows.Forms.MouseEventArgs) Handles _
    m_Canvas.MouseUp
    If Not m_Drawing Then Exit Sub
    m_Drawing = False

    ' Raise the LineSelected event. 
    ' The main program can refresh the canvas if desired.
    RaiseEvent LineSelected(m_X0, m_Y0, m_X1, m_Y1)
End Sub
 
Finally, subroutine DrawLine draws the line with its caption. It gets the line's angle. It then uses transformations to rotate text so it lies in the direction of the line and to translate the origin to the middle of the line. It then draws the caption centered at the origin and the transformation puts it in the middle of the line as desired.
 
' Draw the line with its caption.
Private Sub DrawLine(ByVal x0 As Single, ByVal y0 As _
    Single, ByVal x1 As Single, ByVal y1 As Single)
    ' Make the Graphics object.
    Dim gr As Graphics = m_Canvas.CreateGraphics()

    ' Draw the line.
    gr.DrawLine(Pens.Black, x0, y0, x1, y1)

    ' Get the line's angle.
    Dim angle As Double = Math.Atan2(y1 - y0, x1 - x0)

    ' Rotate and translate to put the origin in the middle
    ' of the line
    ' rotated at the proper angle.
    Dim cx As Double = (x0 + x1) / 2
    Dim cy As Double = (y0 + y1) / 2
    gr.RotateTransform(angle * 180 / Math.PI, _
        Drawing2D.MatrixOrder.Append)
    gr.TranslateTransform(cx, cy, _
        Drawing2D.MatrixOrder.Append)

    ' Clear a rectangle around the text.
    Dim br As New SolidBrush(m_Canvas.BackColor)
    Dim text_size As SizeF = gr.MeasureString(m_Caption, _
        m_Canvas.Font)
    gr.FillRectangle(br, _
        -text_size.Width / 2, -text_size.Height / 2, _
        text_size.Width, text_size.Height)
    br.Dispose()

    ' Draw the text, centered at the origin.
    Dim sf As New StringFormat
    sf.Alignment = StringAlignment.Center
    sf.LineAlignment = StringAlignment.Center
    gr.DrawString(m_Caption, m_Canvas.Font, Brushes.Black, _
        0, 0, sf)
    sf.Dispose()
End Sub
 
For information on building controls in Visual Basic .NET (and tons of other topics), see my book Visual Basic 2005 Programmer's References.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated