Results 1 to 11 of 11

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

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    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.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    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?

  4. #4
    Member
    Join Date
    Apr 2008
    Location
    Palm Springs, FL
    Posts
    39

    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.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

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

    Better yet, see if you can find a more flexible, third party, line control.

  6. #6
    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.

  7. #7
    Member
    Join Date
    Apr 2008
    Location
    Palm Springs, FL
    Posts
    39

    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

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

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

    Thank you Renovator

    Let's hope that Tontow likes it too

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2006
    Posts
    172

    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.

  10. #10
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    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)

  11. #11
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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
  •  



Click Here to Expand Forum to Full Width