private void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { if (!EditEnabled) { return; } var point = e.GetPosition(this); if (MaxChildren > 0 && History.Count >= MaxChildren) { // 不在框内按下鼠标,不处理拖动 if (!current.ElementRect.Contains(point)) { return; } MouseDownPoint = point; MoveMode = true; IsMouseDown = true; EmitDrawEvent(DrawState.Start); return; } current = new DrawRecord { Shape = DefaultDrawType, Color = Colors.Blue }; current.Start = point; IsMouseDown = true; EmitDrawEvent(DrawState.Start); }
public static void Draw(this Canvas canvas, DrawRecord record) { if (History == null) { History = new Dictionary <Canvas, Stack <int> >(); } Stack <int> history = null; if (History.ContainsKey(canvas)) { history = History[canvas]; } else { history = new Stack <int>(); History.Add(canvas, history); } var element = record.GetElement(); if (element == null) { return; } history.Push(canvas.Children.Add(element)); GC.Collect(); }
public static void Draw(this Graphics graphics, DrawRecord record) { var width = record.Width; using (var pen = new Pen(record.Color.ToDrawingColor(), width)) { pen.StartCap = LineCap.Round; pen.MiterLimit = 0.1f; if (record.LineStyle == LineStyles.Dashed) { pen.DashStyle = DashStyle.Dash; } else if (record.LineStyle == LineStyles.Dotted) { pen.DashStyle = DashStyle.Dot; } switch (record.Shape) { case DrawShapes.Curve: pen.EndCap = LineCap.Round; graphics.DrawCurve(pen, record.Points.Select(p => new Point((int)p.X, (int)p.Y)).ToArray()); break; case DrawShapes.Ellipse: graphics.DrawEllipse(pen, record.Rect.ToDrawingRectangle()); break; case DrawShapes.Line: pen.EndCap = LineCap.Round; graphics.DrawLine(pen, record.Start.ToDrawingPoint(), record.End.ToDrawingPoint()); break; case DrawShapes.Rectangle: graphics.DrawRectangle(pen, record.Rect.ToDrawingRectangle()); break; case DrawShapes.Text: if (string.IsNullOrWhiteSpace(record.Text)) { return; } graphics.DrawString(record.Text, record.TextFont, new SolidBrush(record.Color.ToDrawingColor()), record.Start.ToDrawingPoint()); break; case DrawShapes.Arrow: var cap = new AdjustableArrowCap(5, 5, false); pen.CustomEndCap = cap; graphics.DrawLine(pen, record.Start.ToDrawingPoint(), record.End.ToDrawingPoint()); break; } } graphics.Flush(); GC.Collect(); }
/// <summary> /// 绘制图形 /// </summary> /// <param name="record"></param> public void Draw(DrawRecord record) { var element = record.GetElement(); if (element == null) { return; } if (History.Any(item => item.Equals(element))) { return; } Children.Add(element); History.Push(element); GC.Collect(); }
public static void Draw(this Graphics graphics, DrawRecord record) { var width = record.Width; var brush = new SolidBrush(record.Color.ToDrawingColor()); var pen = new Pen(brush, width); //pen.StartCap = LineCap.Square; //pen.MiterLimit = 0.1f; if (record.LineStyle == LineStyles.Dashed) { pen.DashStyle = DashStyle.Dash; } else if (record.LineStyle == LineStyles.Dotted) { pen.DashStyle = DashStyle.Dot; } switch (record.Shape) { case DrawShapes.Curve: //pen.EndCap = LineCap.Square; graphics.DrawCurve(pen, record.Points.Select(p => new Point((int)p.X, (int)p.Y)).ToArray()); break; case DrawShapes.Ellipse: if (record.Mode == DrawModes.Stroke) { graphics.DrawEllipse(pen, record.Rect.ToDrawingRectangle()); } else if (record.Mode == DrawModes.Fill) { graphics.FillEllipse(brush, record.Rect.ToDrawingRectangle()); } break; case DrawShapes.Line: //pen.EndCap = LineCap.Square; graphics.DrawLine(pen, record.Start.ToDrawingPoint(), record.End.ToDrawingPoint()); break; case DrawShapes.Rectangle: if (record.Mode == DrawModes.Stroke) { graphics.DrawRectangle(pen, record.Rect.ToDrawingRectangle()); } else if (record.Mode == DrawModes.Fill) { graphics.FillRectangle(brush, record.Rect.ToDrawingRectangle()); } break; case DrawShapes.Polygon: if (record.Mode == DrawModes.Stroke) { graphics.DrawPolygon(pen, record.Points.Take(record.Points.Count - 1).Select(p => p.ToDrawingPoint()).ToArray()); } else if (record.Mode == DrawModes.Fill) { graphics.FillPolygon(brush, record.Points.Take(record.Points.Count - 1).Select(p => p.ToDrawingPoint()).ToArray()); } break; case DrawShapes.Text: if (string.IsNullOrWhiteSpace(record.Text)) { return; } graphics.DrawString(record.Text, record.TextFont, new SolidBrush(record.Color.ToDrawingColor()), record.Start.ToDrawingPoint()); break; case DrawShapes.Arrow: int arrowSize; switch (record.Distance) { case 5: case 4: arrowSize = 2; break; case 3: case 2: case 1: arrowSize = 1; break; default: arrowSize = 5; break; } var cap = new AdjustableArrowCap(arrowSize, arrowSize, false); pen.CustomEndCap = cap; graphics.DrawLine(pen, record.Start.ToDrawingPoint(), record.End.ToDrawingPoint()); break; } pen.Dispose(); brush.Dispose(); graphics.Flush(); GC.Collect(); }