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 ?
Edit: Just to clarify, because I tried asking a similar question before which was marked as duplicate of this question and I deleted it, what I am trying to do is similar, but a lot different, because in the answer provided in that question, the solution provided in the answers, is to make a full-screen borderless form, set a screenshot of the desktop as the background of that form, and then crop a rectangle from that. Firstly, that solution "freezes" the screen, since all you see a photo of your desktop while the cropping takes place, and secondly, it is near impossible to handle multi-monitor setups that way. Plus it does extra and unnecessary work.
Make two forms. One for taking the fullscreen snapshot and other for cropping the required area. And after selecting the area, pass the values taken to the form containing the image and save it.
I'll give you an example in which you just add two forms without have to do anything in design
Form_ScreenShot
Now the Semi transparent Selecting form
Form_TransparentSelection
Try putting this on your Program.cs file
this will revert it back to normal whenever the application ends or crashes..
So like changing from Normal to Cross, you can change whatever cursor you want to Arrow
It won't work when you stop the application (Ctrl+F5), because that will skip all the lines. But will work completely after publishing the application.
Setting the cursor to its default value before exiting the application will fix the issue. This can be implemented in the
Form.Closing
event as:Edit: Killing the process through the "Kill Task" option in the task manager will trigger the
Form.Closing
event. There's no way to intercept aTerminateProcess()
call, the one used with the "Kill Process" option.