0

I have a Mouse Move method that moves a Button up and down along the Y axis on a WinForms project
When the Cursor is outside the bounds of the form the Button will not move
I would like to limit the Cursor from exiting or losing focus of the form
I would also like to set a limit on the top and bottom Y positions of the Button btnPad
Other Controls on the form are three buttons that form a TOP, BOTTOM and RIGHT WALL
The Y INSIDE dimensions of the TOP and BOTTOM walls are 50 and 690
FWIW the Form dimensions are Width 1000 and Height 890

I have some code that prevents the cursor from entering the form called MoveCursor
I will post some code I have tried like frmStart MouseLeave and MouseEnter as well as the actual MouseMove code

The Question is how to prevent the Cursor from exiting the Form?

Private Sub MoveCursor()
    ' Set the Current cursor, move the cursor's Position,
    ' and set its clipping rectangle to the form. 
    Me.Cursor = New Cursor(Cursor.Current.Handle)
    Cursor.Position = New Point((Cursor.Position.X + 1000), (Cursor.Position.Y + 890))
    Cursor.Clip = Me.Bounds
    Cursor.Clip = New Rectangle(Me.Location, Me.Size)
End Sub
Private Sub frmStart_MouseLeave(ByVal sender As Object, ByVal e As EventArgs)
    Cursor.Position = New Point(X, Y)
    Cursor.Clip = Me.Bounds
End Sub
Private Sub frmStart_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    MoveCursor()
    btnPad.Top = e.Y
End Sub

The code below was as close as I could come to keeping the Cursor inside the play area which is the form the mouse still wanders out on the right side

Private Sub frmStart_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    Cursor.Clip = New Rectangle(Me.Location, Me.Size)
    btnPad.Top = e.Y
End Sub
Vector
  • 3,066
  • 5
  • 27
  • 54
  • 3
    This seems like a very bad idea to me, I'm sure you have your reason but sill. If your mouse is limited to only your form, how is the user going to access other windows like the start button? – Hursey Sep 10 '20 at 20:32
  • @Hursey I agree this was a issue noted in another SO Post I only have one form and make use of KeyPress events to manage Start and Stop Button Because it is a Game the number of Controls are limited Valid Concern https://stackoverflow.com/questions/15029274/prevent-mouse-from-leaving-my-form – Vector Sep 10 '20 at 20:45
  • What isn't working with your Cursor.Clip solution? – Darryl Sep 12 '20 at 00:00
  • @Darryl I boiled the Cursor Clip down to the code I just posted in an Update to the question While it is better than what I had still not real perfect I am not much of a game developer just especially if you consider I am using visual basic Any suggestions are welcome and appreciated – Vector Sep 13 '20 at 15:59

0 Answers0