I have a WinForms application with a user control in which I change the cursor dynamically based on its location. I create my custom cursors as static members of the user control, like this:
private static Cursor _openHandCursor = new Cursor( Properties.Resources.openHand.Handle );
Then in the OnMouseMove override I set the appropriate cursor, like this:
this.Cursor = <some condition> ? _openHandCursor : Cursors.Default;
When I start the application, it works correctly. However, after a couple of cursor changes (around 20) it sticks with the default (arrow) bitmap and does not change any more. But the oddest thing is that after this happens, I check the Handle property of the current Cursor value and it is equal to the handle of the _openHandCursor object! So it seems the value of the property is still being set correctly but the cursor bitmap on the screen doesn’t get updated. Any suggestions about this weird behaviour?
Change
to
When you create
Cursorobject (new Cursor (<Handle>)), it just reuses the same handle as cursor from resources.Since you don’t store reference to original cursor (created by
Properties.Resources.openHand.Handleproperty getter), it is going to be garbage collected.Cursorclass defines finalizer, which destroys handle by callingDestroyCursor(), making it invalid. NowCursor, which you created, has the same handle, but it does not know that handle is not valid anymore.OS just uses default cursor when you try to set an invalid one.