Cancel button for System.Drawing?

I make the image editor curious for my own pleasure and wondered how can I make the cancel button to undo the last paints I made? I want to know how I would do it, a tutorial or sample code would be good, or at least something would point me in the right direction.

Thank!

+3
source share
3 answers

heh cancel is not really as difficult as it seems. The magic here is that you have to record each action as an object that is listed in a list or queue, f.ex, the user draws a line, so your entry may look like x, y start poing and x, y end point or it self object, which has a Draw () method, so canceling something will just delete this object.

in code that might look something like this:

interface IDrawObject
{
     public void Draw();
}

class Line : IDrawObject
{
    private Point _startP;
    private Point _endP;

    public Line(Point startPoint; Poing endPoint)
    {
        _startP = startPoint;
        _endP = endPoint;
    }

    public void Draw()
    {
        //* call some generic draw processor to perform the action with your given parameters.
    }
}

class Rectangle : IDrawObject
{
    //* your code.
    public void Draw()
    {
         //* call some generic draw processor to perform the action with your given parameters.
    }
}

//* then in your code, you could have something like this.
List<IDrawObject> myObjectsINeedToDraw = new List<IDrawObject>();
myObjectsINeedToDraw.Add(new Line(new Point(0, 0), new Point(10, 10));

foreach(IDrawObject objectToDraw in myObjectsINeedToDraw)
{
    //* will draw your object.
    objectToDraw.Draw();
}

//* in this way you will have unlimited history of your objects, and you will always can remove object from that list.
+3
source

http://en.wikipedia.org/wiki/Command_pattern

Command objects are useful for implementing:

     , . , command () .

: , , "", , , .. , .

, , .

, , , - . , , .

, . 5 , , , . 10 , - .

, /, , .

+1

One of the main ways is to keep a stack that will contain every new stroke that you make.

When you press the cancel button, pull the stack and discard your change (depending on the features of your application, buy, maybe repaint it in the background color?)

+1
source

Source: https://habr.com/ru/post/1747418/


All Articles