Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pan an Image with Left Click: Rate of Scroll is faster than Mouse (.NET)

The goal is to pan a form or image on the form when left clicking, and dragging the mouse. The below code works perfectly fine for my needs, but there is only one issue. When I left click and drag the mouse, the form pans quicker than the mouse, making it awkward. Is there any method I can use to make the form pan at the same rate as my mouse? Here is my code:

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

Thanks!

like image 658
Mike Avatar asked Jan 20 '15 14:01

Mike


Video Answer


1 Answers

You have a little mistake in your code:

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

You need the difference between the previous mouse position and the current one. What you did was to calculate the total from the position you clicked.