프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkupToPDF_Old / Controls / Shape / RectangleControl.cs @ 9fa712a5

이력 | 보기 | 이력해설 | 다운로드 (14.7 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Windows;
8
using System.Windows.Shapes;
9
using System.Windows.Controls;
10
using System.Windows.Media;
11
using MarkupToPDF.Controls.Common;
12

    
13
namespace MarkupToPDF.Controls.Shape
14
{
15
    public class RectangleControl : Control, IDisposable, INotifyPropertyChanged, IMarkupCommonData
16
    {
17
        public Path Base_RectPath { get; set; }
18
        private const string PART_RectPath = "PART_RectPath";
19

    
20
        #region Property
21

    
22

    
23
        public MouseMode mousemode
24
        {
25
            get
26
            {
27
                return (MouseMode)GetValue(mousemodeProperty);
28
            }
29
            set
30
            {
31
                SetValue(mousemodeProperty, value);
32
                OnPropertyChanged("mousemode");
33
            }
34
        }
35

    
36
        public SolidColorBrush StrokeColor
37
        {
38
            get
39
            {
40
                return (SolidColorBrush)GetValue(StrokeColorProperty);
41
            }
42
            set
43
            {
44
                SetValue(StrokeColorProperty, value);
45
                OnPropertyChanged("StrokeColor");
46
            }
47
        }
48

    
49
        public Double LineSize
50
        {
51
            get
52
            {
53
                return (Double)GetValue(LineSizeProperty);
54
            }
55
            set
56
            {
57
                SetValue(LineSizeProperty, value);
58
                OnPropertyChanged("LineSize");
59
            }
60
        }
61

    
62
        public Geometry PathData
63
        {
64
            get
65
            {
66
                return (Geometry)GetValue(PathDataProperty);
67
            }
68
            set
69
            {
70
                SetValue(PathDataProperty, value);
71
                OnPropertyChanged("PathData");
72
            }
73

    
74
        }
75

    
76
        public Geometry OverViewPathData
77
        {
78
            get
79
            {
80
                return (Geometry)GetValue(OverViewPathDataProperty);
81
            }
82
            set
83
            {
84
                SetValue(OverViewPathDataProperty, value);
85
                OnPropertyChanged("OverViewPathData");
86
            }
87
        }
88

    
89
        public SolidColorBrush FillColor
90
        {
91
            get { return (SolidColorBrush)GetValue(FillColorProperty); }
92
            set
93
            {
94
                SetValue(FillColorProperty, value);
95
                OnPropertyChanged("FillColor");
96
            }
97
        }
98

    
99
        public Point StartPoint
100
        {
101
            get
102
            {
103
                return (Point)GetValue(StartPointProperty);
104
            }
105
            set
106
            {
107
                SetValue(StartPointProperty, value);
108
                OnPropertyChanged("StartPoint");
109
            }
110
        }
111

    
112
        public Point SetPoint
113
        {
114
            get
115
            {
116
                return (Point)GetValue(SetPointProperty);
117
            }
118
            set
119
            {
120
                SetValue(SetPointProperty, value);
121
                OnPropertyChanged("SetPoint");
122
            }
123
        }
124

    
125
        public Point OriginPoint
126
        {
127
            get
128
            {
129
                return (Point)GetValue(OriginPointProperty);
130
            }
131
            set
132
            {
133
                SetValue(OriginPointProperty, value);
134
                OnPropertyChanged("OriginPoint");
135
            }
136
        }
137

    
138
        public Point TopRightPoint
139
        {
140
            get
141
            {
142
                return (Point)GetValue(TopRightPointProperty);
143
            }
144
            set
145
            {
146
                SetValue(TopRightPointProperty, value);
147
                OnPropertyChanged("TopRightPoint");
148
            }
149
        }
150

    
151
        public Point LeftBottomPoint
152
        {
153
            get
154
            {
155
                return (Point)GetValue(LeftBottomPointProperty);
156
            }
157
            set
158
            {
159
                SetValue(LeftBottomPointProperty, value);
160
                OnPropertyChanged("LeftBottomPoint");
161
            }
162
        }
163

    
164
        public Point EndPoint
165
        {
166
            get
167
            {
168
                return (Point)GetValue(EndPointProperty);
169
            }
170
            set
171
            {
172
                SetValue(EndPointProperty, value);
173
                OnPropertyChanged("EndPoint");
174
            }
175
        }
176

    
177
        public bool IsSelected
178
        {
179
            get
180
            {
181
                return (bool)GetValue(IsSelectedProperty);
182
            }
183
            set
184
            {
185
                SetValue(IsSelectedProperty, value);
186
                OnPropertyChanged("IsSelected");
187
            }
188
        }
189

    
190
        public ControlType ControlType
191
        {
192
            set
193
            {
194
                SetValue(ControlTypeProperty, value);
195
                OnPropertyChanged("ControlType");
196
            }
197
            get
198
            {
199
                return (ControlType)GetValue(ControlTypeProperty);
200
            }
201
        }
202

    
203
        public List<Point> PointSet
204
        {
205
            get { return (List<Point>)GetValue(PointSetProperty); }
206
            set { SetValue(PointSetProperty, value); }
207
        }
208

    
209

    
210
        public double CanvasX
211
        {
212
            get { return (double)GetValue(CanvasXProperty); }
213
            set
214
            {
215
                if (this.CanvasX != value)
216
                {
217
                    SetValue(CanvasXProperty, value);
218
                    OnPropertyChanged("CanvasX");
219
                }
220
            }
221
        }
222

    
223
        public double CanvasY
224
        {
225
            get { return (double)GetValue(CanvasYProperty); }
226
            set
227
            {
228
                if (this.CanvasY != value)
229
                {
230
                    SetValue(CanvasYProperty, value);
231
                    OnPropertyChanged("CanvasY");
232
                }
233
            }
234
        }
235

    
236
        #endregion
237

    
238
        #region Dependency Property
239

    
240

    
241

    
242
        public static readonly DependencyProperty mousemodeProperty =
243
                DependencyProperty.Register("mousemode", typeof(MouseMode), typeof(RectCloudControl), new PropertyMetadata(MouseMode.None, PointValueChanged));
244

    
245
        public static readonly DependencyProperty IsSelectedProperty =
246
          DependencyProperty.Register("IsSelected", typeof(bool), typeof(RectangleControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
247

    
248
        public static readonly DependencyProperty ControlTypeProperty =
249
                DependencyProperty.Register("ControlType", typeof(ControlType), typeof(RectangleControl), new FrameworkPropertyMetadata(ControlType.Rectangle));
250

    
251
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
252
                "StrokeColor", typeof(SolidColorBrush), typeof(RectangleControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
253

    
254
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
255
              "LineSize", typeof(double), typeof(RectangleControl), new PropertyMetadata((Double)2));
256

    
257
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
258
               "PathData", typeof(Geometry), typeof(RectangleControl), null);
259

    
260
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
261
               "OverViewPathData", typeof(Geometry), typeof(RectangleControl), null);
262

    
263

    
264

    
265
        public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
266
                "FillColor", typeof(SolidColorBrush), typeof(RectangleControl), new PropertyMetadata(new SolidColorBrush(Colors.White)));
267

    
268
        public static readonly DependencyProperty CanvasXProperty = DependencyProperty.Register(
269
                 "CanvasX", typeof(double), typeof(RectangleControl), new PropertyMetadata((double)0, OnSetCansvasChanged));
270

    
271
        public static readonly DependencyProperty CanvasYProperty = DependencyProperty.Register(
272
                "CanvasY", typeof(double), typeof(RectangleControl), new PropertyMetadata((double)0, OnSetCansvasChanged));
273

    
274
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
275
               "TopRightPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
276

    
277
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
278
                 "LeftBottomPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
279

    
280
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
281
            "StartPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
282

    
283
        public static readonly DependencyProperty SetPointProperty = DependencyProperty.Register(
284
        "SetPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
285

    
286
        public static readonly DependencyProperty OriginPointProperty = DependencyProperty.Register(
287
        "OriginPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
288

    
289
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
290
         "PointSet", typeof(List<Point>), typeof(RectangleControl), new PropertyMetadata(new List<Point>()));
291

    
292
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
293
              "EndPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
294

    
295
        #endregion  Dependency Property
296

    
297
        #region Dependency PropertyChanged
298
        public static void OnSetCansvasChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
299
        {
300
            var instance = (RectangleControl)sender;
301

    
302
            if (e.OldValue != e.NewValue && instance != null)
303
            {
304
                instance.SetValue(e.Property, e.NewValue);
305
                Canvas.SetLeft(instance, instance.SetPoint.X);
306
                Canvas.SetTop(instance, instance.SetPoint.Y);
307
                //Canvas.SetLeft(instance, instance.CanvasX);
308
                //Canvas.SetTop(instance, instance.CanvasY);
309
            }
310
        }
311

    
312

    
313
        public static void OnUpdateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
314
        {
315
            var instance = (RectangleControl)sender;
316

    
317
            if (e.OldValue != e.NewValue && instance != null)
318
            {
319
                instance.SetValue(e.Property, e.NewValue);
320

    
321
                instance.SetRectPath(instance);
322
            }
323
        }
324

    
325
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
326
        {
327
            var instance = (RectangleControl)sender;
328

    
329
            if (e.OldValue != e.NewValue && instance.Base_RectPath != null)
330
            {
331
                instance.SetValue(e.Property, e.NewValue);
332

    
333
                if (instance.IsSelected)
334
                {
335
                    instance.Base_RectPath.Stroke = new SolidColorBrush(Colors.Blue);
336
                }
337
                else
338
                {
339
                    instance.Base_RectPath.Stroke = new SolidColorBrush(Colors.Red);
340
                }
341
            }
342
        }
343

    
344
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
345
        {
346
            var instance = (RectangleControl)sender;
347

    
348
            if (e.OldValue != e.NewValue && instance.Base_RectPath != null)
349
            {
350
                instance.SetValue(e.Property, e.NewValue);
351

    
352
                instance.SetRectPath(instance);
353
            }
354
        }
355
        #endregion Dependency PropertyChanged
356

    
357
        public void SetRectPath(RectangleControl instance)
358
        {
359
            this.ApplyTemplate();
360

    
361
            if (Base_RectPath != null)
362
            {
363
                Base_RectPath.StrokeDashCap = PenLineCap.Square;
364

    
365
                PathFigure pathFigure = new PathFigure
366
                {
367
                    IsClosed = true
368
                };
369

    
370
                pathFigure.StartPoint = this.StartPoint;
371

    
372
                LineSegment lineSegment1 = new LineSegment();
373
                lineSegment1.Point = this.LeftBottomPoint;
374
                pathFigure.Segments.Add(lineSegment1);
375

    
376
                LineSegment lineSegment2 = new LineSegment();
377
                lineSegment2.Point = this.EndPoint;
378
                pathFigure.Segments.Add(lineSegment2);
379

    
380
                LineSegment lineSegment3 = new LineSegment();
381
                lineSegment3.Point = this.TopRightPoint;
382
                pathFigure.Segments.Add(lineSegment3);
383

    
384
                LineSegment lineSegment0 = new LineSegment();
385
                lineSegment0.Point = this.StartPoint;
386
                pathFigure.Segments.Add(lineSegment0);
387

    
388
                PathGeometry pathGeometry = new PathGeometry();
389
                pathGeometry.Figures = new PathFigureCollection();
390
                pathGeometry.Figures.Add(pathFigure);
391

    
392
                this.FillColor = null;
393
                this.PathData = pathGeometry;
394
                ApplyOverViewData();
395
                //OverViewPathData = PathData;
396
                Adorner.AdornerControl adornerControl = new Adorner.AdornerControl();
397
                //adornerControl
398

    
399
                adornerControl.Content = pathGeometry;
400

    
401
            }
402
        }
403

    
404
        public void ApplyOverViewData()
405
        {
406
            this.OverViewPathData = this.PathData;
407
        }
408

    
409
        #region Internal Method
410

    
411

    
412
        static RectangleControl()
413
        {
414
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RectangleControl), new FrameworkPropertyMetadata(typeof(RectangleControl)));
415
            ResourceDictionary dictionary = new ResourceDictionary();
416
            dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
417
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
418
        }
419

    
420

    
421
        public override void OnApplyTemplate()
422
        {
423
            base.OnApplyTemplate();
424

    
425
            Base_RectPath = GetTemplateChild(PART_RectPath) as Path;
426

    
427
            if (Base_RectPath == null)
428
                return;
429

    
430
            //SetRectPath();
431
        }
432

    
433
        public void updateControl()
434
        {
435
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
436
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
437
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
438
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
439
        }
440

    
441
        private void OnPropertyChanged(string name)
442
        {
443
            if (PropertyChanged != null)
444
            {
445
                PropertyChanged(this, new PropertyChangedEventArgs(name));
446
            }
447
        }
448

    
449
        public event PropertyChangedEventHandler PropertyChanged;
450

    
451
        public void Dispose()
452
        {
453
            GC.Collect();
454
            GC.SuppressFinalize(this);
455
        }
456
        #endregion Internal Method
457
    }
458
}
클립보드 이미지 추가 (최대 크기: 500 MB)