用左键单击平移图像:滚动速度比鼠标快 (.NET)
Pan an Image with Left Click: Rate of Scroll is faster than Mouse (.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
您需要鼠标先前位置与当前位置之间的差异。你所做的是从你点击的位置开始计算总数。
目标是在单击鼠标左键并拖动鼠标时平移窗体或窗体上的图像。下面的代码非常适合我的需要,但只有一个问题。当我左键单击并拖动鼠标时,窗体的平移速度比鼠标快,这让人很尴尬。有什么方法可以使表单 以与我的鼠标相同的速率 平移吗?这是我的代码:
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
您需要鼠标先前位置与当前位置之间的差异。你所做的是从你点击的位置开始计算总数。