I'd like to prevent the user from putting their mouse outside the window. Because you target enemies with your mouse in my game, if there's an enemy close to the edge of the screen, there's a good chance the user will overshoot and click outside the window - thus defocusing the window, disrupting gameplay, and angering the user.
Yes, I know. Full-screen applications never have this problem. But most of my program is already windowed (only the action sequences need the directX form, not the option menu, map screen, etc), and I'd prefer to keep it windowed. Obviously, if the user alt-tabs out of the window, the restriction on mouse movement should be released until they've gone back into the game again.
I'm hoping there's a relatively painless way to do this... does DirectInput mouse have a way to clip the mouse without a certain boundary, or something

Keep mouse inside window
shosholoza
MSDN documents it quite well. Don't forget to call ::ReleaseCapture() when your window loses focus (say, because of alt-tab).
abhinavvaid
It worked!
To use those methods in C#, it looks like this:
[
DllImport("user32.dll")] static extern bool ClipCursor(ref Rectangle lpRect);[
DllImport("user32.dll")] static extern bool GetClipCursor(ref Rectangle lpRect);battleofhalfwits
I can't find an equivalent managed call, but you might make use of the Win32 call "ClipCursor", which allows you to constrain movement of the cursor to a specified rectangle.
Prior to doing this, call "GetClipCursor" to retrieve the current cursor constraints, and store this rectangle. When exiting your application, call ClipCursor again with the retrieved rectangle to restore the original range of movement.
Robert Dunlop
Microsoft DirectX MVP
www.directxzone.org
Crispin
you could also try to set your mouse device to exclusive and foreground modes (DISCL_EXCLUSIVE and DISCL_FOREGROUND) and draw the mouse cursor yourself at the appropriate coordinates.
vvgalc
Leandro Mussi
Rahul_It
Here's my code so far:
[
DllImport("user32.dll")] static extern IntPtr SetCapture(IntPtr hWnd);SetCapture
(this.Handle); // in Form_loadI'm no longer getting an error when SetCapture runs, but it still doesn't apear to actually do anything. My cursor turns into a nice hourglass, but I can still move my mouse outside the form (NOT what I want, the mouse should be stuck inside the form until it either closes or you alt-tab out)...
... any ideas
Ed Mee
Or the easier to understand Cursor.Clip
http://msdn2.microsoft.com/en-us/system.windows.forms.cursor.clip.aspx