简体   繁体   中英

Getting the window coordinates in MonoGame

What I'm actually trying to do, is to get the mouse coordinates inside a windowed game. So far, I've only found ways to retrieve the screen coordinates of the mouse. Therefore, I would like to know the position of the window, so that I can subtract that from the mouse's screen coordinates in order to get the mouse's window coordinates.

Is this possible, and if so, how?

Have you tried Mouse.GetState ? It returns a MouseState with X and Y properties on it. The documentation on MSDN is pretty sparse, but if you look carefully at documentation for the specific X and Y properties you'll see that it returns a position relative to the upper left corner of the window which is more in line with what your after.

MouseState.X - Horizontal position of the mouse cursor in relation to the upper-left corner of the game window.

MouseState.Y - Vertical position of the mouse cursor in relation to the upper-left corner of the game window.

The advantage of doing this way is that it's not platform specific, so if you port your game to another platform in the future, the code won't have to change.

您是否尝试过引用System.Windows.Forms并通过Window.Handle从handle获取Form,然后从Form获取位置?

Point WindowCoordinates = System.Windows.Forms.Form.FromHandle(Window.Handle).Location;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM