撤消图片框中的操作以撤消C#.net中绘制的矩形

时间:2012-08-24 07:51:52

标签: .net visual-studio-2010 c#-4.0

在我的项目中有图片框,用户将首先通过点击查看图片按钮选择图片。此图片将按照视图图像按钮前面的文本框中的图像名称。用户现在点击图片框,点数是drwan在选择点之后,用户将点击绘制曲线按钮,绘制平滑曲线,因为我使用了图形类的Drawclosedcurve方法。 我的问题是;假设用户错误地点击了一个点并且他想要UNDO那一点,那么什么是解决方案?

1 个答案:

答案 0 :(得分:0)

您是否考虑使用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; }
}

然后,当用户点击点击时,您可以创建Actionpush就可以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 }

{{1}}

这段代码显然没有经过测试,需要在你的结构中成型,但它应该足够了。