|
-
Apr 22nd, 2008, 11:50 AM
#1
Thread Starter
Addicted Member
VB6.0: Adding Events to Line Controls?
What is the best way to add events to the Line control? Or is there another vershion of the Line control that already has events added to it?
I need to be able to use the Click, Mouse Move, and possably a Mouse Over events.
-
Apr 22nd, 2008, 12:33 PM
#2
Re: VB6.0: Adding Events to Line Controls?
If you only need horizontal or vertical lines then you can use Label control instead:
Code:
Private Sub Form_Load()
'horizontal "line"
Label1.Height = 15
Label1.BorderStyle = 1 'show border
Label1.Appearance = 0 'flat
Label1.Width = 3000 'or whatever
Label1.Move 1000, 3000 'or anywhere you wish
End Sub
-
Apr 22nd, 2008, 01:24 PM
#3
Thread Starter
Addicted Member
Re: VB6.0: Adding Events to Line Controls?
It would be nice if I could add events to lines that are at an angle also. Any Ideas on how I would do that?
-
Apr 23rd, 2008, 06:53 AM
#4
Member
Re: VB6.0: Adding Events to Line Controls?
Yes.
If you make your lines a unique color you can add pget() to mouseover event of Form to test for that color.
-
Apr 23rd, 2008, 08:49 AM
#5
Re: VB6.0: Adding Events to Line Controls?
Better yet, see if you can find a more flexible, third party, line control.
-
Apr 23rd, 2008, 11:57 AM
#6
Re: VB6.0: Adding Events to Line Controls?
 Originally Posted by Tontow
It would be nice if I could add events to lines that are at an angle also. Any Ideas on how I would do that?
You can do that using the "Point to Line Distance"
Using this as a reference: [Edit]Link does not point to the same thing as before...
I wrote this code:
Just put a line on the form at any angle, leave default name "Line1", and copy & paste this code.
The line will change color to red when the mouse is over it.
Code:
Option Explicit
Private Type POINT
X As Double
Y As Double
End Type
Private Const LineProximity As Double = 2 ' In Pixels
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim P1 As POINT, P2 As POINT, P3 As POINT
Dim U As Double
Dim LineMagnitude As Double
Dim Intersection As POINT
P1.X = Line1.X1
P1.Y = Line1.Y1
P2.X = Line1.X2
P2.Y = Line1.Y2
P3.X = X
P3.Y = Y
LineMagnitude = Distance(P2, P1)
U = ((P3.X - P1.X) * (P2.X - P1.X) + (P3.Y - P1.Y) * (P2.Y - P1.Y)) / (LineMagnitude * LineMagnitude)
If U > 0 And U < 1 Then
Intersection.X = P1.X + U * (P2.X - P1.X)
Intersection.Y = P1.Y + U * (P2.Y - P1.Y)
Me.Caption = Format(U, "0.000") & ", " & Format(Distance(P3, Intersection), "0.000")
If Distance(P3, Intersection) < Me.ScaleX(LineProximity, vbPixels, Me.ScaleMode) Then
Line1.BorderColor = vbRed ' mouse over the line
Else
Line1.BorderColor = vbBlack ' mouse away from the line
End If
Else
Me.Caption = ""
End If
End Sub
Private Function Distance(P1 As POINT, P2 As POINT) As Double
Dim P As POINT
P.X = P2.X - P1.X
P.Y = P2.Y - P1.Y
Distance = Sqr(P.X * P.X + P.Y * P.Y)
End Function
Last edited by CVMichael; Jan 1st, 2009 at 07:25 PM.
-
Apr 23rd, 2008, 11:16 PM
#7
Member
Re: VB6.0: Adding Events to Line Controls?
Forget what I said about pget(). CVMichael's solution not only works perfectly, but can be expanded to distinguish individual lines. Got to thank you CVM, this wasn't my question, but your answer is so good it's absolutely a keeper - Renovator
-
Apr 23rd, 2008, 11:34 PM
#8
Re: VB6.0: Adding Events to Line Controls?
Thank you Renovator
Let's hope that Tontow likes it too
-
Apr 23rd, 2008, 11:39 PM
#9
Thread Starter
Addicted Member
Re: VB6.0: Adding Events to Line Controls?
Yes, I do like it. Thankyou.
And it dosen't look like it will be too hard to change to deal with control arrays.
-
Apr 24th, 2008, 12:02 AM
#10
Re: VB6.0: Adding Events to Line Controls?
Just an optimization point on CV's code.
Code:
LineMagnitude = Distance(P2, P1) = Sqr(...)
U = (...) / (LineMagnitude * LineMagnitude)
Finding the square root of a number and squaring the result is an obvious was of time. I would put the distance calculation inline and skip the unnecessary math.
Code:
Dim dX As Double
Dim dY As Double
' ...
dX = P2.X - P1.X
dY = P2.Y - P1.Y
U = ((P3.X - P1.X) * dX + (P3.Y - P1.Y) * dY) / (dX * dX + dY * dY)
-
Apr 24th, 2008, 03:07 AM
#11
Re: VB6.0: Adding Events to Line Controls?
Another little tweak to add, there might be a situation where P2 and P1 are the same so you should check that (dX * dX + dY * dY) is none zero before dividing with it.
edit: Also it's quicker to square numbers than to square root them so when testing an array of lines you could compare the final square distance to line proximity squared, thereby taking all square root operations out of the procedure.
Last edited by Milk; Apr 24th, 2008 at 03:14 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|