- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个具有撤消和重做功能的简单绘图应用程序。我假设您可以将要绘制的内容添加到列表中,然后调用列表绘制所有内容。然后撤消应该只删除最后添加的项目并重新绘制所有内容。问题是,如何将我绘制的内容添加到列表中并使用该列表撤消?
我正在使用位图重绘方法。我是这样画的:
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 列中有一个公式,它是 A 和 B 列中值的函数。当我更改 A 或 B 中的值并按 Enter 键时,C 中的值会自动更新。但是,当我 Ctrl-Z 时,C 列中的值不会自动恢复到我更改
我使用 Eclipse 进行几乎所有语言的编程。我也喜欢用于快速编辑的 vim 快捷方式,所以我安装了 vrapper。问题是当我写了很多代码,我只想撤消一些小东西。它撤消了很多代码块,而不是 2 或
假设我们无意中将命名分支( ABC ) merge 到我们的 default 中。分支。 hg rollback不是一个选择,因为从那以后有几次提交。 有没有办法撤销这个? 最佳答案 您将需要 Mq
有什么办法可以实现这一点吗?我正在使用一个分页插件,它读取 ul 中的 li 数量,并确定要吐出的编号链接的数量。 最佳答案 您可能想使用 .hide() 并检查 li 是否可见。 这可以通过以下方式
我需要能够检测是否触发了“撤销”,以及它是否对我的 RichTextBox 的内容产生了影响。 当我在 RichTextBox 中键入内容,然后按 Ctrl+Z 时,windows 似乎会为我处理撤消
我的每个 php 页面的顶部都有以下代码: foreach ($_POST as $key => $value) { if (!is_array($value)) {
我正在为一个类的项目工作,我们应该在该类中实现文本编辑器的基本功能,包括撤消和重做。我目前的撤消/重做功能正常工作,唯一的问题是,我在尝试从 Command 中释放内存时遇到 valgrind 错误。
假设我有以下两个变量: bob1 = u'bob\xf0\xa4\xad\xa2' bob2 = 'bob\xf0\xa4\xad\xa2' 如何让 bob1 的值成为 bob2 的值?也就是说,我如
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 1 年前。 社区在 1 年前 审查了是否重
我已经成功地实现了 touchesMoved 并且它正在正确地绘制线条,现在我在实现删除方法时遇到了问题,请在我缺少的地方帮助我。 - (void)touchesMoved:(NSSet *)touc
我使用的是 Visual Studio 2015。TFS 的版本是 2012(版本 11.0.51106.1)。有没有办法撤销 unshelve,其他用户的 shelfset,有 unshelve 命
我正在使用 intelliJ 在功能分支上工作,我想将我的团队在 master 上所做的更改集成到我的分支中,但我搞砸了。 我检查了 master,从 Git pop 窗口中我选择了功能分支并选择了“
我的情况:我克隆了一个存储库(内核的源文件)。然后检查了一个分支,并构建了内核。二进制文件已存储在工作树的子目录中。现在,我想放弃所有本地更改,包括构建的二进制文件,并恢复到克隆存储库的不同分支。 我
我正在使用 SourceTree,我在“工作副本更改”部分下有一些修改过的文件,这些文件正在被跟踪,如图中的黄色图标所示。但我放弃了这些变化。现在如何恢复这些更改? 最佳答案 如果项目未暂存(在顶部
撤消特定提交的最简单方法是: 不在头部或头部 已推送到远程。 因为如果不是最新的提交, git reset HEAD 没用。并且因为它已经被推送到远程, git rebase -i 和 git reb
我有一个 Pdf 文件,每页包含几张幻灯片,包括文本(不仅是图像)。 该 pdf 可能是使用 pdfnup 创建的。 我可以恢复 pdfnup 操作,以便每张幻灯片都显示在一页上吗? 最佳答案 据我所
我正在进行合并。合并后,我提交所有文件而没有冲突。然后我解析单个文件并提交它们。如果我犯了一个错误,我该如何重做解析(即使我还没有提交解析文件)? 与预期相反,我无法还原文件。合并后恢复到任何步骤时,
当之后进行了 17 次提交时,如何撤消不应该进行的提交? 背景:我团队的一个同事单独工作了一个月,现在是时候将他们的分支 merge 到 master 上了。但是,其中一个提交包含一个太大的文件,无法
我正在尝试使用Command Pattern在我的应用程序中实现撤消/重做功能。我遇到了一个问题。 为了说明这一点,让我们假设您可以使用我的应用程序2D配置文件创建(任意数量)。 然后,可以从这些2D
我有一个工作流,我将描述如下: [ Dump(query) ] ---+ | +---> [ Parquet(d
我是一名优秀的程序员,十分优秀!