gpt4 book ai didi

c# - 命令模式和 OnPaint 事件问题

转载 作者:行者123 更新时间:2023-11-30 17:17:38 30 4
gpt4 key购买 nike

我正在尝试使命令模式适应具有撤消功能的简单绘图应用程序。我一直坚持使用 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 操作相同的方法。它像 hell 一样工作。唯一的区别是这个残酷的 OnPaint 方法..

提前感谢您的任何建议!

[编辑] 匆忙中我忘记了将一个引用类型分配给另一个引用类型并不是复制它(创建独立对象),更改此:

this.prevImage = simpleImage.Image;

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

最佳答案

这里的要点不是直接在 Canvas 上作画,而是要有一个代表您的作画的数据结构。然后,您可以向该绘画对象添加一条线, Canvas 的主循环将从数据结构中绘制适当的图形。然后你的 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
}

您的主应用程序将只保留一个列表,并在命令更改绘画集合时重新绘制 Canvas 。

关于c# - 命令模式和 OnPaint 事件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6346317/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com