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



目标是在左键单击并拖动鼠标时平移表单上的表单或图像。 下面的代码非常适合我的需求,但只有一个问题。 当我左键单击并拖动鼠标时,表单的平移速度比鼠标快,使其很尴尬。 有什么方法可以使表单以与鼠标相同的速率平移吗? 这是我的代码:

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

谢谢!

你的代码中有一个小错误:

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

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

最新更新