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

air*_*n19 4 .net c# mouse xna cursor

我目前正在XNA开发一款游戏.我想在游戏中添加一个游标(不是标准的Windows游标).我已经将精灵添加到我的内容文件夹中.我有一个找到鼠标位置的方法,但我不知道如何在窗口中显示光标.

这是我用来查找鼠标位置的方法(我在Game1类的开头实例化了一个"MouseState"类):

public int[] getCursorPos()
    {
        cursorX = mouseState.X;
        cursorY = mouseState.Y;

        int[] mousePos = new int[] {cursorX, cursorY};
        return mousePos;
    }
Run Code Online (Sandbox Code Playgroud)

pek*_*pek 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

  • Content.Load <>不接受扩展,对吧? (2认同)

小智 5

如果你喜欢加载Windows光标(ani,cur),你可以看到:http: //allenwp.com/blog/2011/04/04/changing-the-windows-mouse-cursor-in-xna/