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
|
Option Explicit
' Position lors du clic
Private gClicX As Single, gClicY As Single
' Etiquette qui génère les événement
Private WithEvents goLabel As MSForms.Label
' Formulaire contenant le label et les procédures DragMouseDown / DragMouseMove / DragMouseUp
Private goUserForm As Object
' Propriété pour définir l'étiquette
Public Property Let Label(pLabel As MSForms.Label)
Set goLabel = pLabel
End Property
' Propriété pour définir le formulaire
Public Property Let UserForm(pUserForm As Object)
Set goUserForm = pUserForm
End Property
' Sur souris appuyée
Private Sub goLabel_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' Si bouton gauche
If Button = vbKeyLButton Then
' Conserve la position du clic
gClicX = X
gClicY = Y
' Appelle la procédure DragMouseDown du formulaire
CallByName goUserForm, "DragMouseDown", VbMethod, goLabel
End If
End Sub
' Sur souris déplacée
Private Sub goLabel_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' Si bouton gauche
If Button = vbKeyLButton Then
' Déplace l'étiquette en fonction de sa position courante, de la position du clic et de la position
' de la souris déplacée sur l'étiquette
goLabel.Left = goLabel.Left - (gClicX - X)
goLabel.Top = goLabel.Top - (gClicY - Y)
' Appelle la procédure DragMouseMove du formulaire
CallByName goUserForm, "DragMouseMove", VbMethod, goLabel
End If
End Sub
' Sur souris relâchée
Private Sub goLabel_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
' Si bouton gauche
If Button = vbKeyLButton Then
' Appelle la procédure DragMouseUp du formulaire
CallByName goUserForm, "DragMouseUp", VbMethod, goLabel
End If
End Sub |
Partager