Dim PrevX As Long, PrevY As Long
Dim PrevX2 As Long, PrevY2 As LongPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PrevX2 = X: PrevY2 = Y
End SubPrivate Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Command1.Move Command1.Left + X - PrevX2, Command1.Top + Y - PrevY2
End If
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PrevX = X: PrevY = Y
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Text1.Move Text1.Left + X - PrevX, Text1.Top + Y - PrevY
End If
End Sub在窗口中有一个text1控件和一个command1控件,上面的代码可以实现这两个控件的移动,怎样让它们两个控件不可以重叠在一起呢?
Dim PrevX2 As Long, PrevY2 As LongPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PrevX2 = X: PrevY2 = Y
End SubPrivate Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Command1.Move Command1.Left + X - PrevX2, Command1.Top + Y - PrevY2
End If
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PrevX = X: PrevY = Y
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Text1.Move Text1.Left + X - PrevX, Text1.Top + Y - PrevY
End If
End Sub在窗口中有一个text1控件和一个command1控件,上面的代码可以实现这两个控件的移动,怎样让它们两个控件不可以重叠在一起呢?
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Declare Function IntersectRect Lib "user32" (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As LongDim PrevX As Long, PrevY As LongPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Object_MouseDown Button, Shift, X, Y
End SubPrivate Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Object_MouseMove Command1, Button, Shift, X, Y
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Object_MouseDown Button, Shift, X, Y
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Object_MouseMove Text1, Button, Shift, X, Y
End SubPrivate Sub Object_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
PrevX = X
PrevY = Y
End If
End SubPrivate Sub Object_MouseMove(AObject As Control, Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim FormObject As Control
Dim R1 As RECT, R2 As RECT
Dim CanMove As Boolean
If Button = vbLeftButton Then
With R1
.Left = AObject.Left + X - PrevX
.Top = AObject.Top + Y - PrevY
.Right = .Left + AObject.Width
.Bottom = .Top + AObject.Height
End With
CanMove = True
For Each FormObject In Me
If FormObject.Name <> AObject.Name Then
With R2
.Left = FormObject.Left
.Top = FormObject.Top
.Right = .Left + FormObject.Width
.Bottom = .Top + FormObject.Height
End With
IntersectRect R2, R1, R2
If R2.Left + R2.Top + R2.Right + R2.Bottom <> 0 Then
CanMove = False
Exit For
End If
End If
Next
If CanMove Then AObject.Move R1.Left, R1.Top
End If
End Sub