- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
有没有办法把绳子拉起来然后去掉?
我使用了以下类来撤消/重做矩形、圆形、直线、箭头类型的形状,但不知道如何删除绘制的字符串。
这是我在形状列表中添加矩形的方法:当我从列表中撤消或重做时,这很有效。
拉绳
Shape shape = new Shape();
shape.shape = ShapesTypes.ShapeTypes.Rectangle;
shape.CopyTuplePoints(points);
shape.X = StartPoint.X;
shape.Y = StartPoint.Y;
shape.Width = EndPoint.X;
shape.Height = EndPoint.Y;
Pen pen = new Pen(new SolidBrush(penColor), 2);
shape.pen = pen;
undoactions.AddShape(shape);
这就是我绘制文本的方式:
var fontFamily = new FontFamily("Calibri");
var font = new Font(fontFamily, 12, FontStyle.Regular, GraphicsUnit.Point);
Size proposedSize = new Size(int.MaxValue, int.MaxValue);
TextFormatFlags flags = TextFormatFlags.WordEllipsis | TextFormatFlags.NoPadding | TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.WordBreak;
Size size = TextRenderer.MeasureText(e.Graphics, textAreaValue, font, proposedSize, flags);
Shape shape = new Shape();
shape.shape = ShapesTypes.ShapeTypes.Text;
shape.X = ta.Location.X;
shape.Y = ta.Location.Y;
shape.Width = size.Width;
shape.Height = size.Height;
shape.Value = textAreaValue;
Pen pen = new Pen(new SolidBrush(penColor), 2);
shape.pen = pen;
undoactions.AddShape(shape);
但这不适用于撤消重做列表。也许问题出在 pen 和 font-size 但我无法弄清楚如何将 pen 与 DrawString 一起使用。
编辑:这是我在绘画事件中的绘画方式
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
foreach (var item in undoactions.lstShape)
{
if (item.shape == ShapesTypes.ShapeTypes.Line)
{
e.Graphics.DrawLine(item.pen, item.X, item.Y, item.Width, item.Height);
}
else if (item.shape == ShapesTypes.ShapeTypes.Pen)
{
if (item.Points.Count > 1)
{
e.Graphics.DrawCurve(item.pen, item.Points.ToArray());
}
}
else if (item.shape == ShapesTypes.ShapeTypes.Text)
{
var fontFamily = new FontFamily("Calibri");
var font = new Font(fontFamily, 12, FontStyle.Regular, GraphicsUnit.Point);
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString(item.Value, font, new SolidBrush(item.pen.Color), new PointF(item.X, item.Y));
}
}
}
Shape.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Drawing
{
public class Shape : ICloneable
{
public ShapesTypes.ShapeTypes shape { get; set; }
public List<Point> Points { get; }
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Pen pen { get; set; }
public String Value { get; set; }
public Shape()
{
Points = new List<Point>();
}
public void CopyPoints(List<Point> points)
{
for (int i = 0; i < points.Count; i++)
{
Point p = new Point();
p.X = points[i].X;
p.Y = points[i].Y;
Points.Add(p);
}
}
public void CopyCopyPoints(List<List<Point>> points)
{
for (int j = 0; j < points.Count; j++)
{
List<Point> current = points[j];
for (int i = 0; i < current.Count; i++)
{
Point p = new Point();
p.X = current[i].X;
p.Y = current[i].Y;
Points.Add(p);
}
}
}
public void CopyTuplePoints(List<Tuple<Point, Point>> points)
{
foreach (var line in points)
{
Point p = new Point();
p.X = line.Item1.X;
p.Y = line.Item1.Y;
Points.Add(p);
p.X = line.Item2.X;
p.Y = line.Item2.Y;
Points.Add(p);
}
}
public object Clone()
{
Shape shp = new Shape();
shp.X = X;
shp.Y = Y;
shp.Width = Width;
shp.Height = Height;
shp.pen = pen;
shp.shape = shape;
shp.Value = Value;
for (int i = 0; i < Points.Count; i++)
{
shp.Points.Add(new Point(Points[i].X, Points[i].Y));
}
return shp;
}
}
}
画圆
if (currentshape == ShapesTypes.ShapeTypes.Circle)
{
Shape shape = new Shape();
shape.shape = ShapesTypes.ShapeTypes.Circle;
shape.CopyTuplePoints(cLines);
shape.X = StartPoint.X;
shape.Y = StartPoint.Y;
shape.Width = EndPoint.X;
shape.Height = EndPoint.Y;
Pen pen = new Pen(new SolidBrush(penColor), 2);
shape.pen = pen;
undoactions.AddShape(shape);
}
撤消
if (currentshape != ShapesTypes.ShapeTypes.Undo)
{
oldshape = currentshape;
currentshape = ShapesTypes.ShapeTypes.Undo;
}
if (undoactions.lstShape.Count > 0)
{
undoactions.Undo();
this.Invalidate();
}
if (undoactions.redoShape.Count > 0)
{
btnRedo.Enabled = true;
}
撤消重做
public class UndoRedo
{
public List<Shape> lstShape = new List<Shape>();
public List<Shape> redoShape = new List<Shape>();
public void AddShape(Shape shape)
{
lstShape.Add(shape);
}
public void Undo()
{
redoShape.Add((Shape)lstShape[lstShape.Count - 1].Clone());
lstShape.RemoveAt(lstShape.Count - 1);
}
public void Redo()
{
lstShape.Add((Shape)redoShape[redoShape.Count - 1].Clone());
redoShape.RemoveAt(redoShape.Count - 1);
}
}
最佳答案
您可以创建一个 TextShape
派生自 Shape
,具有 Text
、Font
、Location
和 Color
属性并将其视为其他形状,因此重做和撤消不会成为问题。
以下是一些可以帮助您解决问题的提示:
Shape
类或接口(interface),其中包含基本方法,例如 Draw
、Clone
、HitTest
等。 TextShape
都应派生自 Shape
。 TextShape
也是一种形状,具有 Text
、Font
、Location
和 Color
属性.Shape
的每个实现都有其基方法的实现。INotifyPropertyChanged
,然后您可以监听属性的变化,例如,在更改颜色、边框宽度等后向撤消缓冲区添加一些内容。IClonable
或基类Clone
方法。添加到撤消缓冲区时,所有形状都应该是可复制的。Pen
和 Brush
。这不是可选的。INotifyPropertyChanged
,然后通过形状或此类属性的每次更改,您可以添加此类的克隆以撤消缓冲区。形状
下面是一个Shape
类的例子:
public abstract class Shape : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string name = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public abstract void Draw(Graphics g);
public abstract Shape Clone();
}
文字形状
注意属性的实现以引发PropertyChanged
事件以及Clone
方法来克隆撤消缓冲区的对象,以及GDI 对象的使用方式绘制
:
public class TextShape : Shape {
private string text;
public string Text {
get { return text; }
set {
if (text != value) {
text = value;
OnPropertyChanged();
}
}
}
private Point location;
public Point Location {
get { return location; }
set {
if (!location.Equals(value)) {
location = value;
OnPropertyChanged();
}
}
}
private Font font;
public Font Font {
get { return font; }
set {
if (font!=value) {
font = value;
OnPropertyChanged();
}
}
}
private Color color;
public Color Color {
get { return color; }
set {
if (color!=value) {
color = value;
OnPropertyChanged();
}
}
}
public override void Draw(Graphics g) {
using (var brush = new SolidBrush(Color))
g.DrawString(Text, Font, brush, Location);
}
public override Shape Clone() {
return new TextShape() {
Text = Text,
Location = Location,
Font = (Font)Font.Clone(),
Color = Color
};
}
}
绘图上下文
这个类实际上包含所有形状和一些其他属性,如绘图表面的背景颜色。这是您需要将其克隆添加到撤消缓冲区的类:
public class DrawingContext : INotifyPropertyChanged {
public DrawingContext() {
BackColor = Color.White;
Shapes = new BindingList<Shape>();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string name = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
private Color backColor;
public Color BackColor {
get { return backColor; }
set {
if (!backColor.Equals(value)) {
backColor = value;
OnPropertyChanged();
}
}
}
private BindingList<Shape> shapes;
public BindingList<Shape> Shapes {
get { return shapes; }
set {
if (shapes != null)
shapes.ListChanged -= Shapes_ListChanged;
shapes = value;
OnPropertyChanged();
shapes.ListChanged += Shapes_ListChanged;
}
}
private void Shapes_ListChanged(object sender, ListChangedEventArgs e) {
OnPropertyChanged("Shapes");
}
public DrawingContext Clone() {
return new DrawingContext() {
BackColor = this.BackColor,
Shapes = new BindingList<Shape>(this.Shapes.Select(x => x.Clone()).ToList())
};
}
}
绘图表面
这个类实际上是具有撤消和重做功能的控件,并且还在其表面上绘制当前绘图上下文:
public class DrawingSurface : Control {
private Stack<DrawingContext> UndoBuffer = new Stack<DrawingContext>();
private Stack<DrawingContext> RedoBuffer = new Stack<DrawingContext>();
public DrawingSurface() {
DoubleBuffered = true;
CurrentDrawingContext = new DrawingContext();
UndoBuffer.Push(currentDrawingContext.Clone());
}
DrawingContext currentDrawingContext;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public DrawingContext CurrentDrawingContext {
get {
return currentDrawingContext;
}
set {
if (currentDrawingContext != null)
currentDrawingContext.PropertyChanged -= CurrentDrawingContext_PropertyChanged;
currentDrawingContext = value;
Invalidate();
currentDrawingContext.PropertyChanged += CurrentDrawingContext_PropertyChanged;
}
}
private void CurrentDrawingContext_PropertyChanged(object sender, PropertyChangedEventArgs e) {
UndoBuffer.Push(CurrentDrawingContext.Clone());
RedoBuffer.Clear();
Invalidate();
}
public void Undo() {
if (CanUndo) {
RedoBuffer.Push(UndoBuffer.Pop());
CurrentDrawingContext = UndoBuffer.Peek().Clone();
}
}
public void Redo() {
if (CanRedo) {
CurrentDrawingContext = RedoBuffer.Pop();
UndoBuffer.Push(CurrentDrawingContext.Clone());
}
}
public bool CanUndo {
get { return UndoBuffer.Count > 1; }
}
public bool CanRedo {
get { return RedoBuffer.Count > 0; }
}
protected override void OnPaint(PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var brush = new SolidBrush(CurrentDrawingContext.BackColor))
e.Graphics.FillRectangle(brush, ClientRectangle);
foreach (var shape in CurrentDrawingContext.Shapes)
shape.Draw(e.Graphics);
}
}
关于c# - 使用撤消和重做功能绘制形状和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50690023/
示例:我在 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
我是一名优秀的程序员,十分优秀!