schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
 
namespace XHandler.Controls.DrawCanvas
{
    /// <summary>
    /// 椭圆
    /// </summary>
    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
    }
}