I'm using XNA to make a game in which mouse movement controls the rotation of the camera. When running in windowed mode, I wanted to ensure that the cursor doesn't leave the screen if the game's not paused. I tried using Mouse.SetPosition, but the mouse still leaves the screen when moved quickly. This semi-frequently results in clicking off the screen, losing focus at undesirable times.
What can I do to prevent this?
XNA - Trapping the Mouse in Windowed Mode
Started by Labouts, Feb 02 2012 01:50 PM
5 replies to this topic
Ad:
#2 Members - Reputation: 110
Posted 02 February 2012 - 03:17 PM
Perhaps you can set the mouse window handle property to the graphics device viewport.
http://msdn.microsof...ndowhandle.aspx
http://msdn.microsof...e.viewport.aspx
Because at the moment it would be set to the O.S underlying window.
http://msdn.microsof...ame.window.aspx
http://msdn.microsof...ndowhandle.aspx
http://msdn.microsof...e.viewport.aspx
Because at the moment it would be set to the O.S underlying window.
http://msdn.microsof...ame.window.aspx
#3 Members - Reputation: 786
Posted 02 February 2012 - 04:33 PM
I am going to say this is a VERY VERY idea. The reason people run things in window mode is so we can quickly do other things and then come back to the game. The "correct" way to handle this is to pause your game when the person leaves the area or is in another window.
#4 Members - Reputation: 120
Posted 02 February 2012 - 11:19 PM
To imagine the camera controls, think of Minecraft, Skyrim or Counter-Strike. Those games all trap the mouse because they would be unplayable in windowed mode otherwise; in fact, such games can be unplayable full screen with dual monitors if they don't trap the mouse. The player would still be able to quickly switch out by tapping TAB, Alt-TAB or ESC (opening the menu, removing focus from the game window or pausing the game).
blackbook: Unless I'm doing this wrong, changing the window handle doesn't seem to trap the mouse in that window.
blackbook: Unless I'm doing this wrong, changing the window handle doesn't seem to trap the mouse in that window.
#6 Members - Reputation: 120
Posted 03 February 2012 - 01:44 AM
Perfect, that does exactally what I want. For anyone who's curious:
Obviously, this code only runs when the player is using the mouse for the camera. The mouse is unclipped with no position reseting in every other input state. Thanks for the help.
//<Mouse handler class>
[DllImport("user32.dll")]
static extern bool ClipCursor(ref System.Drawing.Rectangle lpRect);
//<In the relevant method>
Mouse.SetPosition( gameWindow.ClientBounds.Width/2,
gameWindow.ClientBounds.Height/2);
System.Drawing.Rectangle window = new System.Drawing.Rectangle( gameWindow.ClientBounds.Left,
gameWindow.ClientBounds.Top,
gameWindow.ClientBounds.Right,
gameWindow.ClientBounds.Bottom );
ClipCursor( ref window );
Obviously, this code only runs when the player is using the mouse for the camera. The mouse is unclipped with no position reseting in every other input state. Thanks for the help.


















