Question : How to detect mouse click on a line control?

The Access relationship diagram tool is able to recognize a mouse click on a diagonal connecting line.
I'm wondering how it does that, because the line control has no mouse event.
I suspect that rather than detect it by some (undocumented) event, it recognizes by iterating (for any non-node click) all connectors and calculating if it ought to be a hit.

The context is that I would like to achieve the same on an access 2003 form.
Any comments/experiences?

Answer : How to detect mouse click on a line control?

I was working with mouse events and lines recently. I tried this for you: a form with a bunch of (black) lines and a transparent button covering the entire detail section. The code below will highlight the "clicked" line.

It is not very pleasant without a transparent button, because the lines do actually mask the click event for the section. The line does not expose a click event, but it traps them still...

If you want to play with it, just create a form with some lines and a transparent button called cmdMouse. The number 'r' between 0 and 1 means the projection of the point occurs on the segment (and not on the line extending from it), and d is the distance to the line (not the segment).

(°v°)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
Option Explicit
 
Dim lin As Line
 
Private Sub cmdMouse_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
    Dim ctl As Control
    Dim Ax As Single, Ay As Single, Bx As Single, By As Single
    Dim r As Double, s As Double, d As Double
    
    For Each ctl In Me.Controls
    If ctl.ControlType = acLine Then
    
        With ctl
            If .LineSlant Then
                Ax = .Left: Ay = .Top + .Height
                Bx = .Left + .Width: By = .Top
            Else
                Ax = .Left: Ay = .Top
                Bx = .Left + .Width: By = .Top + .Height
            End If
        End With
    
        d = (Bx - Ax) ^ 2 + (By - Ay) ^ 2
        r = ((X - Ax) * (Bx - Ax) + (Y - Ay) * (By - Ay)) / d
        s = ((Ay - Y) * (Bx - Ax) - (Ax - X) * (By - Ay)) / d
        d = Abs(s) * d ^ (1 / 2)
        
        If d < 50 And 0 <= r And r <= 1 Then
            Set lin = ctl
            lin.BorderColor = vbRed
            Exit For
        End If
        
    End If
    Next ctl
    
End Sub
 
Private Sub cmdMouse_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Not lin Is Nothing Then
        lin.BorderColor = vbBlack
        Set lin = Nothing
    End If
End Sub
Random Solutions  
 
programming4us programming4us