0

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

4

1 回答 1

0

您是否考虑过使用 astack来存储您的操作,例如创建一个类来存储您的操作,然后枚举您的操作类型:

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
}

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

于 2012-08-24T08:06:44.853 回答