我有一些代码可以移动控件,但是当你到达屏幕的末尾时,你必须释放鼠标右键,再次右键单击图像,然后再拖动,重复..
现在我尝试重写代码,这样控件就可以在不移动鼠标的情况下移动。这就是我现在所拥有的:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Dim xx As String = DirectCast(e, MouseEventArgs).Button.ToString
If xx = "Right" Then
Dim Px As Point = New Point(Me.Location.X + SplitContainer1.Location.X + SplitContainer1.SplitterDistance + CInt((SplitContainer1.Width - SplitContainer1.SplitterDistance) / 2), Me.Location.Y + SplitContainer1.Location.Y + CInt(SplitContainer1.Height / 2))
Windows.Forms.Cursor.Position = Px
PictureBox1.Left = PictureBox1.Left + (e.X - Px.X)
PictureBox1.Top = PictureBox1.Top + (e.Y - Px.Y)
End If
End Sub
然而,图像不会移动,它会随鼠标一起返回原始位置。
如何在不允许鼠标更改位置的情况下使此代码移动图片框?
答案 0 :(得分:0)
啊,我很容易解决它:
Dim TemporaryMousePointerLocation As Point
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Static UpdateMouseOnNextMove As Boolean = False ' we need to have an update delay between each mouse move, if we update the position of the mouse each mousemove then there will be no movement
If DirectCast(e, MouseEventArgs).Button = MouseButtons.Right Then
If UpdateMouseOnNextMove Then
PictureBox1.Left -= (Windows.Forms.Cursor.Position.X - TemporaryMousePointerLocation.X)
PictureBox1.Top -= (Windows.Forms.Cursor.Position.Y - TemporaryMousePointerLocation.Y)
Windows.Forms.Cursor.Position = TemporaryMousePointerLocation
UpdateMouseOnNextMove = False
Else
UpdateMouseOnNextMove = True ' allow cursor to move a bit so we can update the position of the mouse
End If
Else
TemporaryMousePointerLocation = Windows.Forms.Cursor.Position
End If
End Sub
如果有更好的代码可供使用,请与我们联系。