在XNA/C#中添加自定义光标?

4
我目前正在开发一个XNA游戏。我想在游戏中添加一个光标(不是标准的Windows光标)。我已经将精灵添加到我的内容文件夹中。我有一种方法可以找到鼠标的位置,但我不知道该如何在窗口中显示光标。
以下是我用来查找鼠标位置的方法(我在Game1类的开头实例化了一个“MouseState”类):
public int[] getCursorPos()
    {
        cursorX = mouseState.X;
        cursorY = mouseState.Y;

        int[] mousePos = new int[] {cursorX, cursorY};
        return mousePos;
    }
2个回答

15

加载一个 Texture2D 用于光标图像,然后简单地绘制它。

class Game1 : Game 
{
  private SpriteBatch spriteBatch;

  private Texture2D cursorTex;
  private Vector2 cursorPos;


  protected override void LoadContent() 
  {
    spriteBatch = new SpriteBatch(GraphicsDevice);
    cursorTex = content.Load<Texture2D>("cursor");
  }

  protected override Update(GameTime gameTime() {
    cursorPos = new Vector2(mouseState.X, mouseState.Y);
  }

  protected override void Draw(GameTime gameTime)
  {
    spriteBatch.Begin();
    spriteBatch.Draw(cursorTex, cursorPos, Color.White);
    spriteBatch.End();
  }
}

正确的,它不需要扩展名。已修复。谢谢。 - pek

1

你也可以使用GUI并手动加载Windows光标来替换默认光标


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接