using EC.Entity.Warning; using ECMonitor.Code.DB; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace ECMonitor.PlayerDraw.Code { public class ECDrawing { Button drawFinished; Polygon drawPolygon;//预览多边形 Rectangle circular;//预览圆 InkCanvas writeBorad;//画布 Grid contenter { get; set; }// 控件容器 StylusPointCollection drawPoints { get; set; } = new StylusPointCollection();//添加到画布图像的点 PointCollection drawingPoints { get; set; } = new PointCollection();//预览图形的点 BlackboardData data { get; set; } public ECDrawing(BlackboardData data, Grid contenter, InkCanvas writeBorad) { this.data = data; this.contenter = contenter; this.writeBorad = writeBorad; this.writeBorad.EditingModeChanged += writeBorad_EditingModeChanged; this.data.PropertyChanged += data_PropertyChanged; } void writeBorad_EditingModeChanged(object sender, RoutedEventArgs e) { StopDraw(); } void data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (drawPoints.Count() != 0 && (e.PropertyName == "DrawType" || e.PropertyName == "Draw")) { StopDraw(); } } private void StopDraw() { drawPoints.Clear(); if (drawPolygon != null) { contenter.Children.Remove(drawPolygon); drawPolygon = null; } if (circular != null) { contenter.Children.Remove(circular); drawPolygon = null; } if (drawFinished != null) { contenter.Children.Remove(drawFinished); drawFinished = null; } drawingPoints.Clear(); } public void MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this.data.Draw == true && writeBorad.EditingMode == InkCanvasEditingMode.None && (drawPolygon != null || circular != null)) { Draw createDraw = new Draw(writeBorad); Point point = e.GetPosition(writeBorad); if (!drawingPoints.Contains(point)) { drawPoints.Add(new StylusPoint(point.X, point.Y)); drawingPoints.Add(point); } DrawTypes drawType = (DrawTypes)data.DrawType; switch (drawType) { case DrawTypes.矩形: if (drawPoints.Count == 2) { writeBorad.Strokes.Add(createDraw.Rectangle(drawPoints[0], drawPoints[1], data.LineColor)); drawPoints.Clear(); drawingPoints.Clear(); contenter.Children.Remove(drawPolygon); drawPolygon = null; } break; case DrawTypes.线段: if (drawPoints.Count == 2) { writeBorad.Strokes.Add(createDraw.Line(drawPoints[0], drawPoints[1], data.LineColor)); drawPoints.Clear(); drawingPoints.Clear(); contenter.Children.Remove(drawPolygon); drawPolygon = null; } break; case DrawTypes.圆: if (drawPoints.Count == 2) { writeBorad.Strokes.Add(createDraw.Circle(drawPoints[0], drawPoints[1], data.LineColor)); drawPoints.Clear(); drawingPoints.Clear(); contenter.Children.Remove(circular); circular = null; } break; case DrawTypes.三角形: if (drawPoints.Count == 3) { writeBorad.Strokes.Add(createDraw.Triangle(drawPoints[0], drawPoints[1], drawPoints[2])); drawPoints.Clear(); drawingPoints.Clear(); contenter.Children.Remove(drawPolygon); drawPolygon = null; } break; case DrawTypes.多边形: if (drawFinished == null) { drawFinished = new Button { Content = "完成", Background = Brushes.Green, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1), FontSize = 18, Foreground = Brushes.Black, VerticalAlignment = VerticalAlignment.Top, HorizontalAlignment = HorizontalAlignment.Left, }; Panel.SetZIndex(drawFinished, 20); drawFinished.Click += delegate (object a, RoutedEventArgs b) { drawPoints.RemoveAt(drawPoints.Count - 1); writeBorad.Strokes.Add(createDraw.Polygon(drawPoints.Clone(), data.LineColor)); drawPoints.Clear(); drawingPoints.Clear(); contenter.Children.Remove(drawPolygon); drawPolygon = null; contenter.Children.Remove(drawFinished); drawFinished = null; }; contenter.Children.Add(drawFinished); } drawFinished.Margin = new Thickness(point.X, point.Y, 0, 0); break; //case DrawTypes.折线: // if (drawFinished == null) // { // drawFinished = new Button // { // Content = "完成", // Background = Brushes.Green, // BorderBrush = Brushes.Black, // BorderThickness = new Thickness(1), // FontSize = 18, // Foreground = Brushes.Black, // VerticalAlignment = VerticalAlignment.Top, // HorizontalAlignment = HorizontalAlignment.Left, // }; // Panel.SetZIndex(drawFinished, 20); // drawFinished.Click += // delegate (object a, RoutedEventArgs b) // { // drawPoints.RemoveAt(drawPoints.Count - 1); // writeBorad.Strokes.Add(createDraw.Lines(drawPoints.Clone(), data.LineColor)); // drawPoints.Clear(); // drawingPoints.Clear(); // contenter.Children.Remove(drawPolygon); // drawPolygon = null; // contenter.Children.Remove(drawFinished); // drawFinished = null; // }; // contenter.Children.Add(drawFinished); // } // drawFinished.Margin = new Thickness(point.X, point.Y, 0, 0); // break; } writeBorad.IsEnabled = true; } } public void MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed && data.Draw == true && writeBorad.EditingMode == InkCanvasEditingMode.None && contenter.IsMouseOver) writeBorad.IsEnabled = false; else writeBorad.IsEnabled = true; if (drawPoints.Count != 0) { Point point = new Point(e.GetPosition(writeBorad).X - 1, e.GetPosition(writeBorad).Y - 1); DrawTypes drawType = (DrawTypes)data.DrawType; if (drawType != DrawTypes.圆 && drawPolygon == null) { drawPolygon = new Polygon { Stroke = Brushes.Black, StrokeThickness = 2, StrokeDashCap = PenLineCap.Square, StrokeDashArray = new DoubleCollection { 6 } }; } switch (drawType) { case DrawTypes.矩形: if (drawPoints.Count == 1) { Point a = new Point(drawPoints[0].X, drawPoints[0].Y); PointCollection points = new PointCollection { a, new Point(a.X, point.Y), point, new Point(point.X, a.Y) }; drawPolygon.Points = points.Clone(); if (!drawPolygon.IsLoaded) contenter.Children.Add(drawPolygon); } break; case DrawTypes.线段: if (drawPoints.Count == 1) { PointCollection points = new PointCollection { new Point(drawPoints[0].X,drawPoints[0].Y), point }; drawPolygon.Points = points.Clone(); if (!drawPolygon.IsLoaded) contenter.Children.Add(drawPolygon); } break; case DrawTypes.圆: if (drawPoints.Count == 1) { double r = Math.Sqrt(Math.Pow(point.X - drawPoints[0].X, 2) + Math.Pow(point.Y - drawPoints[0].Y, 2)); double top = drawPoints[0].Y - r, left = drawPoints[0].X - r; if (circular == null) { circular = new Rectangle { Stroke = Brushes.Black, StrokeThickness = 2, StrokeDashCap = PenLineCap.Square, StrokeDashArray = new DoubleCollection { 6 }, }; contenter.Children.Add(circular); } circular.RadiusX = circular.RadiusY = r; circular.VerticalAlignment = VerticalAlignment.Stretch; circular.HorizontalAlignment = HorizontalAlignment.Stretch; circular.Width = circular.Height = 2 * r; circular.VerticalAlignment = VerticalAlignment.Top; circular.HorizontalAlignment = HorizontalAlignment.Left; circular.Margin = new Thickness(left, top, 0, 0); } break; case DrawTypes.三角形: if (drawPoints.Count == 2) { if (!drawPolygon.IsLoaded) contenter.Children.Add(drawPolygon); drawPolygon.Points = drawingPoints.Clone(); drawPolygon.Points.Add(point); } break; case DrawTypes.多边形: if (!drawPolygon.IsLoaded) contenter.Children.Add(drawPolygon); drawPolygon.Points = drawingPoints.Clone(); drawPolygon.Points.Add(point); break; //case DrawTypes.折线: // if (!drawPolygon.IsLoaded) // contenter.Children.Add(drawPolygon); // drawPolygon.Points = drawingPoints.Clone(); // drawPolygon.Points.Add(point); // break; } } } public void MouseDown(object sender, MouseButtonEventArgs e) { //InkCanvasEditingMode.None; //InkCanvasEditingMode.None // InkCanvasEditingMode.GestureOnly // InkCanvasEditingMode.NoneAndGesture if (data.Draw == true && writeBorad.EditingMode == InkCanvasEditingMode.None && contenter.IsMouseOver) { Point point = e.GetPosition(contenter); drawPoints.Add(new StylusPoint(point.X, point.Y)); drawingPoints.Add(point); } } public void ClearSharpe() { writeBorad.Strokes.Clear(); contenter.Children.Clear(); contenter.Children.Add(writeBorad); //contenter.Width = SystemParameters.PrimaryScreenWidth; //contenter.Height = SystemParameters.PrimaryScreenHeight; } public void LoadDrawShape(string ip,double width,double height, bool reload = true) { if (reload) { ClearSharpe(); } var drawShapeList = DBDataHelper.GetDrawShape(ip); Draw createDraw = new Draw(writeBorad); foreach (DrawShape shape in drawShapeList) { var drawType = (DrawTypes)shape.DrawType; data.LineColor = shape.WarningLevel; List points1 = new List(); foreach (var drPoint in shape.PointList) { // Point point = new Point(drPoint.X, drPoint.Y); Point point = new Point(drPoint.X * width, drPoint.Y * height); points1.Add(point); } StylusPointCollection stylusPointCollection = new StylusPointCollection(points1); switch (drawType) { case DrawTypes.矩形: if (stylusPointCollection.Count >3) { writeBorad.Strokes.Add(createDraw.Rectangle(stylusPointCollection[0], stylusPointCollection[2], data.LineColor)); } break; case DrawTypes.线段: if (stylusPointCollection.Count == 2) { writeBorad.Strokes.Add(createDraw.Line(stylusPointCollection[0],stylusPointCollection[1], data.LineColor)); } break; case DrawTypes.圆: if (stylusPointCollection.Count == 2) { writeBorad.Strokes.Add(createDraw.Circle(stylusPointCollection[0], stylusPointCollection[1], data.LineColor)); } break; case DrawTypes.三角形: if (stylusPointCollection.Count == 3) { writeBorad.Strokes.Add(createDraw.Triangle(stylusPointCollection[0], stylusPointCollection[1], stylusPointCollection[2])); } break; case DrawTypes.多边形: //drawPoints.RemoveAt(drawPoints.Count - 1); writeBorad.Strokes.Add(createDraw.Polygon(stylusPointCollection.Clone(), data.LineColor)); //drawPoints.Clear(); //drawingPoints.Clear(); //contenter.Children.Remove(drawPolygon); //drawPolygon = null; //contenter.Children.Remove(drawFinished); //drawFinished = null; break; } writeBorad.IsEnabled = false; writeBorad.EditingMode = InkCanvasEditingMode.Select; data.Draw = false; } } } }