0

我有一些代码可以移动控件,但是当您到达屏幕末尾时,您必须释放鼠标右键,再次右键单击图像,然后再次拖动,.. 重复..

现在我尝试重写代码,以便在不移动鼠标的情况下移动控件。这就是我现在所拥有的:

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

然而图像并没有移动,它随着鼠标返回到原来的位置。

如何让这段代码在不让鼠标改变位置的情况下移动图片框?

4

1 回答 1

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

如果有更好的代码可以使用,请告诉我。

于 2012-11-29T22:25:45.070 回答