0

ささいなXNA「ゲーム」でカーソルをIBeamに変更するには、次のように更新します。

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

        // TODO: Add your update logic here

        if (Keyboard.GetState().IsKeyDown(Keys.A))
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Arrow;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.I))
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.IBeam;
        }

        base.Update(gameTime);
    }

'I'を押すと、マウスはIBeamカーソルに変わりますが、マウスを動かすとすぐに矢印に戻ります。デフォルトのWindowsIBeamのままにする方法はありますか、それともカスタムカーソルを作成して追跡する必要がありますか?

[編集]カーソルを1フレームごとに設定すると、マウスを動かしたときにカーソルがちらつくことも指摘しておく必要があります。XNA(またはWindows)は、フレームごとにカーソルを矢印に内部的にリセットしているようですか?

4

2 に答える 2

3

手動で描画する前に、基になるSystem.Windows.Forms.FormのCursorプロパティを設定してみます。

Form f = (Form)Control.FromHandle(Game.Window.Handle);
f.Cursor = System.Windows.Forms.Cursors.IBeam;

現在XNAをインストールしていないので、これが機能するかどうか、または永続的であるかどうかはわかりませんが、なぜ機能しないのかわかりません。

于 2011-01-22T22:25:41.820 に答える
2

手動で描画する必要があるようです。それほど難しくはありません。カーソルの位置にSpriteBatchを介してスプライトを描画するだけです。

// ex.
protected override void Draw(GameTime gameTime)
{
    // Other stuff...

    base.Draw(gameTime);

    // Retrieve mouse position
    MouseState mState = Mouse.GetState();
    Vector2 mousePos = new Vector2(mState.X, mState.Y);

    // Use this instead to optionally center the texture:
    // Vector2 mousePos = new Vector2(mState.X - cursorTexture.Width / 2, mState.Y - cursorTexture.Height / 2);

    // Draw cursor after base.Draw in order to draw it after any DrawableGameComponents.
    this.spriteBatch.Begin(); // Optionally save state
    this.spriteBatch.Draw(cursorTexture, mousePos, Color.White);
    this.spriteBatch.End();
}

そして、カーソル画像を抽出するためのコードを次に示します。System.Drawingへの追加の参照が必要になることに注意してください。

protected override void LoadContent()
{
    // Other stuff...

    // The size of the cursor in pixels.
    int cursorSize = 32;

    // Draw the cursor to a Bitmap
    Cursor cursor = Cursors.IBeam;
    Bitmap image = new Bitmap(cursorSize, cursorSize);
    Graphics graphics = Graphics.FromImage(image);
    cursor.Draw(graphics, new Rectangle(0, 0, cursorSize, cursorSize));

    // Extract pixels from the bitmap, and copy into the texture.
    cursorTexture = new Texture2D(GraphicsDevice, cursorSize, cursorSize);
    Microsoft.Xna.Framework.Graphics.Color[] data = new Microsoft.Xna.Framework.Graphics.Color[cursorSize * cursorSize];
    for (int y = 0; y < cursorSize; y++)
    {
        for (int x = 0; x < cursorSize; x++)
        {
            System.Drawing.Color color = image.GetPixel(x, y);
            data[x + y * cursorSize] = new Microsoft.Xna.Framework.Graphics.Color(color.R, color.G, color.B, color.A);
        }
    }
    cursorTexture.SetData<Microsoft.Xna.Framework.Graphics.Color>(data);
}

これは私の頭のてっぺんから外れていることを覚えておいてください-動作することが保証されていません。ただし、基本的な考え方はそこにあります。

于 2010-07-13T00:10:20.570 に答える