Nov 20th, 2000, 08:08 AM
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?
Nov 20th, 2000, 08:33 AM
I don't remember there being anything like an arrow head in VB. You would have to draw your own.
Nov 20th, 2000, 09:47 AM
Do you have any codes that I can refer to?
Nov 20th, 2000, 10:06 AM
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.
I hope this helps.
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
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)
Form1.Line (HeadX1, HeadY1)-(HeadX2, HeadY2)
Form1.Line (HeadX1, HeadY1)-(HeadX3, HeadY3)
Form1.Line (HeadX3, HeadY3)-(HeadX2, HeadY2)
Nov 22nd, 2000, 07:53 AM
It helps a lot!
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!
Click Here to Expand Forum to Full Width
Survey posted by VBForums.