[MDX] Constrain cursor to fullscreen window?

First of all, I'm not at all sure that this is a DirectX question or a C# question. If it is misplaced please tell me so and I'll repost it elsewhere. But here goes:

I have two monitors in dualview, that is: if I move my cursor to the right of my screen it pops up on the other screen. When I'm in windowed mode I would like this behaviour but in fullscreen I would like to constrain the cursor to the primary screen / the fullscreen application.

I know many (most ) games does this in fullscreen and it's indeed very irritating if you miss and click on the right screen, then the fullscreen application will get minimized, lost and then you have to maximize it again.

Now I guess I could do a check constantly to see if the cursor is out of the application boundaries and warp it back but it's not an elegant solution at all and it may be slow aswell. This is a common problem and I guess it has a common solution. I haven't found any constrain functions that could help me in MSDN / DirectX Documentation.


Answer this question

[MDX] Constrain cursor to fullscreen window?

  • robatdural

    At first glance it seems to look just like its supposed to. Thanks a bunch, I'll test it more and if it doesn't work properly I'll post here so others know about it.

  • Ben Jackson

    You could do a simple test to make sure that the mouse never leaves the first screen. Normally in a dual screen setup the size of the display area is just extended such as 2048x768, this means that you basically have 2 x 1024x768 display areas.

    With this said you can then use something like this
    private void ValidateMouseCoordinates(Form window)
    {
    cursor = window.PointToClient(Cursor.Position);
    if(cursor.X < 0) //if the x position is less than 0. set it to the window width
    cursor.X = window.Width / 2; //keep it in the region of the of the first monitor.
    if(cursor.Y < 0) //if the y position is less than 0. set it to the window height
    cursor.Y = window.Height;
    }

    There might be a number of other ways to do this. This just seem the simplest right now.

    I hope this helps.
    Take care.


  • Lee Coward

    Did you try Cursor.Clip

    http://msdn2.microsoft.com/en-us/library/07yd9yty(en-US,VS.80).aspx

    (my only concern is that it says it may not work unless the cursor is captured but the sample code doesn't show that)



  • GavinWu

    Well, that's basically the same as checking mouse position and warp it back if it's outside the bounds with the difference that if I only hide it and I click (even though it's hidden) outside the bounds on a secondary monitor for example. Then the application will minimize and become lost since it's in fullscreen.

    But is there really no standard way to do this I guess I'll have to do bounds checking and warp if outside then O.o

  • Guy C.

    My first guess is to determine where the mouse position is on the first screen. If it's greater than the screen's width/height, don't show the mouse cursor. If the mouse position is less than the width/height, show it again at a default center position (such as width/2, height/2). I believe the method for that is Cursor.Hide() and Cursor.Show(). Hope that helps.


  • [MDX] Constrain cursor to fullscreen window?