Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all
i am making a screen area capture program , now my problem that i failed to draw on the screen the selection rectangle because when i hide the form to begin to define the area , i could not catch mouse events (down , up , move ) to be able the draw the rect and to get the mouse positions
so any tips for that
thanks all bye
Posted

You may need to use Global Mouse hook for this. This[^] should help. I am not too much aware of this so won't be much of help here.

Although for drawing on the desktop, you can use this:
[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
static extern void ReleaseDC(IntPtr dc);


In the method:

C#
IntPtr yourDesktop = GetDC(IntPtr.Zero);
using (Graphics graphics = Graphics.FromHdc(yourDesktop)) {
graphics.DrawRectangle(Brushes.Black, mouseDownX, mouseDownY, Cursor.Position.X, Cursor.Position.Y);
}
ReleaseDC(yourDesktop);


These are the pieces that can help you. Now this is how the code should work:

1. Track the global mouse down and set those coordinates to (mouseDownX, mouseDownY). Also start a System.Timers.Timer in the same method. Try to keep the interval as low as possible*.
2. In the Elapsed event, use the graphics related code I have placed above.
3. Track the global mouse up and set that as final (X,Y). This point should replace the Cursor.Position coordinates.

Once the drawing thing is done, stop the timer so that it doesn't draw things anymore.

*Any other window movement will cause the repaint of desktop so to provide a smoother drawing, you should draw regularly.
 
Share this answer
 
Comments
Olivier Levrey 22-Apr-11 10:17am    
Good answer, my 5. But I think it is enough to set the Capture property of the form to true to receive mouse inputs.
dan!sh 22-Apr-11 10:20am    
I wasn't aware of Capture property. Cool! Should make OP's life easier. :)
dan!sh 22-Apr-11 10:32am    
Only when the Form window is the foreground window.
Olivier Levrey 22-Apr-11 10:37am    
So this will not make OP's life easier after all! ;)
To capture the mouse, you just need to set the Capture property of your form to true. Don't forget to set it back to false once you are done.

To paint on the screen, refer to d@nish's answer.
 
Share this answer
 
Comments
dan!sh 22-Apr-11 10:32am    
This will work only till the time the Form is the foreground window. If the user minimizes the window, capture won't work.
Olivier Levrey 22-Apr-11 10:36am    
OK I didn't know that. I used this property in my applications many times but it is true I have never used it to capture clicks outside of the main window. My mistake.
Could you make the window transparent while you're drawing the selection rectangle?
 
Share this answer
 
Comments
AhmedOsamaMoh 22-Apr-11 9:23am    
i want it like professional software , if i made it 100% transparent it acts as if i hide it !!
Marc A. Brown 22-Apr-11 10:31am    
Sorry that didn't help -- I didn't know the window would behave that way at 100% transparency. Looks like you've got some other good answers though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900