使用左键单击平移图像:滚动速度比鼠标 (.NET) 快

Mik*_*ike 5 .net vb.net pan autoscroll panning

目标是在左键单击并拖动鼠标时平移窗体或窗体上的图像。下面的代码非常适合我的需要,但只有一个问题。当我左键单击并拖动鼠标时,表单的平移速度比鼠标快,因此很尴尬。有没有什么方法可以让我以鼠标相同的速度制作表格?这是我的代码:

Private leftClick as Point

Private Sub Form_OnMouseDown(sender as Object, e as MouseEventArgs) Handles Form.MouseDown
    leftClick = e.Position
End Sub

Private Sub Form_OnMouseMove(sender as Object, e as MouseEventArgs) Handles Form.MouseMove
    'Resolves issue where mouse keeps on moving even if not
    Static MyPoint As New Point
    If e.Location = MyPoint Then Exit Sub
    MyPoint = e.Location

    'If we left click anywhere on form, pan the form
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Dim DeltaX As Integer = (leftClick.X - e.x) 
        Dim DeltaY As Integer = (leftClick.Y - e.y) 

        Dim newX as Integer = DeltaX - Me.AutoScrollPosition.X
        Dim newY as Integer = DeltaY - Me.AutoScrollPosition.Y

        'Then we set the new autoscroll position.
        Me.AutoScrollPosition = New Point(newX, newY)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

谢谢!

γηρ*_*όμε 6

你的代码有一个小错误:

Private Sub Form_OnMouseMove(sender as Object, e as MouseEventArgs) Handles Form.MouseMove
    'Resolves issue where mouse keeps on moving even if not
    Static MyPoint As New Point
    If e.Location = MyPoint Then Exit Sub
    MyPoint = e.Location

    'If we left click anywhere on form, pan the form
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Dim DeltaX As Integer = (leftClick.X - e.x) 
        Dim DeltaY As Integer = (leftClick.Y - e.y) 

        Dim newX as Integer = DeltaX - Me.AutoScrollPosition.X
        Dim newY as Integer = DeltaY - Me.AutoScrollPosition.Y

        'Then we set the new autoscroll position.
        Me.AutoScrollPosition = New Point(newX, newY)

        'Add this code
        leftClick.X = e.X '<---
        leftClick.Y = e.Y '<---
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

您需要前一个鼠标位置和当前位置之间的差异。您所做的是根据您单击的位置计算总数。