dimanche 19 avril 2015

Change cursor to cross while form is open (snippet tool)

I am trying to implement a software like "Gyazo", a snippet tool that takes a screenshot. The program begins (nothing appears on the screen, other than the cursor changing), the user clicks to point A, then drags to point B (drawing a transparent rectangle), releases the mouse, then the screenshot gets saved and the program closes.


The way I draw that transparent rectangle is that I re-size and re-position a form with a 30% transparency. So the cursor is never on the form! In order to change the cursor, since it is outside of the form, I tried using:



[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 ?


What other way would you suggest to change the cursor to cross in this case ?


Aucun commentaire:

Enregistrer un commentaire