我正在尝试使用撤消功能将Command Pattern改编为简单的绘图应用程序。我在撤消操作时遇到了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
。但实际上没有显示“上一个”图像。
我已阅读过很多网站,而且我还有一个代码项目网站/文章的示例应用程序。它使用TextBox
和Bold / Italicize操作提供相同的方法。它像地狱一样工作。唯一的区别是这种残忍的OnPaint
方法..
提前感谢任何建议!
[编辑] 匆忙我忘记了将一个引用类型分配给另一个不是复制它(创建独立对象),改变了这个:
this.prevImage = simpleImage.Image;
在很少的地方解决了这个问题。现在一切都有效..
答案 0 :(得分: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
}
您的主应用程序只会保留一个List,并在命令更改绘图集时重新绘制画布。
答案 1 :(得分: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的不必要引用。但是,您仍然在命令中有另一个对Image的引用。垃圾收集器运行后,您将返回到对同一图像对象的引用。撤消然后执行以下操作:
simpleImage.Image = prevImage;
在这里你没有改变Image,只是让Image
引用了它所引用的同一个对象。
虽然我非常同意m0sa,但在这种情况下,修复是在创建命令时使prevImage
成为原始图像的 COPY 。对于以下内容,我假设Image.Clone()已实现,但我自己从未尝试过:
this.prevImage = simpleImage.Image.Clone();
注意:如果使用此方法,您可能会因大图像或许多命令而快速耗尽内存。