把两个Label的DragMode属性设为 AutoMatic。Private Sub Label1_DragDrop(Source As Control, X As Single, Y As Single)
Dim t As String
If Source Is Label2 Then t = Label1.Caption Label1.Caption = Label2.Caption Label2.Caption = t End IfEnd SubPrivate Sub Label2_DragDrop(Source As Control, X As Single, Y As Single)
Dim t As String
If Source Is Label1 Then t = Label2.Caption Label2.Caption = Label1.Caption Label1.Caption = t End If
End Sub
Label控件的属性的DragMode设为手动 Option Explicit Dim cx As Long, cy As Long Dim a(1) As String Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Source.Move X - cx, Y - cy End SubPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.Drag 1 cx = X: cy = Y a(0) = Label1.Caption a(1) = Label2.Caption Timer1.Enabled = True End SubPrivate Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Label1.Drag 2 End SubPrivate Sub Label2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Label2.Drag 1 cx = X: cy = Y a(0) = Label1.Caption a(1) = Label2.Caption Timer1.Enabled = True End SubPrivate Sub Label2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) Label2.Drag 2 End SubPrivate Sub Timer1_Timer() If Abs(Label1.Top - Label2.Top) < Label1.Height And Abs(Label1.Width - Label2.Width) < Label1.Width Then Label2.Caption = a(0) Label1.Caption = a(1) Timer1.Enabled = False End If End Sub
Dim t As String
If Source Is Label2 Then
t = Label1.Caption
Label1.Caption = Label2.Caption
Label2.Caption = t
End IfEnd SubPrivate Sub Label2_DragDrop(Source As Control, X As Single, Y As Single)
Dim t As String
If Source Is Label1 Then
t = Label2.Caption
Label2.Caption = Label1.Caption
Label1.Caption = t
End If
End Sub
Option Explicit
Dim cx As Long, cy As Long
Dim a(1) As String
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.Move X - cx, Y - cy
End SubPrivate Sub Label1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Drag 1
cx = X: cy = Y
a(0) = Label1.Caption
a(1) = Label2.Caption
Timer1.Enabled = True
End SubPrivate Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label1.Drag 2
End SubPrivate Sub Label2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label2.Drag 1
cx = X: cy = Y
a(0) = Label1.Caption
a(1) = Label2.Caption
Timer1.Enabled = True
End SubPrivate Sub Label2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Label2.Drag 2
End SubPrivate Sub Timer1_Timer()
If Abs(Label1.Top - Label2.Top) < Label1.Height And Abs(Label1.Width - Label2.Width) < Label1.Width Then
Label2.Caption = a(0)
Label1.Caption = a(1)
Timer1.Enabled = False
End If
End Sub