Ottenere lo stato del mouse
sommario
Ottiene lo stato corrente del mouse. Vengono visualizzati il funzionamento di ciascun pulsante, la posizione del mouse e la quantità di rotazione della rotellina.
Ambiente operativo
Prerequisiti
| Versioni XNA supportate | 
  | 
| Piattaforme supportate | 
  | 
| Versione Vertex Shader richiesta da Windows | 2.0 | 
| Versione Pixel Shader richiesta da Windows | 2.0 | 
Ambiente operativo
| piattaforma | 
  | 
Come utilizzare l'esempio
| Funziona con tastieraController Xbox | 360Tocco del mouse | |||
|---|---|---|---|---|
| Convalida dell'input | - | - | ogni | - | 
sostanza
Per ottenere lo stato di pressione del pulsante del mouse o la posizione del mouse, utilizzare il metodo "Mouse.GetState". Questi stati vengono archiviati in una struttura MouseState.
// マウスの状態を取得
this.mouseState = Mouse.GetState();
Mouse.GetState metodo
Ottieni la posizione del mouse, lo stato di pressione dei pulsanti e così via.
| Valori restituiti | Stato del mouse | Ottiene lo stato del mouse | 
In questo esempio, lo stato del mouse viene visualizzato nel metodo Draw, quindi MouseState viene dichiarato come campo.
 <summary>
 マウスの状態を記憶
 </summary>
private MouseState mouseState = Mouse.GetState();
bottone
È possibile determinare se viene premuto un pulsante del mouse confrontandolo con l'enumerazione "ButtonState". Per verificare se è premuto, è possibile confrontarlo con "ButtonState.Pressed" e per verificare se è stato rilasciato, è possibile controllare con "ButtonState.Released".
// マウスの左ボタン
if (this.mouseState.LeftButton == ButtonState.Pressed)
{
    stateMessage = "Pressed";
}
else
{
    stateMessage = "Released";
}
È possibile utilizzare i seguenti pulsanti del mouse.
- Pulsante sinistro
 - Pulsante centrale
 - Pulsante destro
 - XButton1
 - XButton2
 
ubicazione
La posizione del mouse può essere ottenuta con "MouseState.X" e "MouseState.Y". Questo valore sarà il valore con l'origine (0,0) in alto a sinistra dello schermo, e in basso a destra sarà più è positivo, e in basso a sinistra sarà più negativo sarà.
// マウスの位置
this.spriteBatch.DrawString(this.font,
    "Position : " + this.mouseState.X + ", " + this.mouseState.Y,
    new Vector2(40.0f, 150.0f), Color.White);
ruota
La quantità di scorrimento della rotellina del mouse può essere ottenuta con "MouseState.ScrollWheelValue". La quantità di scorrimento non è la quantità di rotazione in quel momento, ma viene aggiunta o sottratta dal valore precedente.
// ホイールの回転量
this.spriteBatch.DrawString(this.font,
    "ScrollWheelValue : " + this.mouseState.ScrollWheelValue,
    new Vector2(40.0f, 175.0f), Color.White);
È possibile scrivere programmi relativi al mouse su Xbox 360, ma non è possibile utilizzare il mouse stesso, quindi non sarà possibile ottenere un buon valore. In Windows Phone è possibile utilizzare solo il pulsante sinistro anziché il tocco singolo.
Tutti i codici
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 GetMouseState
{
     <summary>
     ゲームメインクラス
     </summary>
    public class GameMain : Microsoft.Xna.Framework.Game
    {
         <summary>
         グラフィックデバイス管理クラス
         </summary>
        private GraphicsDeviceManager graphics = null;
         <summary>
         スプライトのバッチ化クラス
         </summary>
        private SpriteBatch spriteBatch = null;
         <summary>
         スプライトでテキストを描画するためのフォント
         </summary>
        private SpriteFont font = null;
         <summary>
         マウスの状態を記憶
         </summary>
        private MouseState mouseState = Mouse.GetState();
         <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);
            // フォントをコンテンツパイプラインから読み込む
            this.font = this.Content.Load<SpriteFont>("Font");
        }
         <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();
            }
            // マウスの状態を取得
            this.mouseState = Mouse.GetState();
            // 登録された GameComponent を更新する
            base.Update(gameTime);
        }
         <summary>
         描画処理を行うメソッド
         </summary>
         <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param>
        protected override void Draw(GameTime gameTime)
        {
            // 画面を指定した色でクリアします
            this.GraphicsDevice.Clear(Color.CornflowerBlue);
            // スプライトの描画準備
            this.spriteBatch.Begin();
            string stateMessage;
            // マウスの左ボタン
            if (this.mouseState.LeftButton == ButtonState.Pressed)
            {
                stateMessage = "Pressed";
            }
            else
            {
                stateMessage = "Released";
            }
            this.spriteBatch.DrawString(this.font, "LeftButton : " + stateMessage,
                new Vector2(40.0f, 25.0f), Color.White);
            // マウスの右ボタン
            if (this.mouseState.RightButton == ButtonState.Pressed)
            {
                stateMessage = "Pressed";
            }
            else
            {
                stateMessage = "Released";
            }
            this.spriteBatch.DrawString(this.font, "RightButton : " + stateMessage,
                new Vector2(40.0f, 50.0f), Color.White);
            // マウスの中ボタン
            if (this.mouseState.MiddleButton == ButtonState.Pressed)
            {
                stateMessage = "Pressed";
            }
            else
            {
                stateMessage = "Released";
            }
            this.spriteBatch.DrawString(this.font, "MiddleButton : " + stateMessage,
                new Vector2(40.0f, 75.0f), Color.White);
            // マウスのXボタン1
            if (this.mouseState.XButton1 == ButtonState.Pressed)
            {
                stateMessage = "Pressed";
            }
            else
            {
                stateMessage = "Released";
            }
            this.spriteBatch.DrawString(this.font, "XButton1 : " + stateMessage,
                new Vector2(40.0f, 100.0f), Color.White);
            // マウスのXボタン2
            if (this.mouseState.XButton2 == ButtonState.Pressed)
            {
                stateMessage = "Pressed";
            }
            else
            {
                stateMessage = "Released";
            }
            this.spriteBatch.DrawString(this.font, "XButton2 : " + stateMessage,
                new Vector2(40.0f, 125.0f), Color.White);
            // マウスの位置
            this.spriteBatch.DrawString(this.font,
                "Position : " + this.mouseState.X + ", " + this.mouseState.Y,
                new Vector2(40.0f, 150.0f), Color.White);
            // ホイールの回転量
            this.spriteBatch.DrawString(this.font,
                "ScrollWheelValue : " + this.mouseState.ScrollWheelValue,
                new Vector2(40.0f, 175.0f), Color.White);
            // スプライトの一括描画
            this.spriteBatch.End();
            // 登録された DrawableGameComponent を描画する
            base.Draw(gameTime);
        }
    }
}