How to draw a line with an arrowhead?-VBForums
Results 1 to 5 of 5

Thread: How to draw a line with an arrowhead?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    14

    Arrow

    Hi!
    Does anyone know how to draw a line in VB code with an arrowhead? I want to allow the user to draw it on the picturebox.
    Or is there a control in VB that does it?
    Thanks!

  2. #2
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186
    I don't remember there being anything like an arrow head in VB. You would have to draw your own.

    Shrog

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    14
    Do you have any codes that I can refer to?

  4. #4
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    Cool Try this recipe...

    Here is something I just whipped up. It is a crude little example, but works very well.

    The coding below responds to the MouseDown and MouseUp events of a form.

    When the mouse is down, we keep track of the coordinates of this point. When the mouse button is released, we draw a line from the starting point to the release point, and add an arrow head.

    You can create your own function that receives the start and end coordinates, and maybe the arrow size.

    Code:
    Option Explicit
    
    Private StartX As Single
    Private StartY As Single
    Private StopX As Single
    Private StopY As Single
    
    Const ArrowHeadSize = 6
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      'Get the starting point of the line
      StartX = X
      StartY = Y
    End Sub
    
    Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Dim DistX As Single
      Dim Disty As Single
      Dim Distance As Single
      Dim FactorX As Single
      Dim FactorY As Single
      Dim HeadX1 As Single
      Dim HeadY1 As Single
      Dim HeadX2 As Single
      Dim HeadY2 As Single
      Dim HeadX3 As Single
      Dim HeadY3 As Single
      Dim ArrowSize As Long
      
      'convert so it's the same size no matter what scalemode you're in
      ArrowSize = Me.ScaleX(ArrowHeadSize, vbPixels, Me.ScaleMode)
      
      'get the end of the line
      StopX = X
      StopY = Y
      
      'Do some magic
      DistX = StopX - StartX
      Disty = StopY - StartY
      Distance = Sqr(DistX * DistX + Disty * Disty)
      If Distance < 1 Then Distance = 1
      FactorX = DistX / Distance
      FactorY = Disty / Distance
      
      'Claculate the three corners of the arrow head
      HeadX3 = StopX
      HeadY3 = StopY
      StopX = HeadX3 - 2 * ArrowSize * FactorX
      StopY = HeadY3 - 2 * ArrowSize * FactorY
      HeadX1 = StopX + ArrowSize * FactorY
      HeadY1 = StopY - ArrowSize * FactorX
      HeadX2 = StopX - ArrowSize * FactorY
      HeadY2 = StopY + ArrowSize * FactorX
      
      'draw the line and arrow head
      If Distance >= 2 * ArrowSize Then
        Form1.Line (StartX, StartY)-(StopX, StopY)
      End If
      Form1.Line (HeadX1, HeadY1)-(HeadX2, HeadY2)
      Form1.Line (HeadX1, HeadY1)-(HeadX3, HeadY3)
      Form1.Line (HeadX3, HeadY3)-(HeadX2, HeadY2)
      
    End Sub
    I hope this helps.
    Shrog

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    14

    It helps a lot!

    Hello there!
    Yes, your code has helped me a lot. I'll try and combine it with the circle collision that you've posted up aldo and see how it works.
    I'll keep you updated on my progress, thank you again!

    -mybren-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.