Set the mouse pointer to an arbitrary position

Page update date :
Page creation date :

summary

Set the mouse pointer position to the desired location. In the sample, pressing the left mouse button moves the mouse pointer to the upper left corner of the screen.

Operating environment

Prerequisites

Supported XNA Versions
  • 3.0
  • 3.1
  • 4.0
Supported Platforms
  • Windows (XP SP2 or later, Vista, 7)
  • Xbox 360
  • Windows Phone 7
Windows Required Vertex Shader Version 2.0
Windows Required Pixel Shader Version 2.0

Operating environment

platform
  • Windows 7
  • Xbox 360
  • Windows Phone 7 Emulator

How to work with the sample

Works keyboardXbox 360 controllermouse touch
Mouse Position Set - - Left Button -

substance

You can use the "Mouse.SetPosition" method to set the position of the mouse pointer. The screen coordinates are based on the upper left of the screen as the origin (0, 0), with the upper left direction being minus and the lower right direction being positive.

By the way, you can also set the mouse pointer outside the game window, and if the mouse pointer position extends outside the display area, the mouse pointer will be set at the edge of the display area.

Mouse.SetPosition(0, 0);

Mouse.SetPosition method

Set the mouse pointer position relative to the origin of the upper left corner of the screen.

x int Horizontal position relative to the left side of the screen. Negative to the left, positive to the right
y int Vertical position relative to the top of the screen. Negative upward, positive downward

The Xbox 360 allows you to write mouse-related programs, but you can't use the mouse itself, so it doesn't make any difference. Also, nothing happens on Windows Phone because there is no concept of the position of the mouse pointer.

All Codes

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;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif

namespace SetMousePosition
{
    /// <summary>
    /// ゲームメインクラス
    /// </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
        /// <summary>
        /// グラフィックデバイス管理クラス
        /// </summary>
        private GraphicsDeviceManager graphics = null;

        /// <summary>
        /// スプライトのバッチ化クラス
        /// </summary>
        private SpriteBatch spriteBatch = null;


        /// <summary>
        /// GameMain コンストラクタ
        /// </summary>
        public GameMain()
        {
            // グラフィックデバイス管理クラスの作成
            this.graphics = new GraphicsDeviceManager(this);

            // ゲームコンテンツのルートディレクトリを設定
            this.Content.RootDirectory = "Content";

#if WINDOWS_PHONE
            // Windows Phone のデフォルトのフレームレートは 30 FPS
            this.TargetElapsedTime = TimeSpan.FromTicks(333333);

            // バックバッファサイズの設定
            this.graphics.PreferredBackBufferWidth = 480;
            this.graphics.PreferredBackBufferHeight = 800;

            // フルスクリーン表示
            this.graphics.IsFullScreen = true;
#endif

            // ウインドウ上でマウスのポインタを表示するようにする
            this.IsMouseVisible = true;
        }

        /// <summary>
        /// ゲームが始まる前の初期化処理を行うメソッド
        /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う
        /// </summary>
        protected override void Initialize()
        {
            // TODO: ここに初期化ロジックを書いてください

            // コンポーネントの初期化などを行います
            base.Initialize();
        }

        /// <summary>
        /// ゲームが始まるときに一回だけ呼ばれ
        /// すべてのゲームコンテンツを読み込みます
        /// </summary>
        protected override void LoadContent()
        {
            // テクスチャーを描画するためのスプライトバッチクラスを作成します
            this.spriteBatch = new SpriteBatch(this.GraphicsDevice);

            // TODO: this.Content を使用してゲームコンテンツを読み込む
            //       ロジックを書いてください
        }

        /// <summary>
        /// ゲームが終了するときに一回だけ呼ばれ
        /// すべてのゲームコンテンツをアンロードします
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: ContentManager で管理されていないコンテンツを
            //       ここでアンロードしてください
        }

        /// <summary>
        /// 描画以外のデータ更新等の処理を行うメソッド
        /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Update(GameTime gameTime)
        {
            // Xbox 360 コントローラ、Windows Phone の BACK ボタンを押したときに
            // ゲームを終了させます
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // マウスの状態を取得
            MouseState mouseState = Mouse.GetState();

            // マウスの左ボタンを押したら画面の左上にポインタをセットする
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                Mouse.SetPosition(0, 0);
            }

            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }

        /// <summary>
        /// 描画処理を行うメソッド
        /// </summary>
        /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: ここに描画処理を記述します

            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}