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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| Option Explicit
'Généralisation des dimensions et nombre de labels
Const Nc As Integer = 40 'nombre de labels en colonnes
Const Nl As Integer = 23 'nombre de labels en lignes
Const L As Integer = 15 'largeur des labels
Const H As Integer = 20 'hauteur des labels
Const Ec As Integer = 1 'Ecart horizontal entre labels
Const El As Integer = 1 'Ecart vertical entre labels
Dim TheTop()
Dim TheLeft()
Dim Couleur() As Long
Private Sub UserForm_Initialize()
Dim i As Integer, j As Integer, k As Integer
Dim Lbl As Control
'Règlage de la taille et la position du contrôle image
With Me.Image1
.Left = 0
.Top = 0
.Width = Nc * (L + Ec) + 2 * Ec
.Height = Nl * (H + El) + 2 * El
.ZOrder 0
.BackStyle = 0
.BorderStyle = 0
End With
'Ajout des labels dans l'userform et dans le tableau TB
ReDim TheTop(1 To Nl)
ReDim TheLeft(1 To Nc)
ReDim Couleur(1 To Nc * Nl)
For i = 1 To Nl
For j = 1 To Nc
k = Nc * (i - 1) + j
Set Lbl = Me.Controls.Add("Forms.Label.1", "LB" & k, True)
With Lbl
.Left = Ec + ((L + Ec) * (j - 1))
.Top = El + ((H + El) * (i - 1))
.Width = L
.Height = H
.Caption = "LB" & k
.TextAlign = 2
.BorderStyle = 1
.ZOrder 1
.BackColor = 225
If i = 1 Then TheLeft(j) = Array(.Left, .Left + L + Ec)
Couleur(k) = .BackColor
End With
Next j
TheTop(i) = Array(Lbl.Top, Lbl.Top + H + El)
Next i
End Sub
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Static Old As Integer
Dim Tmp As Integer
If Old <> 0 Then Me("LB" & Old).BackColor = Couleur(Old)
Tmp = FindLabelIndex(TheLeft, TheTop, X, Y)
Me.Caption = "Label survolé: LB" & Tmp
If Tmp <> 0 Then
Old = Tmp
Me("LB" & Tmp).BackColor = vbWhite
End If
End Sub
Private Function FindLabelIndex(TbLeft(), TbTop(), ByVal xX As Single, ByVal yY As Single) As Integer
Dim i As Integer, TheLig As Integer, TheCol As Integer
For i = 1 To UBound(TheLeft)
If xX > TbLeft(i)(0) And xX < TbLeft(i)(1) Then
TheCol = i
Exit For
End If
Next i
For i = 1 To UBound(TbTop)
If yY > TbTop(i)(0) And yY < TbTop(i)(1) Then
TheLig = i
Exit For
End If
Next i
If TheLig * TheCol > 0 Then FindLabelIndex = Nc * (TheLig - 1) + TheCol
End Function |