Mouse position is always between 0, 0 and the native resolution of your monitor, regardless of what buffer size you have set. So the code looks like this (I am using a virtual resolution as my game is designed for 1920x1200, so I am scaling up or down everything depending on the actual resollution):
MouseState mouseState = Mouse.GetState();
Point MousePosition = mouseState.Position;
//the bottom right value is 1919, 1079 at this point.
int VIRTUAL_RESOLUTION_WIDTH = 1920;
int VIRTUAL_RESOLUTION_HEIGHT = 1200;
Vector2 TargetResolutionScale = new Vector2
(
graphics.PreferredBackBufferWidth * 1.0f / VIRTUAL_RESOLUTION_WIDTH,
graphics.PreferredBackBufferHeight * 1.0f / VIRTUAL_RESOLUTION_HEIGHT
)
if (isFullscreen)
{
MousePosition = new Point(
(int)(MousePosition.X * TargetResolutionScale.X),
(int)(MousePosition.Y * TargetResolutionScale.Y));
//the bottom right is now 1279, 719
}
This was just some code snippet to give you some clue, of course this is not how it really looks like in my game.
However, there is one small bug that I experienced, so I need to find a proper workaround for it:
When the game is running in windowed mode, if the resolution is the same as your native resolution and the window is not borderless (so you can see the title, the close button and the rest of the window), the 0,0 position will be the actual rendering window’s 0,0 position, but the mouse’s 0,0 will be the monitor’s 0,0 so there is a small calculation error in the above code. I think I need to check this specific case as add some to the x and y of the final mouse postion.