Heinzi
Heinzi

Reputation: 172468

Check if mouse is inside the game window

I'm using XNA to visualize a 3D scene in a window (= not full-screen). The user can click and drag the mouse to move the camera:

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

This works. The problem is that this even works when the mouse is not inside the game window, which looks a bit strange (I move a mail in Outlook and the 3D scene in the other window starts turning).

I didn't find a Mouse.IsInsideGameWindow() property. Is there anything else that I can (easily) do to avoid this?

Upvotes: 0

Views: 3162

Answers (1)

Blau
Blau

Reputation: 5762

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

Upvotes: 8

Related Questions