Click here to Skip to main content
16,003,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello sir,

i want that i could draw a line on the form by using mouse.How could it be done.please help
Posted

I want to add some detail and a fix to the code provided by Simon.

For example, add the following code to the form's constructor:

C#
this.Paint += (sender, eventArgs) => {
    using (Pen pen = Pen(Color.FromArgb(255, 255, 0, 0))) {
        eventArgs.Graphics.DrawLine(pen, startX, startY, endX, endY);
    } //calls Pen.Dispose here, even if exception was thrown
};


Pay attention for the "using" statement. It's used to guarantee disposal of pen, no matter if exception was thrown or not. This construct is a syntactic sugar and is strictly equivalent to placing creation of the pen under try-finally block with disposal operation in the finally part.

Alternatively, you can re-use the pen and other resources. In this case, you should created them in the form's constructor and dispose in Form.Dispose, see http://msdn.microsoft.com/en-us/library/aw58wzka.aspx[^].

You need to initialize geometric parameters startX, startY, endX, endY prior to the call. If you ever need to change the position of the line, make the form fields. To change the position of line, you need to change the values of those fields. Now, how to trigger re-drawing of the picture? You need to cause sending the event WM_PAINT to the form. This is done using Form.Invalidate (inherited from Control.Invalidate. You can improve performance using Invalidate with parameters (Rectangle or Region) to invalidate just some part of the scene.

—SA
 
Share this answer
 
v5
Comments
Simon Bang Terkildsen 21-Aug-11 22:39pm    
I'm a bit embarrassed that I omitted the using. And I should probably have been a bit more detailed, such as takling about Invalidate.
+5 for remedying my incomplete answer, thank you.
Sergey Alexandrovich Kryukov 21-Aug-11 22:42pm    
Thank you very much, Simon. Please don't be embarrassed too much: by accepting criticism constructively you show real decency.
My respect,
--SA
BillWoodruff 21-Aug-11 23:09pm    
Great answer SAK +5
Sergey Alexandrovich Kryukov 21-Aug-11 23:42pm    
Thank you, Bill. By the way, I've fixed a bug afterwards.
--SA
Handle the Forms Paint event and then you can draw like so:
C#
Pen pen = new Pen(Color.FromArgb(255, 255, 0, 0));
e.Graphics.DrawLine(pen, _startX, _startY, _endX, _endY);

That's the draw a line part. Now you just need to track the mouse the MouseDown, MouseUp, MouseMove event might help you with that.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Aug-11 20:59pm    
As OP asks the question about drawing, he apparently need help on how to handle this event. Unfortunately, you also have a bug in your code: you fail to dispose pen. This bug is serious enough. So, I can only vote 3, sorry.

Please see my solution which explains things.
--SA

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