新建一工程,窗口里放一个frame和一个picturebox,系统默认名称,将picture1.enabled=false,拷贝下列代码,会发现可以方便移动picture1(如果设置picture1.enabled=true,直接使用picture1的mousemove事件移动,则图片框会颤抖,移动效果不理想)。
'**************************************拷贝下列代码,直接运行即可**********************************************
Dim oldX As Single, oldY As Single
Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 And X > Picture1.Left And X < Picture1.Left + Picture1.Width And Y > Picture1.Top And Y < Picture1.Top + Picture1.Height Then
'如果鼠标左键按下并且鼠标处于picture1坐标范围则记录初始坐标
oldX = X
oldY = Y
Else
oldX = -1
oldY = -9999
End If
End Sub
Private Sub Frame1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If oldX = -1 And oldY = -9999 Then Exit Sub
If Button = 1 Then
Picture1.Left = Picture1.Left + X - oldX
Picture1.Top = Picture1.Top + Y - oldY
oldX = X
oldY = Y
End If
End Sub