Adding a custom cursor in XNA/C#?

2019-01-20 11:58发布

I am currently developing a game in XNA. I would like to add a cursor (not the standard Windows one)to the game. I have already added the sprite to my contents folder. I have a method for finding the position of the mouse, but I don't know how I should go about displaying the cursor in the window.

Here is the method I am using to find the position of the mouse (I instantiated a "MouseState" class in the beginning of the Game1 class):

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

        int[] mousePos = new int[] {cursorX, cursorY};
        return mousePos;
    }

3条回答
狗以群分
2楼-- · 2019-01-20 12:02

If you like load a Windows Cursor (ani, cur) you can see: http://allenwp.com/blog/2011/04/04/changing-the-windows-mouse-cursor-in-xna/

查看更多
你好瞎i
3楼-- · 2019-01-20 12:03

you can also use GUI and manually load a windows cursor to replace the default cursor

查看更多
聊天终结者
4楼-- · 2019-01-20 12:07

Load a Texture2D for the cursor image and simply draw it.

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();
  }
}
查看更多
登录 后发表回答