这是我的代码:
Dim Offset As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
Offset = New Point(-e.X, -e.Y)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim Pos As Point = Me.PointToClient(MousePosition)
Pos.Offset(Offset.X, Offset.Y)
PictureBox1.Location = Pos
End If
End Sub
我能够做到用户想要的东西,(拖动图片和添加按钮)虽然..
问题1
每当我将pictureBox拖到面板内部时,无论我带到哪里,它都会稍微向右偏移。从技术上讲,当我移动鼠标时,图片框只是在跟随鼠标序列之前向右移动,我知道它只是次要的,但它是一个我无法让用户看到它的错误。
希望你们能帮忙!谢谢!
答案 0 :(得分:1)
您需要存储图片框的原始客户位置(鼠标按下)并在此点上进行偏移:
Private offset As Point
Private pbpos As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
Me.pbpos = Me.PictureBox1.Location
Me.offset = Control.MousePosition
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = Windows.Forms.MouseButtons.Left) Then
Me.PictureBox1.Location = New Point((Me.pbpos.X + (Control.MousePosition.X - Me.offset.X)), (Me.pbpos.Y + (Control.MousePosition.Y - Me.offset.Y)))
End If
End Sub