问题描述
|
我正在尝试使命令模式适应具有撤消功能的简单绘画应用程序。而且我在撤销操作上一直坚持
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,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));
}
}
它实际上画一条线,执行命令并将其压入堆栈。我无法实现工发组织的工作。我的意思是。分步调试我看到对象从堆栈中弹出,然后执行“ 0”。但实际上没有显示“上一个”图像。
我已经阅读了许多站点,并且还从codeproject站点的/文章之一中获得了示例应用程序。对于ѭ3和加粗/斜体运算,它也提供了相同的方法。它像地狱一样运作。唯一的区别是这种残酷的“ 0”方法。
在此先感谢您的任何建议!
[编辑]我急于忘记将一种引用类型分配给另一种引用类型不会复制它(创建独立的对象),更改了以下内容:
this.prevImage = simpleImage.Image;
在少数地方解决了这个问题。现在一切正常。
解决方法
看来您有混叠问题。在ѭ6中,您在操作之前先提取对图像的引用并将其存储为:
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
上面的内容使ѭ9成为不必要的Image引用。但是,您的命令中仍然还有对Image的引用。垃圾收集器运行之后,您将回到对同一图像对象的引用。然后,撤消执行以下操作:
simpleImage.Image = prevImage;
在这里,您没有更改Image,仅使ѭ11引用了一直引用的同一对象。
尽管我非常同意m0sa,但在这种情况下,解决的方法是在创建命令时将prevImage
作为原始图像的副本。对于以下内容,我假设实现了Image.Clone(),尽管我自己从未尝试过:
this.prevImage = simpleImage.Image.Clone();
注意:如果使用此方法,您可能会很快耗尽大图像或许多命令的内存。
, 这里的重点不是直接在画布上绘画,而是要有一个代表您绘画的数据结构。然后,您需要向此绘画对象添加一条线,并且画布的主循环将从数据结构中绘制适当的图形。然后,您的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
}
您的主应用程序将仅保留一个List,并在命令更改绘画集合时重新绘画画布。