检查鼠标是否在游戏窗口内

Hei*_*nzi 0 .net xna

我正在使用XNA来显示窗口中的3D场景(=不是全屏).用户可以单击并拖动鼠标来移动相机:

Public Sub New()
    ...
    Me.IsMouseVisible = True
    Me.Window.AllowUserResizing = True
    ...
End Sub

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
    Dim m = Mouse.GetState()

    ' Change camera position based on m
    ...
End Sub
Run Code Online (Sandbox Code Playgroud)

这有效.问题是,当鼠标不在游戏窗口内时,这甚至可以工作,这看起来有点奇怪(我在Outlook中移动邮件,而另一个窗口中的3D场景开始转动).

我找不到Mouse.IsInsideGameWindow()房产.还有什么我可以(轻松)做到避免这种情况吗?

Bla*_*lau 8

bool IsMouseInsideWindow()
{
    MouseState ms = Mouse.GetState();
    Point pos = new Point(ms.X, ms.Y);
    return GraphicsDevice.Viewport.Bounds.Contains(pos);
}
Run Code Online (Sandbox Code Playgroud)