
Originally Posted by
abuckau907
as you move over the box, your cursor will have speed --> the code will center the mouse, and your finger/hand will still have momentum and keep moving the mouse, however slightly. Probably not a problem, just throwing that in.
I am aware of that
here is part of the code that stops momentum by putting a "hold" on sticky snap for a few milliseconds
Public Class Form1
' When the Snap Hold should break
Private dSnapEnd As Date
' Duration of the Snap Hold - set this value to
' determine how long we hold the cursor
Private dReleaseTime As Double = 250
Private Sub SnapToCenter(ByVal control As Control)
Dim pCenter As Point
' get the center of the control
pCenter.X = control.Width / 2
pCenter.Y = control.Height / 2
' Set the cursor to the center
Cursor.Position = control.PointToScreen(pCenter)
End Sub
' Event to handle MouseEnter events
Private Sub StartSnap(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
' Start by snapping the cursor to the center
SnapToCenter(CType(sender, Control))
' Set the starting and ending snap times
dSnapEnd = dSnapStart.AddMilliseconds(dReleaseTime)
End Sub
' Event to handle MouseMove events
Private Sub CheckSnap(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
' If we've not reached the SnapEnd time, center the mouse
If Date.Now < dSnapEnd Then
SnapToCenter(CType(sender, Control))
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Add the event handlers for each control.
AddHandler Button1.MouseEnter, AddressOf StartSnap
AddHandler Button1.MouseMove, AddressOf CheckSnap
AddHandler YourControlHere.MouseEnter, AddressOf StartSnap
AddHandler YourControlHere.MouseMove, AddressOf CheckSnap
End Sub
End Class