Jump to content
  • Advertisement
Sign in to follow this  
BenjaminSergent

XNA - Trapping the Mouse in Windowed Mode

This topic is 2507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Perhaps you can set the mouse window handle property to the graphics device viewport.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.mouse.windowhandle.aspx
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.viewport.aspx

Because at the moment it would be set to the O.S underlying window.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.window.aspx

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I don't know if .Net has an equivalent yet, but in Win32 you can use ClipCursor. I seem to recall having trouble P/invoking it, so I wrote a C++/CLI DLL just for it...

Share this post


Link to post
Share on other sites
Perfect, that does exactally what I want. For anyone who's curious:



//<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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!