how can i enable user to draw line in the form in c# user draw with left mouse button and earse the line if he draw with the right mouse button
views:
132answers:
4
A:
Not that complicated, a quick example... I have not included the checks here.
Graphics g = null; // initialize in Form_Load with this.CreateGraphics()
Point lastPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
g.DrawLine(Pens.Blue, lastPoint, e.Location);
lastPoint = e.Location;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
The above is an example to show how one can draw on a Form. Ideally, you should put all the line coordinates in a collection and draw that using DrawLines(). Then use the Graphics::DrawLines() in the Form::OnPaint. When the right mouse is clicked, just clear the point collection and force a redraw.
A9S6
2010-03-14 13:48:14
Directly drawing to the screen is quite inappropriate.
Hans Passant
2010-03-14 14:49:31
A:
Ultimately you may want to contain all the drawing/erasing action within a specific control and manage its redraw/invalidation status coherently but A9S6's answer will surely get you started and enjoy some GDI+ drawing ;)
Jelly Amma
2010-03-15 09:03:32