简体   繁体   English

在MonoGame中获取窗口坐标

[英]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 ? 您是否尝试过Mouse.GetState It returns a MouseState with X and Y properties on it. 它返回带有X和Y属性的MouseState。 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. MSDN上的文档非常稀疏,但是如果您仔细查看特定XY属性的文档,您会发现它返回相对于窗口左上角的位置,该位置与您的操作更加一致。

MouseState.X - Horizontal position of the mouse cursor in relation to the upper-left corner of the game window. MouseState.X-鼠标光标相对于游戏窗口左上角的水平位置。

MouseState.Y - Vertical position of the mouse cursor in relation to the upper-left corner of the game window. MouseState.Y-鼠标光标相对于游戏窗口左上角的垂直位置。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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