您是否考虑过使用stack 来存储您的操作,例如创建一个类来存储您的操作,然后枚举您的操作类型:
enum ActionType
{
DrawPoint = 1,
DrawCurve = 2
}
public class Action
{
public int X { get; set; }
public int Y { get; set; }
public ActionType Action { get; set; }
}
然后,每当用户单击以指出一个点时,您就可以从这里创建一个 Action 和 push 它到您的 Stack 上,如下所示:
Stack eventStack = new Stack<Action>();
// capture event either clicking on PictureBox or clicking on Curve button
// and convert to action
Action action = new Action() {
X = xPosOnPictureBox,
Y = yPosOnPictureBox,
ActionType = ActionType.DrawPoint
};
eventStack.push(action);
然后您可以根据堆栈继续渲染您的图片框。当您想撤消时,只需 pop 将最近的操作从堆栈中移除,然后从堆栈中剩余的事件中重新绘制您的 PictureBox。
protected void btnUndo_Click(object sender, EventArgs e)
{
var action = eventStack.pop();
// ignore action as we just want to remove it
// now redraw your PictureBox with what's left in the Stack
}
这段代码显然没有经过测试,需要在你的结构中成型,但应该足够了。