Using MouseCursor class to make a cursor that changes colour based on its position

I am trying to create a circular cursor that changes colour based on its position. I create a graphics context (using Graphics class) and pass it a reference to an Image ( on intialization) and then pass another reference to that image to a MouseCursor instance and then use the graphics class to draw the circle with appropriate colour etc. but all I am getting is a black square. I am passing a reference to this cursor with setCursor(). Is this approach valid? I figure I could achieve a similar sort of effect using a component that follows the cursor and hiding the cursor but really I am trying to make a custom cursor.

heres the method I’m drawing to the image that I initialized my MouseCursor object with.

    void CursorFactory::drawCircularCursor()
        Graphics graphicsContext(cursorImage);
        float radius = static_cast<float>(cursorWidth / 2);
        graphicsContext.fillEllipse(juce::Rectangle<float>(radius, radius, radius, radius));
        graphicsContext.drawEllipse(juce::Rectangle<float>(radius, radius, radius, radius), borderThickness);

I suspect you will have to re-assign the MouseCursor via setCursor() after changing the image content. Also, note that your image may be scaled to fit a standard size and/or converted to back&white, depending on your system (according to the documentation in that header file).

Yes it’s not shown here but following a call to drawCircleCurosr() I call setCursor(getCustomeCursor()) function. The getter returning a reference to the cursor.

I did see that. I am on a windows machine and I believe you can do coloured cursors.


Coloured custom coursors definitely exist and you can even use transparent colours. There is indeed a size limit: 32x32pxls. But that’s no reason for drawing your cursor all black. I don’t know what fillColour and borderColour are here. I suppose they change on different states? To be sure that states don’t interfere with your drawing try clearing the image before drawing to it. cursorImage.clear(cursorImage.getBounds(), 0, 0, false) or something like that. Also if it’s a plugin, studio one doesn’tdraw custom cursors, so i hope it’s not an essential feature

Just draw a coloured circle centred at the mouse position in paintOverChildren()?

Yea borderColour is pretty much a static colour but in theory it could change. fillColour is changed based on the mouse position both are member variables. I did try clearing it even to something other than black and nothing changed. It’s like once I initialize the MouseCursor instance with with my image it doesn’t change after that and that initial image is just a “cleared”. I must be doing something wrong…hmm I took a break from the project but will be returning to it tomorrow. Will post what the problem was once I solve it!

P.s. thanks for all the responses everyone!

Yea I was thinking something like this should work, but was trying to do it with the MouseCursor class.

here, have my latest plugin’s project, where i also used a custom cursor. maybe you can find something in it that makes the difference to your project. NEL-19/Space.h at master · Mrugalla/NEL-19 · GitHub

here’s a bit of navigation in this rather huge file:
line 573: the array that holds the mouseCursors.
line 589: importing an image as a mouseCursor and editing it a bit for the other cursors.
line 607: applying the mouseCursors and sending pointers to them to other components
line 480: calling makeCursors in the initializer list
line 520: calling setCursors almost at the end of the constructor


Thanks to all those that helped I did solve this problem the solution (or workaround) was similar to along the lines of what @HowardAntares suggested but seems I had to take it one step further by creating a new MouseCursor with a reference to the (updated) image. I don’t quite understand why I had do this since MouseCursor is intialized with a const reference and the new MouseCursor was intialized with the same image…bit of a head scratcher. Probably missing something here.