Do not let the mouse leave my form

I attached several MouseMove and MouseClick events to my program, and the following is one of them:

  • Get a "global" mouse movement so that I can read the location of the mouse even outside the form.
  • Do not let my mouse leave the form in the first place.

My project is a game, so it would be great if the mouse did not leave my form, as in most other games (for example, you can move it if you switch focus using alt + tab fe.) And look at the answers to other issues requiring a global movement of monsieur, they seem pretty dirty for my needs.

Is there an easy way to prevent my mouse from going beyond my form? Or, firstly, so that it does not cross borders in the first place, I want the mouse to remain inside the client area.


Additional information about the game:

The game is a short game lasting 5-30 seconds (it gets too heavy after 30 seconds to keep you alive), where you have to dodge bullets with the mouse. This is very annoying when you push the mouse out of the form, and then the player ( System.Windows.Forms.Panel attached to the mouse) stops and instantly hits the bullet. This is why preventing the mouse from moving out of the area would be nice.

+7
source share
5 answers

Late answer, but may come in handy. You can subscribe to MouseLeave and MouseMove forms and process them as follows:

  private int X = 0; private int Y = 0; private void Form1_MouseLeave(object sender, EventArgs e) { Cursor.Position = new Point(X, Y); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (Cursor.Position.X < this.Bounds.X + 50 ) X = Cursor.Position.X + 20; else X = Cursor.Position.X - 20; if (Cursor.Position.Y < this.Bounds.Y + 50) Y = Cursor.Position.Y + 20; else Y = Cursor.Position.Y - 20; } 

The above will make sure that the mouse cursor never goes beyond the bounds of the form. Make sure you unsubscribe from events when the game is finished.

Edit:

Hans Passants answer makes more sense than my answer. Use Cursor.Clip on MouseEnter :

 private void Form1_MouseEnter(object sender, EventArgs e) { Cursor.Clip = this.Bounds; } 

You can free the cursor in case of an error / failure (I'm sure you could catch it):

 Cursor.Clip = Rectangle.Empty; 
+4
source

You cannot capture the mouse, which will prevent the user from, for example, launching the Start menu. Closest you can assign the Cursor.Clip property. But it is easily defeated by the user by pressing Ctrl + Esc, for example, there are no notifications for this.

It’s best to subscribe to the “Deactivated event” form, it reliably informs you that the user has switched to another program. An activated event tells you when the user has returned. Of course, the user will have several reasons to actually do this when the rating of the game depends on the movement of the game object. Therefore, do not forget to give the user an easy way to pause the game, say, using the Escape key.

+4
source

I do not know the solution to your specific problem, but I have a completely different idea for you. I don’t know how your game works, but based on what you told me, why not make it even harder: add borders to the game zone, for example, 4 pixels wide rectangles that you cannot touch. If you touch them, you die, and the mouse is freed.

+2
source

You can use the Cursor class. For instance:

 int X = Cursor.Position.X; int Y = Cursor.Position.Y; 

As well as to prevent the user from moving the mouse outside the form, the best approach would probably be if you knew what the coordinates of your form are on the screen and attach the MouseMove , and check that the mouse is inside the form rectangle.

To find out the position of the form on the screen, review this question.

+1
source

I would not recommend global mouse movement control for two reasons.

  • Bad design, you must respect the boundaries of the operating system. Make the app fullscreen if you want this behavior. The only applications that must perform such operations are kiosk applications that block the entire OS (to prevent abuse by the operator).

  • Global key interceptors are erratic, operation is not guaranteed, and they are dangerous because they affect a key part of the operating system (all controls). An error in your code may result in a reboot on the computer.

However, the last time I checked (some time ago, on Vista), SetWindowsHookEx still works (but it is not officially supported by IIRC), it is an uncontrollable call, so you have to ping, but you can refuse it send messages that will move the mouse outside of your application. I am not 100% sure if the OS allows you to beat it to the cursor control (I only blocked the keyboard on the desktop boxes), but probably your best shot.

0
source

All Articles