ExclamationAutocenter Mouse on Picturebox

Posts 1–5 of 5 · Page 1 of 1
Autocenter Mouse on Picturebox
How do you put the mouse cursor on the center of a picture box within the form when the mouse goes into the picture box?
the picture box is 72 by 72 and it's located 195,60 on my form. also i want to be able to override this when i move the mouse out of the picture box

---------- Post added at 05:17 AM ---------- Previous post was at 03:20 AM ----------

Nvm i have found the answer online.
well, half way would be ImageWidth / 2 and ImageHeight / 2

Cursor.Position = New Point(PictureBox1.Left + (PictureBox1.Width \2),PictureBox1.Top + (PictureBox1.Height \ 2))

Or something very close to that.

Post code, in case someone comes to this thread having a similar issue?

Good luck on moving it back out of the picture box...some custom logic needed for that one I guess. Again, I think the invisible form is not required, and is actually causing you problems. You need to re-think your design. imo.
Just on the event, mouse enter and it'll center. Its a one time event
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.
Quote Originally Posted by abuckau907 View Post
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
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us