2011-06-14 60 views
3

我正尝试使用撤销功能将命令模式调整为简单的绘图应用程序。我一直坚持使用OnPaint撤销操作事件。这是代码:命令模式和OnPaint事件问题

[解决]在后

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)); 
    } 
} 

的端部细节它实际上绘制一条线,则执行命令,并将它推入堆栈。我无法实现UNDO的工作。我的意思是。逐步调试我看到对象从堆栈中弹出,然后OnPaint执行。但实际上没有显示“以前的”图像。

我已经阅读了很多网站,并且还从一个codeproject网站的/文章中示例了应用程序。它提供了与TextBox和粗体/斜体操作相同的方法。它像地狱一样工作。唯一的区别是这种残忍的OnPaint方法..

在此先感谢您的任何建议!

[编辑]在匆忙我忘记了分配一个引用类型到另一种未复制它(创建独立对象),改变此:

this.prevImage = simpleImage.Image; 

在几个地方解决了这个问题。现在一切正常..

+0

如果您在得到答案之前解决了问题,您通常会将解决方案发布为您自己问题的答案。然后人们可以对它投票等 – 2011-06-14 17:48:09

+0

哦..这是有道理的;)谢谢你的提示。 – qlf00n 2011-06-14 18:08:50

回答

1

看起来你有一个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的另一个引用。垃圾收集器运行后,您将回到对同一图像对象的引用。撤消然后执行以下操作:

simpleImage.Image = prevImage; 

在这里,你没有改变的图像,只作了Image引用相同的对象是alreafy引用。

尽管我非常同意m0sa,但在此情况下,修复程序是在创建命令时使原始图像变为prevImage a COPY。对于以下我假设图像。克隆()来实现,虽然我从来没有尝试过自己:

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

注意:您可以快速地处理大图像或多个命令运行的内存,如果你使用这种方法。

+0

是的,我明白这种方法的缺点。特别是内存使用。这只是一个学习Command Approach的示例应用程序。我会尝试修改代码并实现给定的建议。谢谢。 – qlf00n 2011-06-14 17:59:27

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  
} 

你的主要应用程序将只是保持一个列表,并重新绘制在画布当命令改变了书画收藏。

+0

谢谢您的意见,我一定会改变我的做法。 – qlf00n 2011-06-14 18:00:23