Problem with Command Pattern and OnPaint

I am trying to adapt a Command Pattern to a simple paint with a undo function. And I'm stuck with OnPaintEvent on Undo operations . This is the code:

[SOLVED] details at the end of the message

interface ICommand {
    void Execute();
    void UnExecute();
}

class DrawLineCommand : ICommand {
    private SimpleImage simpleImage;
    private Image prevImage;
    public DrawLineCommand(SimpleImage simpleImage) {
        this.simpleImage = simpleImage;
        this.prevImage = simpleImage.Image;
    }
    public void Execute() {
        simpleImage.DrawLine();
    }
    public void UnExecute() {
        simpleImage.Image = prevImage;
    }
}

class CommandManager {
    private Stack undoStack = new Stack();
    public void ExecuteCommand(ICommand command) {
        command.Execute();
        undoStack.Push(command);
    }
    public void UnExecuteCommand() {
        if (undoStack.Count > 0) {
            ICommand command = (ICommand)undoStack.Pop();
            command.UnExecute();
        }
    }
}

class SimpleImage {
    private Point startPoint;
    private Point endPoint;
    private PictureBox pictureBox;
    public SimpleImage(PictureBox pictureBox) {
        this.pictureBox = pictureBox;
        pictureBox.Paint += new PaintEventHandler(pictureBox_Paint);
    }
    void pictureBox_Paint(object sender, PaintEventArgs e) {
        // this code shows the line during drawing
        // this code is under "if operation == drawLine" block
        Graphics graphics = e.Graphics;
        graphics.DrawLine(Pens.Red, startPoint, endPoint);

        // how can i refresh picturebox after undo operation?
        // "if operation == undo" then ??
    }
    public void DrawLine() {
        // this code actually saves finally drawn line
        Image img = Image;
        Graphics graphics = Graphics.FromImage(img);
        graphics.DrawLine(Pens.Red, startPoint, endPoint);
        Image = img;
    }

    public void Invalidate() {
        pictureBox.Invalidate();
    }
    public Image Image {
        get { return pictureBox.Image; }
        set { pictureBox.Image = value; }
    }
    public Point StartPoint {
        get { return startPoint; }
        set { startPoint = value; }
    }
    public Point EndPoint {
        get { return endPoint; }
        set { endPoint = value; }
    }
}

public partial class FormMain : Form {
    private PictureBox pictureBox;
    private SimpleImage simpleImage;
    private CommandManager commandManager;
    public FormMain() {
        InitializeComponent();
        simpleImage = new SimpleImage(this.pictureBox);
        commandManager = new CommandManager();
    }
    void pictureBox_MouseDown(object sender, MouseEventArgs e) {
        if (e.Button != MouseButtons.Left)
            return;

        simpleImage.StartPoint = e.Location;
    }
    void pictureBox_MouseMove(object sender, MouseEventArgs e) {
        if (e.Button != MouseButtons.Left)
            return;

        simpleImage.EndPoint = e.Location;
        simpleImage.Invalidate();
    }
    void pictureBox_MouseUp(object sender, MouseEventArgs e) {
        simpleImage.Invalidate();
        commandManager.ExecuteCommand(new DrawLineCommand(simpleImage));
    }
}

He actually draws a line, she executes a command and pushes it on the stack. I can not achieve the work of UNDO. I mean. Phased debugging. I see how an object crashes out of the stack and then executes OnPaint. But no "previous" image is actually shown.

I have read many sites and I have an example application from one of the codeproject sites / articles. It presents the same approach with operations TextBoxand Bold / Italicize. He works like hell. The only difference is this cruel method OnPaint.

!

[EDIT] , ( ), :

this.prevImage = simpleImage.Image;

. .

+3
2

, aliasing. DrawLineCommand :

this.prevImage = simpleImage.Image;

. , :

Image img = Image; // Now a third reference to the same image object
Graphics graphics = Graphics.FromImage(img);
graphics.DrawLine(Pens.Red, startPoint, endPoint);
Image = img; // and you set the Image reference back to the same object

img . Image . . Undo :

simpleImage.Image = prevImage;

, Image , .

m0sa, prevImage a COPY . , Image.Clone() , :

this.prevImage = simpleImage.Image.Clone();

. .

+1

, , , . , . do/undo , .

- :

interface IPaintable // intarface for Lines, Text, Circles, ...
{
    void OnPaint(Image i); // does the painting
}

interface IPaintableCommand // interface for commands 
{
    void Do(ICollection<IPaintable> painting); // adds line/text/circle to painting
    void Undo(ICollection<IPaintable> painting);  // removes line/text/circle from painting    
}

, .

+1

All Articles