# [.net] XNA - Mouse coordinates while windowed

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

## Recommended Posts

For the moment, my XNA game isn't running 100% of the time in full screen simply because it's not happy if (certainly not when) a crash occurs. I might have it full screen only upon release, but that remains to be seen. Either way, I've noticed that I have some problems with mouse coordinates. It seems that all the coordinates are shifted down or to the right a bit when the program is windowed. I'm rather confused. The coordinates no longer seem to be in relation to to the upper left hand corner of the window. Nor is are they in relation to the upper left hand corner of the screen. Anyone know why this is? And on a semi-related note, none of the sprites will draw further to the left than the left edge of the screen when windowed. Not a big deal, but everything starts shifting to the right the moment that the window goes to beyond the left edge of the screen. What's up with this? It's very odd behavior that I can't find any documentation about.

##### Share on other sites
I'm positive that you can draw sprites off the edge of the window in windowed mode. I'm also positive that Mouse.GetState() will return coordinates relative to the upper-left corner of your window. Perhaps your code that is analyzing or using the mouse values is creating the problem? Can you post some of your mouse code and some of the drawing code?

What do you mean "everything starts shifting to the right the moment the window goes to beyond the left edge of the screen"? Do you mean when your window has been dragged off of the screen? That shouldn't affect the rendering, but it could mess up the mouse input, though I've never tested. I would have to assume the mouse, though, would still be read in correctly.

##### Share on other sites
Oh, so this isn't a 'feature' like I was expecting. Here's more info:

I'm using XNA 1 Refresh with VC# 2005 on Windows Vista.
Here's how I handle the mouse position:
			KeyboardState keystate = Keyboard.GetState( );			MouseState mousestate = Mouse.GetState( );			Vector2 mouseLocation = new Vector2( mousestate.X, mousestate.Y );

This looks pretty harmless to me. I must be messing up elsewhere, but I have no idea where. I've checked as best I can, and the mouse coordinate seems to be about twelve pixels lower than it should be (I clicked on a an easily identifiable point on the background image and read the mouse coordinates there. Then I went to the actual image and the location of that point was 12 pixels higher than what the mouse click returned. The background image is draw starting at (0,0) ).

As for the issue with sprites being shifted to the right, I mean that if a sprite where to be drawn beyond the edge of the screen, it simply refuses and is drawn as if the left edge of the screen is the x=0 line, not the left edge of the window. This makes it appear as if they are moving to the right by the same amount that I am moving the window to the left. Mouse coordinates, however, are still in terms of the left-edge of the window being x=0, rather than the left edge of the monitor. I just checked the drawing code, and it seems to be in working order. The rectangle that is past to SpriteBatch.Draw() is most certainly normal even when the window is dragged off-screen. That is, the background was being drawn in the rectangle (0,0, 1024,768) even when I could see that it wasn't. Here's a screenshot of it. Note that the window is about 400 pixels beyond the edge of the screen, and yet the background image's left edge is still visible. Nothing happens when I move the window off the bottom or the right of the screen.

Is there anything I can provide you with? I feel like I haven't said much other than rewording my original question. None of my code is doing anything odd with the numbers, and I've checked both immediately before they are being sent off into XNA functions (SpriteBatch.Draw and Rectangle.Contains).

##### Share on other sites
Bump. Thoughts anyone?

##### Share on other sites
Some more specifics and ideally some more code may help us identify your problem.

I suggest setting up debug output to display the X and Y location of the mouse pointer. I checked the XNA docs and it does indeed say that the mouse position, in pixels, is relative to the upper-left corner of the game window. I would check the origin and position of the mouse cursor you are drawing.
If you are setting "IsMouseVisible = true;" then that might be part of the problem. I have only drawn my own custom cursor and had no problems so far. The only problem I've had with the cursor was due to having the sprite origin set incorrectly and using bounds checking that cause the mouse to act weird at the edges of the window.

As for drawing off the edge of the screen; Are you using bounds checking when you draw? I would also suggest double checking your settings for the display, client, viewport, and back buffer size properties. I've drawn plenty of stuff that extends off the screen with no problems, including sprites that are completely drawn off screen(just to see what would happen).

The only unknown is that you're running Vista, it's possible vista does something weird (go figure). Try running it on a XP machine if possible and see if you can reproduce the problem.

And finally I cannot stress enough the importance of using debug features built into the game. Setting up a simple class that display variables via use of DrawString has helped me solve a number of problems, couple that with using exception handling and that should help you solve your crash problems also.

P.S. If you are resizing the window that could be related to some of your problems, though from looking at the screenshot it almost looks like its related to restricting rendering to the title safe region.

##### Share on other sites
Thanks for the suggestions. No, I'm on XP Sp2. The window is not resize-able. I am just using the default mouse pointer. So I made a class to print out the mouse's coordinates. The results were good; the coordinates seemed to always be relative to the upper left hand corner of the window. This would make me think that my rendering is actually messed up and is causing both of these issues.

Okay, that's all I can do right now, but I'll get back later.

##### Share on other sites
Is it maybe the height of the title bar and the width of the left border? I.E. is (0,0) the upper left of the window, including title bar, or the upper left of the viewable/drawable area?

##### Share on other sites
Almost, nagromo. I think I've figured out the problem - the mouse coordinates are not relative to the top of the screen's border, however the total height of the window includes that. That is, since I've got a window of height 768, the title bar is 20px of that, leaving a mere 748 in the window! This seems to have shrunk things ever so slightly. I can't notice it, but by the bottom of the screen, things are off by ~10 pixels. So that's one of the issues resolved (I'll just set the screen height to 20+768 if we're windowed and hope that every Windows skin has a height of 20 over their windows).

But this is a very odd issue. If I move my mouse to the very top of that border, I get the mouse y coordinate of -20. And yet, it is still included in the total height of the window. Then, when I move into full screen, there no longer is a border and I get a full 768 pixels of drawing freedom. Inconsistent much?

Hm, but now I don't know how to change the window's height after the graphics device is created. Can anyone help me out with that?

Update: Okay, got the mouse coordinate problem completely fixed. However, I'm a little confused about the fix. I figured I would have to set graphics.PreferredBackBufferHeight to 788. The exra 20 pixels would cover the top bar and then I have the normal 768 for the actual program. But it turns out that I need to set it to 748 instead! Huh? Why does this work?

Another Update: And now, for no apparent reason, my other issue is fixed. Now the window will draw off-screen to the left just as I would have expected. No clue why it wouldn't before or why it will now!

[Edited by - Ezbez on December 10, 2007 4:40:15 AM]

##### Share on other sites
In my sample project, the game window defaulted to 800X600. Which DID NOT include the size of the title bar. So it sounds like a mistake in your window code setup.

Do a search of the XNA Documentation on Client Bounds, this should explain how things work and may give you some ideas on how to work with the issue.

You could try this link, not sure if it will work:
ms-help://MS.VSExpressCC.v80/MS.VSIPCC.v80/MS.XNAFX.1033/XNA/DisplaysAndViewports.htm

I also confirmed that the mouse coords are reported relative to the upper left corner of the game window (which does not include the title bar). While it may not be the same behavior you get in fullscreen; it does allow you to identify when and where the cursor is moved out of the game window, allowing for changing focus on windows, etc.

If you dont like the way the mouse coords go negative, its a very simple fix to add bounds checking code to keep the mouse coordinates within the game window coords using the MathHelper.Clamp method.