Ecco del codice che ho scritto che ti verrà certamente in aiuto:
codice:
Private m_movingControl As Control
Private vShift As Single
Private hShift As Single
Private Property Set movingControl(vNewValue As Control)
Set m_movingControl = vNewValue
If vNewValue Is Nothing Then
Screen.MousePointer = vbDefault
Else
Screen.MousePointer = vbSizeAll
End If
End Property
Private Property Get movingControl() As Control
Set movingControl = m_movingControl
End Property
Private Sub HandleMouseDown(Sender As Object, ByVal X As Single, ByVal Y As Single, Optional Moveable As Boolean = True)
If movingControl Is Nothing And Moveable Then
vShift = Y
hShift = X
Set movingControl = Sender
Else
If TypeOf Sender Is Control Then
X = Sender.Left + X
Y = Sender.Top + Y
ElseIf TypeOf Sender Is Form Then
Else
Err.Raise 5
End If
If Not (movingControl Is Nothing) Then
movingControl.Move X - hShift, Y - vShift
Set movingControl = Nothing
End If
End If
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
HandleMouseDown Me, X, Y, False
End Sub
Aggiungendo tale codice all'inizio del tuo form puoi poi utilizzarlo per gestire gli spostamenti dei vari controlli; per far sì che un controllo possa essere spostato aggiungi al suo evento MouseDown il seguente codice:
codice:
HandleMouseDown <nomecontrollo>, X, Y
dove <nomecontrollo> è il nome di tale controllo; per far sì che su un controllo se ne possa spostare un altro aggiungi il seguente codice al suo evento MouseDown:
codice:
HandleMouseDown <nomecontrollo>, X, Y, False
.
Di seguito un esempio completo, in cui vi sono due PictureBox (PictureBox1 e PictureBox2) che possono essere spostate in giro per il form:
codice:
Option Explicit
Private m_movingControl As Control
Private vShift As Single
Private hShift As Single
Private Property Set movingControl(vNewValue As Control)
Set m_movingControl = vNewValue
If vNewValue Is Nothing Then
Screen.MousePointer = vbDefault
Else
Screen.MousePointer = vbSizeAll
End If
End Property
Private Property Get movingControl() As Control
Set movingControl = m_movingControl
End Property
Private Sub HandleMouseDown(Sender As Object, ByVal X As Single, ByVal Y As Single, Optional Moveable As Boolean = True)
If movingControl Is Nothing And Moveable Then
vShift = Y
hShift = X
Set movingControl = Sender
Else
If TypeOf Sender Is Control Then
X = Sender.Left + X
Y = Sender.Top + Y
ElseIf TypeOf Sender Is Form Then
Else
Err.Raise 5
End If
If Not (movingControl Is Nothing) Then
movingControl.Move X - hShift, Y - vShift
Set movingControl = Nothing
End If
End If
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
HandleMouseDown Me, X, Y, False
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
HandleMouseDown Picture1, X, Y
End Sub
Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
HandleMouseDown Picture2, X, Y
End Sub
.
Per spostare i controlli "abilitati" allo spostamento basta cliccarci sopra (il cursore cambierà forma) e quindi cliccare in un punto del form o di un controllo "abilitato" allo spostamento o alla ricezione di spostamenti.