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 に答える