【问题标题】:Undo operation in picturebox to undo drawn rectangles in C#.net在图片框中撤消操作以撤消 C#.net 中绘制的矩形
【发布时间】:2023-03-23 23:43:01
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多