using System; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace XHandler.Controls.DrawCanvas { /// /// 椭圆 /// public sealed class EllipseDrawTool : DrawGeometryBase { public EllipseDrawTool(DrawingCanvas drawingCanvas) : base(drawingCanvas) { this.DrawingToolType = DrawToolType.Ellipse; // 准备要处理的事件 this.CanTouchDown = true; } #region 鼠标键盘事件 public override Boolean OnKeyDown(Key key) { if (key != Key.LeftShift || !bottomRight.HasValue) return false; return OnTouchMove(bottomRight.Value); } public override Boolean OnKeyUp(Key key) { if (key != Key.LeftShift || !bottomRight.HasValue) return false; return OnTouchMove(bottomRight.Value); } public override Boolean OnTouchLeave(Point point) { //if (!bottomRight.HasValue) // this.drawingCanvas.DeleteVisual(this); //else { Point centerPoint = new Point(point.X, point.Y); center = centerPoint; PathFigure myPathFigure = new PathFigure();//画第一条直线 myPathFigure.StartPoint = new Point(point.X - 30, point.Y); LineSegment myLineSegment = new LineSegment(); myLineSegment.Point = new Point(point.X + 30, point.Y); PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection(); myPathSegmentCollection.Add(myLineSegment); myPathFigure.Segments = myPathSegmentCollection; PathFigure myPathFigure2 = new PathFigure();//画第二条直线 myPathFigure2.StartPoint = new Point(point.X, point.Y - 30); LineSegment myLineSegment2 = new LineSegment(); myLineSegment2.Point = new Point(point.X, point.Y + 30); PathSegmentCollection myPathSegmentCollection2 = new PathSegmentCollection(); myPathSegmentCollection2.Add(myLineSegment2); myPathFigure2.Segments = myPathSegmentCollection2; PathFigureCollection myPathFigureCollection = new PathFigureCollection(); myPathFigureCollection.Add(myPathFigure);//添加画的线段 myPathFigureCollection.Add(myPathFigure2); pathGeometry.Figures = myPathFigureCollection; FormattedText text = new FormattedText(this.ID.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("宋体"), 60, Brushes.Black); Geometry textGeometry = text.BuildGeometry(new Point(point.X + 40, point.Y - 40)); GeometryGroup geometryGroup = new GeometryGroup(); geometryGroup.Children.Add(geometry); geometryGroup.Children.Add(textGeometry); //geometry = geometry.GetFlattenedPathGeometry(); geometry = geometryGroup.GetFlattenedPathGeometry(); Draw(); DrawingCanvas.ID++; nIndex++; } this.DrawingCanvas.DeleteWorkingDrawTool(this); this.IsFinish = true; this.CanTouchDown = false; this.CanTouchMove = false; this.CanTouchLeave = false; this.CanKeyDown = false; this.CanKeyUp = false; if (this.TouchId == 0 && this.DrawingCanvas.IsMouseCaptured) this.DrawingCanvas.ReleaseMouseCapture(); return true; } public override Boolean OnTouchDown(Int32 touchId, Point point) { this.TouchId = touchId; // if (!topLeft.HasValue) { this.DrawingCanvas.AddWorkingDrawTool(this); this.pen = this.DrawingCanvas.Pen; topLeft = point; this.geometry = new PathGeometry(); //var figure = new PathFigure { IsClosed = true }; //pathGeometry.Figures.Add(figure); this.CanTouchMove = false; this.CanKeyDown = false; this.CanKeyUp = false; this.CanTouchUp = true; //if (this.TouchId != 0 || !this.drawingCanvas.CaptureMouse()) this.CanTouchLeave = true; this.DrawingCanvas.AddVisual(this); } //else //return OnTouchLeave(point); return true; } public override Boolean OnTouchMove(Point point) { var dc = this.RenderOpen(); var startPoint = topLeft.Value; if (Keyboard.IsKeyDown(Key.LeftShift)) { var len = Math.Min(Math.Abs(point.X - startPoint.X), Math.Abs(point.Y - startPoint.Y)); point = new Point(startPoint.X + (point.X > startPoint.X ? len : -len), startPoint.Y + (point.Y > startPoint.Y ? len : -len)); } if ((startPoint - point).Length <= pen.Thickness) { dc.Close(); return true; } bottomRight = point; radiusX = (point.X - startPoint.X) / 2; radiusY = (point.Y - startPoint.Y) / 2; Point centerPoint= new Point(startPoint.X + radiusX, startPoint.Y + radiusY); center = centerPoint; radiusX = Math.Abs(radiusX); radiusY = Math.Abs(radiusY); dc.DrawEllipse(null, pen, center, radiusX, radiusY); dc.Close(); return true; } #endregion #region 序列化 public override DrawGeometrySerializerBase ToSerializer() { var serializer = new DrawEllipseSerializer { Color = ((SolidColorBrush)pen.Brush).Color, StrokeThickness = pen.Thickness, Geometry = geometry.ToString() }; if (geometry.Transform != null) serializer.Matrix = geometry.Transform.Value; return serializer; } public override void DeserializeFrom(DrawGeometrySerializerBase serializer) { this.pen = new Pen(new SolidColorBrush(serializer.Color), serializer.StrokeThickness); this.geometry = Geometry.Parse(serializer.Geometry).GetFlattenedPathGeometry(); this.geometry.Transform = new TranslateTransform(serializer.Matrix.OffsetX, serializer.Matrix.OffsetY); this.IsFinish = true; this.Draw(); } #endregion #region 字段 private Point? topLeft, bottomRight; private static int nIndex = 0; public Point center { get; set; } //public int ID = 1; public Double radiusX { get; set; } public Double radiusY { get; set; } #endregion } }