Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Picture2.Left = X Picture2.Top = Y End If End Sub
//2、如何处理当picture2的任何一边与picture1重叠问题, 什么意思?
Picture2.Left = X + 一个你要附近的差距 Picture2.Top = Y + 一个你要附近的差距
'这样行吗 Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then If X + Picture2.ScaleWidth > Picture1.ScaleLeft + Picture1.ScaleWidth Then X = Picture1.ScaleLeft + Picture1.ScaleWidth - Picture2.ScaleWidth - 100 End If If Y + Picture2.ScaleHeight > Picture1.ScaleTop + Picture1.ScaleHeight Then Y = Picture1.ScaleTop + Picture1.ScaleHeight - Picture2.ScaleHeight - 100 End If Picture2.Left = X Picture2.Top = Y End If End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then If X + Picture2.ScaleWidth > Picture1.ScaleLeft + Picture1.ScaleWidth Then X = Picture1.ScaleLeft + Picture1.ScaleWidth - Picture2.Width End If If Y + Picture2.ScaleHeight > Picture1.ScaleTop + Picture1.ScaleHeight Then Y = Picture1.ScaleTop + Picture1.ScaleHeight - Picture2.Height End If Picture2.Left = X Picture2.Top = Y End If End Sub
如果你要以鼠标座标作为 Picture2 的中心点: Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim newX As Single, newY As Single newX = X - Picture2.ScaleWidth / 2 newY = Y - Picture2.ScaleHeight / 2If newX < Picture1.ScaleLeft Then newX = Picture1.ScaleLeft If newX + Picture2.ScaleWidth > Picture1.ScaleWidth Then newX = Picture1.ScaleWidth - Picture2.ScaleWidthIf newY < Picture1.ScaleTop Then newY = Picture1.ScaleTop If newY + Picture2.ScaleHeight > Picture1.ScaleHeight Then newX = Picture1.ScaleHeight - Picture2.ScaleHeightPicture2.Move newX, newYEnd Sub
我的代码最短 你看这样行不: private sub picture1_MouseDown(Button as integer,Shift as integer,x as single,y as single) picture2.left=x: picture2.top=y if picture2.left<=0 then '使左边不重叠 picture2.left=30 else if picture2.left>= picture1.width-picture2.width then'使右边不重叠 picture1.width-picture2.width-30 endif if picture2.top<=0 then '使上边不重叠 picture2.top=30 else if picture2.top>=picture1.height-picture2.height then'使下边不重叠 picture2.top=picture1.height-picture2.height -30 endif 试试看行不行
If Button = 1 Then
Picture2.Left = X
Picture2.Top = Y
End If
End Sub
什么意思?
Picture2.Top = Y + 一个你要附近的差距
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
If X + Picture2.ScaleWidth > Picture1.ScaleLeft + Picture1.ScaleWidth Then
X = Picture1.ScaleLeft + Picture1.ScaleWidth - Picture2.ScaleWidth - 100
End If
If Y + Picture2.ScaleHeight > Picture1.ScaleTop + Picture1.ScaleHeight Then
Y = Picture1.ScaleTop + Picture1.ScaleHeight - Picture2.ScaleHeight - 100
End If
Picture2.Left = X
Picture2.Top = Y
End If
End Sub
If Button = 1 Then
If X + Picture2.ScaleWidth > Picture1.ScaleLeft + Picture1.ScaleWidth Then
X = Picture1.ScaleLeft + Picture1.ScaleWidth - Picture2.Width
End If
If Y + Picture2.ScaleHeight > Picture1.ScaleTop + Picture1.ScaleHeight Then
Y = Picture1.ScaleTop + Picture1.ScaleHeight - Picture2.Height
End If
Picture2.Left = X
Picture2.Top = Y
End If
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim newX As Single, newY As Single
newX = X - Picture2.ScaleWidth / 2
newY = Y - Picture2.ScaleHeight / 2If newX < Picture1.ScaleLeft Then newX = Picture1.ScaleLeft
If newX + Picture2.ScaleWidth > Picture1.ScaleWidth Then newX = Picture1.ScaleWidth - Picture2.ScaleWidthIf newY < Picture1.ScaleTop Then newY = Picture1.ScaleTop
If newY + Picture2.ScaleHeight > Picture1.ScaleHeight Then newX = Picture1.ScaleHeight - Picture2.ScaleHeightPicture2.Move newX, newYEnd Sub
private sub picture1_MouseDown(Button as integer,Shift as integer,x as single,y as single)
picture2.left=x: picture2.top=y
if picture2.left<=0 then '使左边不重叠
picture2.left=30
else if picture2.left>= picture1.width-picture2.width then'使右边不重叠
picture1.width-picture2.width-30
endif
if picture2.top<=0 then '使上边不重叠
picture2.top=30
else if picture2.top>=picture1.height-picture2.height then'使下边不重叠
picture2.top=picture1.height-picture2.height -30
endif
试试看行不行
baoaya(点头) 的可以
of123() 有点问题,你在pic1的底部点击的话,那么pic2的一部份会被遮住
wenquan836(电脑狂徒) 的可以
结贴了