vb.net 如何用鼠标移动图片框,但鼠标保持在它的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13635536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:23:18  来源:igfitidea点击:

How to move picturebox with mouse, but with the mouse remaining on it's position?

vb.netvisual-studio-2010vb.net-2010

提问by

I had some code to move the control around, but when you reached the end of the screen you had to release your right mouse button, right click on the image again, and drag again, .. repeat..

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

Now I tried to rewrite the code so the control would move without the mouse being moved. This is what I have for now:

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

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

However the image doesn't move, it returns to it's original position along with the mouse.

然而图像不会移动,它会随着鼠标返回到它的原始位置。

How would I make this code move the picturebox without allowing the mouse to change position?

我如何让这段代码移动图片框而不让鼠标改变位置?

采纳答案by

Ah I solved it quite easily:

啊我很容易地解决了它:

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

If there is some better code to use, let me know.

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