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
 
 
 
 
 
TitleLet the user interactively draw lines with arrow heads
Keywordsarrow, arrowhead, line, draw
CategoriesGraphics
 
Use an m_Drawing variable to keep track of whether the user is drawing a line. Use the MouseDown, MouseMove, and MouseUp events to show the line as the user draws it.

When the user finishes, use the DrawArrow routine to draw the line with arrow head. For information on this routine, see the HowTo Draw an arrowhead on a line.

 
Private m_Drawing As Boolean
Private m_X1 As Single
Private m_Y1 As Single
Private m_X2 As Single
Private m_Y2 As Single

Private Sub Form_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    m_Drawing = True
    m_X1 = X
    m_Y1 = Y
    m_X2 = X
    m_Y2 = Y
    DrawMode = vbInvert
    Line (m_X1, m_Y1)-(m_X2, m_Y2)
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not m_Drawing Then Exit Sub

    Line (m_X1, m_Y1)-(m_X2, m_Y2)
    m_X2 = X
    m_Y2 = Y
    Line (m_X1, m_Y1)-(m_X2, m_Y2)
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Not m_Drawing Then Exit Sub
    m_Drawing = False

    Line (m_X1, m_Y1)-(m_X2, m_Y2)
    m_X2 = X
    m_Y2 = Y

    DrawMode = vbCopyPen
    Line (m_X1, m_Y1)-(m_X2, m_Y2)

    DrawArrow m_X1, m_Y1, m_X2, m_Y2, 200
End Sub

Private Sub DrawArrow(ByVal x1 As Single, ByVal y1 As _
    Single, ByVal x2 As Single, ByVal y2 As Single, ByVal _
    arm_length As Single)
Dim dx As Single
Dim dy As Single
Dim length As Single

    dx = x2 - x1
    dy = y2 - y1
    length = Sqr(dx * dx + dy * dy)
    If length = 0 Then Exit Sub

    Line (x1, y1)-(x2, y2)
    dx = dx / length * arm_length
    dy = dy / length * arm_length
    Line (x2, y2)-Step(-dx - dy, -dy + dx)
    Line (x2, y2)-Step(-dx + dy, -dy - dx)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated