English 中文(简体)
指挥模式和安顿活动问题
原标题:Command Pattern and OnPaint Event issue

我正试图使指挥模式适应简单的油漆应用,使之具有独创的功能。 而我用OnPaint st。 特别活动。 这是法典:

<><><><><><>> > >

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

它实际上走了一条线,执行指挥,将其推向 st。 我无法完成裁军部的工作。 我指的是。 逐步递减 我看到该物体的pop,然后看OnPaint。 但没有实际显示以前的形象。

我已经阅读了许多网站,我还从一个代码项目现场抽取了数据。 该表对<代码>TextBox和Bold /Ialicize的操作提出了同样的做法。 它像头盔一样运作。 唯一的区别是这一残酷的<编码>OnPaint方法。

Thanks in advance for any advices!

<>strong>[EDIT] in a rush i forgot thatsigning one reference category to another is notplicaing it (creating independent Object), changing this:

this.prevImage = simpleImage.Image;

在少数地方解决这一问题。 现在一切都奏效。

最佳回答
问题回答

这里的意思不是直接在信道上画面,而是有一个代表你绘画的数据结构。 然后,你将添加一条线,而信道的主要航道将从数据结构中得出适当的图表。 那么,你的手法/单单单是需要操纵数据结构,而不是绘画。

你们需要这样的东西:

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    
}

你的主要应用只是保留一份清单,在指挥改变油漆收集工作时,重新铺设信道。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签