samedi 18 avril 2015

Change cursor while form is open

I have seen a lot of questions about this, but I haven't seen any solid solution yet. I am not trying to change my cursor to a custom .cur file, or overwrite any registrys.


What I want to do is change the cursor to Cursors.Cross while my form is open. That means that the cursor will have to be Cross even outside the form, while the form is open.


What I have tried:



[DllImport("user32.dll")]
static extern bool SetSystemCursor(IntPtr hcur, uint id);
[DllImport("user32.dll")]
static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
private int CROSS = 32515;
private const uint NORMAL = 32512;

//and then calling
SetSystemCursor(LoadCursor(IntPtr.Zero, CROSS), NORMAL);


The problem I had with this code is that it is really buggy. When the form closes, the cursor doesn't change back to normal. I don't know how to revert the cursor properly. Also, reverting the cursor when the form is closed from the task manager will be impossible, correct ? Any ideas on how to achieve that without the "revert cursor back to normal" problems ?


Aucun commentaire:

Enregistrer un commentaire