Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all i want to drawtline while mouse moving the function drawlines in c# accept array of object point
i want to add the e.location to this array at every time the posion changed
how can i do it ?
Posted
Comments
joshrduncan2012 11-Feb-14 10:41am    
What have you tried? Where are you stuck?

The rendering concept in System.Windows.Forms used with System.Drawing looks like this: you need to do all the rendering in response to the WM_PAINT windows messages, which is done by handling the event System.Windows.Forms.Control.Paint or in your overridden virtual method System.Windows.Forms.Control.OnPaint. Therefore, you need to keep all your graphics data in memory, in the form of some model, and render it all from memory on events. To prevent flicker, use double buffering. If you data is modified and needs immediate rendering, the event is stimulated by calling System.Windows.Forms.Control.Invalidate.

For more detail, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

For some related advanced topics, see also:
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

—SA
 
Share this answer
 
Add a mousemove event and a variable where you save the last point:

something like this (not complete, just to get the idea):
C#
Graphics grphx;
Pen MyPen;
Point OldPoint;
private void OnMouseMove(object sender, MouseEventArgs e)
{
  grphx.DrawLine(MyPen, OldPoint.X, OldPoint,Y, e.X, e.Y);
  OldPoint = new Point(e.X, e.Y);
}
 
Share this answer
 
Comments
fady_hasnaa 12-Feb-14 8:38am    
thanks ,
but i have other question
this is my code

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
//drwa line
Point oldpoint = new Point();

Graphics ex = CreateGraphics();


if (e.Button == MouseButtons.Left)
{
Pen myPen = new Pen(Color.Red);
ex.DrawLine(myPen, oldpoint.X, oldpoint.Y, e.X, e.Y);
oldpoint = e.Location;


}
//erase line
else if (e.Button == MouseButtons.Right)
{
Point secondold=new Point ();
Pen o = new Pen(this.BackColor);
Graphics er = CreateGraphics();
er.DrawLine(o, secondold.X, secondold.Y, e.X, e.Y);
secondold = e.Location;


}

}

private void Form1_Load(object sender, EventArgs e)
{
this.ResizeRedraw = true;
}

private void Form1_Resize(object sender, EventArgs e)
{
Invalidate();
}


when trying to resize the form every thind erased
i wrote this.resizeredraw= true in resize event but nothing happen
did you have any idea about this ?
Johannes Bildstein 12-Feb-14 9:15am    
Your problem with the straight line: you have to put the oldpoint variable outside of the MouseMove event.
And about the resize: see the second solution from Sergey.
fady_hasnaa 12-Feb-14 8:40am    
also this is a strait line , i want the line is drwaning as the mouse move not as strait

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