Results 1 to 11 of 11

Thread: VB6.0: Adding Events to Line Controls?

Threaded View

  1. #4
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: VB6.0: Adding Events to Line Controls?

    Quote 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.

Posting Permissions

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



Click Here to Expand Forum to Full Width