views:

132

answers:

4

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

A: 

This is complicated, you should at least learn some basics about GDI+.

Benny
A: 

Refer to this: System.Drawing.Example

Ricky
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
Directly drawing to the screen is quite inappropriate.
Hans Passant
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