Please help, I have 5 days left :(
GDI : How to delete a line on right click
I have a homework, its aplication for drawing lines on the form. I know how to draw them, but I also have to delete them on right click. For example I click on the form with left click, drag a line, on release paint the line, but if I press right click on it, line will be removed. Does anyone have a any clue how to delete lines ???
Jason AkkermanPosted Nov 11, 2010, 2:15 PM
{
Boolean drag;
Point start;
Point end;
public Form1()
{
InitializeComponent();
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
drag = true;
start.X = e.X;
start.Y = e.Y;
}
else
{
drag = false;
Graphics g = Graphics.FromHwnd(this.Handle);
g.Clear(this.BackColor);
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (drag)
{
end.X = e.X;
end.Y = e.Y;
Graphics g = Graphics.FromHwnd(this.Handle);
g.DrawLine(new Pen(Color.Black, 2), start, end);
}
base.OnMouseUp(e);
}
}
That is a simple answer. If right clicking the form is just suppose to erase the last line drawn then you need to keep track of all the lines that have been drawn. I suggest a generic collection (or better yet a System.Collections.Stack) of a custom structure that contains two System.Drawing.Points. Capture the right click and remove the last line from the collection. Call the Invalidate() method of your form to force a redraw. Overload the OnPaint() method to draw each line in your collection.
Santhosh NPosted Jan 7, 2010, 12:20 AM
you can check here for a sample on how to do this
http://support.microsoft.com/kb/314945
Mahesh ChandPosted Jan 6, 2010, 9:06 PM