I'm trying to create a simple drawing application with undo and redo features. I assume you can add what you are drawing into a list and calling upon the list to draw everything. Then undoing should just remove the last added item and redraw everything again. The problem is, how do I add what I've drawn into a list and use that list to undo?
I'm using the bitmap redraw method.
This is how I draw:
Point start, end;
bool painting;
private List<PointF> myPoints = new List<PointF>();
private void pnlMain_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
painting = true;
}
private void pnlMain_MouseUp(object sender, MouseEventArgs e)
{
painting = false;
}
private void pnlMain_MouseMove(object sender, MouseEventArgs e)
{
if (painting == true)
{
end = e.Location;
g.DrawLine(p, start, end);
myPoints.Add(e.Location);
pnlMain.Refresh();
start = end;
}
}
private void btnUndo_Click(object sender, EventArgs e)
{
g.Clear(cldFill.Color);
if (myPoints.Count > 2)
{
myPoints.RemoveAt(myPoints.Count - 1);
g.DrawCurve(p, myPoints.ToArray());
}
pnlMain.Refresh();
//This works but you have to spam it to get rid of
//a line and does some weird connections.
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…