Custom mouse cursor not drawing correctly

I’m using Monogame 3.7.1.189 and I’m trying to set a custom mouse cursor using MouseCursor.FromTexture2D, but the texture comes out scrambled.

Here is the relevant code.

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = “Content”;
this.IsMouseVisible = true;
}

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        _texture = Content.Load<Texture2D>("finger_pointer");

        Mouse.SetCursor(MouseCursor.FromTexture2D(_texture, 0, 0));
        // TODO: use this.Content to load your game content here
    }

When I run the game, the cursor looks like this:

When it should look like this:
finger_pointer

How can I get the pointer to display correctly?

I have never done this approach, but have some clues and ideas…

What I and others have done in the past, is just draw a sprite on the position of the mouse, while disabling the actual mouse cursor draw… Do it like any other sprite.

But to get exactly what you want, a “hardware” cursor,
you might need to verify that the pointer texture you have selected complies with / adheres to any format rules, like resolution, image size, etc…

Your glitch looks to me, a lot like it is expecting other dimensions for the image, which causes that kind of distortion.

1 Like

29x35

It is not square… ^2

c# - How to override maximum 32x32 mouse size in Windows like this program can - Stack Overflow

I am currently doing this:

Mouse.SetCursor(MouseCursor.FromTexture2D(Content.Load<Texture2D>(PATH_TO_IMAGE), 0, 0));

Replace PATH_TO_IMAGE with yours.

This seemed to do the trick. I just resized my cursor have a 32px height and added some blank space to get it to 32px width, since the important part is the upper left corner of the texture. Thanks a ton.

1 Like