Export (0) Print
Expand All

How To: Get the Current Mouse Position (Windows)

Demonstrates how to get the current horizontal and vertical position of the mouse cursor relative to the upper-left corner a game window.
Bb197572.note(en-us,XNAGameStudio.31).gifNote
This example applies only to Windows development. The Mouse and MouseState objects are not supported on Xbox 360.

The example queries the Mouse class to return a MouseState object that holds the button states and the current position of the mouse relative to the upper-left corner of the game window.

Retrieving the Current Mouse Position

To retrieve the current mouse position

  1. Call GetState to get the current state of the mouse in a MouseState object.

  2. Access the X and Y properties of the MouseState retrieved in the previous section to get the X (horizontal) and Y (vertical) mouse position, in pixels, relative to the upper-left corner of the game window.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace MousePosition
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        Color backColor = Color.CornflowerBlue;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
        }

        protected override void Initialize()
        {
            base.Initialize();

            // Set this to true to make the mouse cursor visible.
            // Use the default (false) if you are drawing your own
            // cursor or don't want a cursor.
            this.IsMouseVisible = true;
        }

        protected override void LoadContent()
        {
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == 
                ButtonState.Pressed)
                this.Exit();

            UpdateMouse();

            base.Update(gameTime);
        }

        protected void UpdateMouse()
        {
            MouseState current_mouse = Mouse.GetState();

            // The mouse x and y positions are returned relative to the
            // upper-left corner of the game window.
            int mouseX = current_mouse.X;
            int mouseY = current_mouse.Y;

            // Change background color based on mouse position.
            backColor = new Color((byte)(mouseX / 3), (byte)(mouseY / 2), 0);
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(backColor);
            base.Draw(gameTime);
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft