c# - 绘制多条徒手折线或曲线图 - 添加撤消功能

标签 c# winforms graphics gdi+

我正在尝试创建一个具有撤消和重做功能的简单绘图应用程序。我假设您可以将要绘制的内容添加到列表中,然后调用列表绘制所有内容。然后撤消应该只删除最后添加的项目并重新绘制所有内容。问题是,如何将我绘制的内容添加到列表中并使用该列表撤消?

我正在使用位图重绘方法。 我是这样画的:

    Point start, end;
    bool painting;
    private List<PointF> myPoints = new List<PointF>();

    private void pnlMain_MouseDown(object sender, MouseEventArgs e)
    {
        start = e.Location;
        painting = true;
    }

    private void pnlMain_MouseUp(object sender, MouseEventArgs e)
    {
        painting = false;
    }

    private void pnlMain_MouseMove(object sender, MouseEventArgs e)
    {
        if (painting == true)
        {
            end = e.Location;
            g.DrawLine(p, start, end);
            myPoints.Add(e.Location);
            pnlMain.Refresh();
            start = end;
        }
    }

    private void btnUndo_Click(object sender, EventArgs e)
    {
        g.Clear(cldFill.Color);
        if (myPoints.Count > 2)
        {
            myPoints.RemoveAt(myPoints.Count - 1);
            g.DrawCurve(p, myPoints.ToArray());
        }
        pnlMain.Refresh();
        //This works but you have to spam it to get rid of
        //a line and does some weird connections.
    }

最佳答案

您需要将行存储在 List<List<Point>> 中.列表中的每个元素都包含您使用向下、移动和向上绘制的绘图点。您绘制的下一行将存储在列表的下一个元素中。每次撤消都会删除最后一张绘图。

将此控件的一个实例放在您的窗体上,它将为您处理绘图。同样要执行撤消,请调用其 Undo方法。

using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
public class DrawingSurface : Control
{
    public DrawingSurface() { this.DoubleBuffered = true; }
    List<List<Point>> Lines = new List<List<Point>>();
    bool drawing = false;
    protected override void OnMouseDown(MouseEventArgs e) {
        Lines.Add(new List<Point>());
        Lines.Last().Add(e.Location);
        drawing = true;
        base.OnMouseDown(e);
    }
    protected override void OnMouseMove(MouseEventArgs e) {
        if (drawing) { Lines.Last().Add(e.Location); this.Invalidate(); }
        base.OnMouseMove(e);
    }
    protected override void OnMouseUp(MouseEventArgs e) {
        if (drawing) {
            this.drawing = false;
            Lines.Last().Add(e.Location);
            this.Invalidate();
        }
        base.OnMouseUp(e);
    }
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        foreach (var item in Lines)
            e.Graphics.DrawLines(Pens.Black, item.ToArray()); /*or DrawCurve*/
    }
    public void Undo() {
        if (Lines.Count > 0) { this.Lines.RemoveAt(Lines.Count - 1); this.Invalidate(); }
    }
}

注意

  • 使用此逻辑,您可以使用其他 List<List<Point>> 简单地实现重做.使用 RedoBuffer.Add(Lines.Last()); 将撤消之前的最后一项复制到重做列表就足够了.然后对于每个重做命令,将重做缓冲区的最后一项添加到 Lines 就足够了并将其从重做缓冲区中删除。您还应该在每次按下鼠标后清除重做缓冲区。
  • 您可以使用 DrawLines 中的任一个或 DrawCurve根据您的要求。 DrawLines绘制折线,同时 DrawCurve绘制出更平滑的曲线。

  • 我更喜欢封装Lines.Count > 0在类似 bool CanUndo 的属性中并使其不受控制地访问。

  • 这只是一个示例,您可以简单地扩展解决方案。例如,而不是 List<List<Point>>你可以创建一个 Shape类包含 List<Point> , LineWidth , LineColor等并使用 List<Shape> 执行任务.

关于c# - 绘制多条徒手折线或曲线图 - 添加撤消功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38296729/

相关文章:

c# - EF 和存储库模式

c# - 在 WPF DataGrid 中格式化可为空的日期时间

java - 绘制多个移动图形

c# - c#中的foreach问题

java - 在java3d中切掉圆锥体的一部分

c# - 如何捕获使用opentk绘制的图像

c# 转换错误(无限值....什么?)

c# - 无符号整数的一元取反

C# 和 WinForms : 'Integrity' of Form. Show() 调用

c# - 为什么 System.Drawing.Color 将 'ff' 添加到十六进制代码中?