领胜LDS 键盘AOI检测项目
patrick.xu
2021-03-06 8a3ab64a65da226636743be07c4bee63f50da25d
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
using Bro.Common.Helper;
using Bro.Common.Interface;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
using static Bro.Common.Helper.EnumHelper;
 
namespace Bro.UI.Model.Winform
{
    public partial class CanvasImage : UserControl
    {
        public CanvasImage()
        {
            InitializeComponent();
 
            DoubleBuffered = true;
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                            ControlStyles.ResizeRedraw |
                            ControlStyles.AllPaintingInWmPaint, true);
 
            MouseWheel += Canvas_MouseWheel;
            KeyDown += OnCanvasKeyDown;
            KeyPress += OnCanvasKeyPressed;
            MouseDoubleClick += Canvas_MouseDoubleClick;
            MouseDown += Canvas_MouseDown;
            MouseMove += Canvas_MouseMove;
            MouseUp += Canvas_MouseUp;
 
            EventRouter.ChangeElementsMouseState -= OnElementChangeMouseState;
            EventRouter.ChangeElementsMouseState += OnElementChangeMouseState;
        }
 
        #region Event
        public Action<MouseState> OnMouseStateChanged;
        public Action<IShapeElement> DrawTemplateChanged = null;
        public Action<Point, Point, string> OnMouseLocationUpdated;
        #endregion
 
        private MouseState mouseState = MouseState.Normal;
        public MouseState MouseState
        {
            get
            {
                return mouseState;
            }
            set
            {
                if (mouseState != value)
                {
                    mouseState = value;
                    OnMouseStateChanged?.BeginInvoke(value, null, null);
                }
            }
        }
 
        #region 属性和字段
        public Bitmap MAP { get; set; } = new Bitmap(10, 10);
        public Matrix Matrix { get; set; } = new Matrix();
        public ObservableCollection<IShapeElement> Elements { get; set; } = new ObservableCollection<IShapeElement>();
 
        Rectangle _selectionRect = new Rectangle();
        #endregion
 
        #region 重绘
        volatile bool _isInRepaint = false;
        object _isInRepaintLock = new object();
        protected override void OnPaint(PaintEventArgs e)
        {
            //if (_isInRepaint)
            //    return;
 
            //lock (_isInRepaintLock)
            //{
            //    if (_isInRepaint)
            //        return;
            //}
 
            //_isInRepaint = true;
 
            try
            {
                Rectangle rect = ClientRectangle;
                Graphics originG = e.Graphics;
                BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
                BufferedGraphics myBuffer = currentContext.Allocate(originG, rect);
                Graphics g = myBuffer.Graphics;
                g.SmoothingMode = SmoothingMode.HighSpeed;
                g.PixelOffsetMode = PixelOffsetMode.HighSpeed;
                g.InterpolationMode = InterpolationMode.Low;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
                g.CompositingQuality = CompositingQuality.HighSpeed;
                g.Clear(BackColor);
 
                g.MultiplyTransform(Matrix);
 
                if (MAP != null)
                {
                    try
                    {
                        g.DrawImage(MAP, 0, 0, MAP.Width, MAP.Height);
                    }
                    catch (Exception ex)
                    {
                    }
                }
 
                DrawTemplate?.Draw(g);
                foreach (IShapeElement ele in Elements)
                {
                    if (ele.IsEnabled && ele.IsShowing)
                    {
                        ele.Draw(g);
                    }
                }
 
                #region Grid
                if (MAP != null)
                {
                    if (ShowGrid)
                    {
                        int baseX = MAP.Width / 2;
                        int baseY = MAP.Height / 2;
 
                        Point[] xPoint = new Point[] { new Point(0, baseY), new Point(MAP.Width, baseY) };
                        Point[] yPoint = new Point[] { new Point(baseX, 0), new Point(baseX, MAP.Height) };
 
                        g.DrawLine(new Pen(Pen_Grid.Color, 5.0f), xPoint[0], xPoint[1]);
                        g.DrawLine(new Pen(Pen_Grid.Color, 5.0f), yPoint[0], yPoint[1]);
 
                        if (GridValue > 0)
                        {
                            int stepX = MAP.Width / 2 / (GridValue * MAP.Width / 2 / _minGridStep / 10);
                            int stepY = MAP.Height / 2 / (GridValue * MAP.Height / 2 / _minGridStep / 10);
 
                            //int stepX = _minGridStep + (10 - GridValue) * (MAP.Width / 2 - _minGridStep) / 10;
                            //int stepY = _minGridStep + (10 - GridValue) * (MAP.Height / 2 - _minGridStep) / 10;
 
                            int yPositive = baseY;
                            do
                            {
                                xPoint = new Point[] { new Point(0, yPositive), new Point(MAP.Width, yPositive) };
                                g.DrawLine(Pen_Grid, xPoint[0], xPoint[1]);
                                yPositive -= stepY;
                            } while (yPositive > 0);
 
                            int yNegative = baseY;
                            do
                            {
                                xPoint = new Point[] { new Point(0, yNegative), new Point(MAP.Width, yNegative) };
                                g.DrawLine(Pen_Grid, xPoint[0], xPoint[1]);
                                yNegative += stepY;
                            } while (yNegative < MAP.Height);
 
                            int xPositive = baseX;
                            do
                            {
                                yPoint = new Point[] { new Point(xPositive, 0), new Point(xPositive, MAP.Height) };
                                g.DrawLine(Pen_Grid, yPoint[0], yPoint[1]);
                                xPositive -= stepX;
                            } while (xPositive > 0);
 
                            int xNegative = baseX;
                            do
                            {
                                yPoint = new Point[] { new Point(xNegative, 0), new Point(xNegative, MAP.Height) };
                                g.DrawLine(Pen_Grid, yPoint[0], yPoint[1]);
                                xNegative += stepX;
                            } while (xNegative < MAP.Width);
                        }
                    }
                }
                #endregion
 
                if (MouseState == MouseState.SelectionZoneDoing)
                {
                    g.DrawRectangle(Pens.AliceBlue, _selectionRect);
                    g.FillRectangle(new SolidBrush(Color.FromArgb(40, 0, 0, 255)), _selectionRect);
                }
 
                myBuffer.Render(originG);
                g.Dispose();
                myBuffer.Dispose();//释放资源
            }
            catch (Exception)
            {
            }
 
            _isInRepaint = false;
        }
 
        private void halfTransparent()
        {
        }
        #endregion
 
        #region 绘制类型
        private IShapeElement drawTemplate = null;
        public IShapeElement DrawTemplate
        {
            get
            {
                return drawTemplate;
            }
            set
            {
                if (drawTemplate != value)
                {
 
                    drawTemplate = value;
                    DrawTemplateChanged?.BeginInvoke(value, null, null);
 
                    if (value == null)
                    {
                        MouseState = MouseState.Normal;
                        return;
                    }
 
                    MouseState = MouseState.New;
 
                    var existed = Elements.FirstOrDefault(e => e.ID == value.ID);
                    if (existed != null)
                    {
                        Elements.Remove(existed);
                    }
 
                    //if (DrawTemplate != null)
                    //{
                    //    DrawTemplate.OnDrawDone += OnElementDrawDone;
                    //}
                }
            }
        }
 
        string currentElementId = "";
        string CurrentElementId
        {
            get
            {
                return currentElementId;
            }
            set
            {
                if (currentElementId != value)
                {
                    currentElementId = value;
                }
            }
        }
 
        private void OnElementDrawDone(IShapeElement ele)
        {
            //int maxIndex = 1;
            //if (Elements.Count > 0)
            //{
            //    maxIndex = Elements.Max(u => u.Index) + 1;
            //}
            //ele.Index = maxIndex;
            //ele.Name = maxIndex.ToString();
 
            //#region 获取基元的设备属性,目前包括运动坐标和相机参数
            //SetElementDevicePara?.Invoke(ele);
            //#endregion
 
            //Elements.Add(ele);
            //DrawTemplate = DrawTemplate?.Clone() as IShapeElement;
        }
        #endregion
 
        #region 状态变换
        private void OnElementChangeMouseState(IShapeElement ele, ElementState preState, ElementState curState)
        {
            if (curState != ElementState.Normal)
            {
                switch (curState)
                {
                    case ElementState.New:
                        MouseState = MouseState.New;
                        break;
                    case ElementState.Selected:
                        CurrentElementId = ele.ID;
                        Cursor = Cursors.Default;
                        break;
                    case ElementState.Moving:
                        MouseState = MouseState.MoveElement;
                        Cursor = Cursors.NoMove2D;
                        break;
                    case ElementState.MouseInSide:
                        MouseState = MouseState.InSideElement;
                        break;
                    case ElementState.MouseHover:
                        MouseState = MouseState.HoverElement;
                        break;
 
                    case ElementState.CanStretchLeft:
                        Cursor = Cursors.SizeWE;
                        break;
                    case ElementState.StretchingLeft:
                        MouseState = MouseState.StretchingLeft;
                        Cursor = Cursors.SizeWE;
                        break;
                    case ElementState.CanStretchBottom:
                        Cursor = Cursors.SizeNS;
                        break;
                    case ElementState.StretchingBottom:
                        MouseState = MouseState.StretchingBottom;
                        Cursor = Cursors.SizeNS;
                        break;
                    case ElementState.CanStretchRight:
                        Cursor = Cursors.SizeWE;
                        break;
                    case ElementState.StretchingRight:
                        MouseState = MouseState.StretchingRight;
                        Cursor = Cursors.SizeWE;
                        break;
                    case ElementState.CanStretchTop:
                        Cursor = Cursors.SizeNS;
                        break;
                    case ElementState.StretchingTop:
                        MouseState = MouseState.StretchingTop;
                        Cursor = Cursors.SizeNS;
                        break;
 
                    case ElementState.CanStretchLeftLowerCorner:
                        Cursor = Cursors.SizeNESW;
                        break;
                    case ElementState.StretchingLeftLowerCorner:
                        MouseState = MouseState.StretchingLeftLowerCorner;
                        Cursor = Cursors.SizeNESW;
                        break;
                    case ElementState.CanStretchLeftUpperCorner:
                        Cursor = Cursors.SizeNWSE;
                        break;
                    case ElementState.StretchingLeftUpperCorner:
                        MouseState = MouseState.StretchingLeftUpperCorner;
                        Cursor = Cursors.SizeNWSE;
                        break;
                    case ElementState.CanStretchRightLowerCorner:
                        Cursor = Cursors.SizeNWSE;
                        break;
                    case ElementState.StretchingRightLowerCorner:
                        MouseState = MouseState.StretchingRightLowerCorner;
                        Cursor = Cursors.SizeNWSE;
                        break;
                    case ElementState.CanStretchRightUpperCorner:
                        Cursor = Cursors.SizeNESW;
                        break;
                    case ElementState.StretchingRightUpperCorner:
                        MouseState = MouseState.StretchingRightUpperCorner;
                        Cursor = Cursors.SizeNESW;
                        break;
                    default:
                        MouseState = MouseState.Normal;
                        break;
                }
            }
            else
            {
                if (Elements.All(e => e.State == ElementState.Normal))
                {
                    CurrentElementId = null;
 
                    if (preState == ElementState.Selected)
                    {
                        DrawTemplate = null;
                    }
                    else if (DrawTemplate != null)
                    {
                        MouseState = MouseState.New;
                        return;
                    }
 
                    MouseState = MouseState.Normal;
                }
            }
 
            this.Invalidate();
        }
        #endregion
 
        #region 鼠标动作
        private void Canvas_MouseWheel(object sender, MouseEventArgs e)
        {
            Point prePoint = ToMapPoint(e.Location);
 
            //先缩放
            if (e.Delta > 0)
            {
                Matrix.Scale((float)1.1, (float)1.1);
            }
            else
            {
                Matrix.Scale((float)0.9, (float)0.9);
            }
 
            Point afterPoint = ToMapPoint(e.Location);
 
            //后平移
            Matrix.Translate(afterPoint.X - prePoint.X, afterPoint.Y - prePoint.Y);
 
            Invalidate();
        }
 
        Point startPoint, currentPoint;
        bool _isMouseBtnPressing = false;
 
        private void Canvas_MouseDown(object sender, MouseEventArgs e)
        {
            Point p = ToMapPoint(e.Location);
 
            if (e.Button == MouseButtons.Left)
            {
                _isMouseBtnPressing = true;
 
                switch (MouseState)
                {
                    case MouseState.Normal:
                        startPoint = e.Location;
                        break;
                    case MouseState.StretchingLeft:
                        break;
                    case MouseState.StretchingRight:
                        break;
                    case MouseState.StretchingTop:
                        break;
                    case MouseState.StretchingBottom:
                        break;
                    case MouseState.MoveElement:
                        break;
                    case MouseState.HoverElement:
                    case MouseState.InSideElement:
                    case MouseState.New:
                        DrawTemplate?.OnMouseDown(p);
                        break;
                    case MouseState.Editing:
                        break;
                    case MouseState.SelectionZone:
                        MouseState = MouseState.SelectionZoneDoing;
                        startPoint = p;
                        break;
                }
 
                foreach (IShapeElement ele in Elements)
                {
                    ele.OnMouseDown(p);
                }
            }
        }
 
        private void Canvas_MouseUp(object sender, MouseEventArgs e)
        {
            _isMouseBtnPressing = false;
            switch (MouseState)
            {
                case MouseState.Normal:
                    break;
                case MouseState.HoverElement:
                    break;
                case MouseState.InSideElement:
                    break;
                case MouseState.StretchingLeft:
                    break;
                case MouseState.StretchingRight:
                    break;
                case MouseState.StretchingTop:
                    break;
                case MouseState.StretchingBottom:
                    break;
                case MouseState.MoveElement:
                    //MouseState = MouseState.SelectedElement;
                    break;
                case MouseState.New:
                    break;
                case MouseState.Editing:
                    break;
                case MouseState.MovingAll:
                    MouseState = MouseState.Normal;
                    break;
                case MouseState.SelectionZone:
                    break;
                case MouseState.SelectionZoneDoing:
                    MouseState = MouseState.SelectionZone;
 
                    foreach (IShapeElement ele in Elements)
                    {
                        if (ele.IsIntersect(_selectionRect))
                        {
                            ele.State = ElementState.Selected;
                        }
                    }
 
                    break;
            }
 
            Cursor = Cursors.Default;
 
            Point p = ToMapPoint(e.Location);
            DrawTemplate?.OnMouseUp(p);
            foreach (IShapeElement ele in Elements)
            {
                ele.OnMouseUp(p);
            }
        }
 
        private void Canvas_MouseMove(object sender, MouseEventArgs e)
        {
            Point p = ToMapPoint(e.Location);
 
            switch (MouseState)
            {
                case MouseState.Normal:
                    {
                        if (_isMouseBtnPressing)
                        {
                            currentPoint = e.Location;
                            Point p1 = ToMapPoint(startPoint);
                            Point p2 = ToMapPoint(currentPoint);
                            Matrix.Translate(p2.X - p1.X, p2.Y - p1.Y);
                            startPoint = e.Location;
                        }
                    }
                    break;
                case MouseState.StretchingLeft:
                    break;
                case MouseState.StretchingRight:
                    break;
                case MouseState.StretchingTop:
                    break;
                case MouseState.StretchingBottom:
                    break;
                case MouseState.MoveElement:
                    break;
                case MouseState.HoverElement:
                case MouseState.InSideElement:
                case MouseState.New:
                    DrawTemplate?.OnMouseMove(p);
                    break;
                case MouseState.Editing:
                    break;
                case MouseState.MovingAll:
                    break;
                case MouseState.SelectionZoneDoing:
                    {
                        currentPoint = p;
 
                        int[] x2 = new int[2] { startPoint.X, currentPoint.X };
                        int[] y2 = new int[2] { startPoint.Y, currentPoint.Y };
 
                        int xMin = x2.Min();
                        int xMax = x2.Max();
                        int yMin = y2.Min();
                        int yMax = y2.Max();
 
                        _selectionRect = new Rectangle(xMin, yMin, xMax - xMin, yMax - yMin);
                    }
                    break;
            }
 
            Point mapPoint = ToMapPoint(e.Location);
            Color color = Color.Transparent;
            if (MAP != null && mapPoint.X > 0 && mapPoint.X < MAP.Width && mapPoint.Y > 0 && mapPoint.Y < MAP.Height)
            {
                color = MAP.GetPixel(mapPoint.X, mapPoint.Y);
            }
            OnMouseLocationUpdated?.BeginInvoke(e.Location, mapPoint, color.Name, null, null);
 
            if (MouseState != MouseState.SelectionZoneDoing)
            {
                foreach (IShapeElement ele in Elements)
                {
                    ele.OnMouseMove(p);
                }
            }
 
            Invalidate();
        }
 
        private void Canvas_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Point p = ToMapPoint(e.Location);
 
            if (e.Button == MouseButtons.Left)
            {
                switch (MouseState)
                {
                    //case MouseState.SelectedElement:
                    case MouseState.HoverElement:
                    case MouseState.InSideElement:
                    case MouseState.MoveElement:
                    case MouseState.Normal:
                        //Elements.ForEach(ele =>
                        foreach (IShapeElement ele in Elements)
                        {
                            ele.OnMouseDoubleClick(p);
                        }
                        //);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                //if (MouseState == MouseState.SelectedElement)
                {
                    MouseState = MouseState.Normal;
 
                    //Elements.ForEach(ele =>
                    foreach (IShapeElement ele in Elements)
                    {
                        ele.State = ElementState.Normal;
                    }
                    //);
                }
            }
        }
        #endregion
 
        #region 图片操作
        bool _firstLoad = true;
 
        /// <summary>
        /// 载入图片
        /// </summary>
        /// <param name="map"></param>
        public void LoadImage(Bitmap map)
        {
            if (map == null)
                return;
 
            MAP?.Dispose();
            MAP = null;
 
            MAP = map;
            //MAP = map;
 
            if (_firstLoad)
            {
                SetScreenSize();
                _firstLoad = false;
            }
            Invalidate();
        }
 
        /// <summary>
        /// 设置图片为原始尺寸
        /// </summary>
        public void SetMapSize()
        {
            Matrix = new Matrix();
            Invalidate();
        }
 
        /// <summary>
        /// 设置图片为适配尺寸
        /// </summary>
        public void SetScreenSize()
        {
            try
            {
                if (MAP == null)
                    return;
 
                Matrix = new Matrix();
 
                //先缩放
                List<float> ratios = new List<float>() { MAP.Width / (float)Width, MAP.Height / (float)Height };
                float ratio = 1 / ratios.Max();
                Matrix.Scale(ratio, ratio);
 
                //再平移
                //将plMain的中心转换为图片坐标
                Point screenCenter = new Point(Width / 2, Height / 2);
                Point mapPoint = ToMapPoint(screenCenter);
 
                //目标坐标减去当前坐标
                Matrix.Translate(-MAP.Width / 2 + mapPoint.X, -MAP.Height / 2 + mapPoint.Y);
 
                Invalidate();
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.GetExceptionMessage());
            }
        }
        #endregion
 
        #region 私有方法
        //private void DisplayMouseLocation(Point location)
        //{
        //    string screenPoint = string.Format("屏幕坐标X:{0};Y:{1}", location.X, location.Y);
 
        //    Point mapPoint = ToMapPoint(location);
        //    string mapPointStr = string.Format("图片坐标X:{0};Y:{1}", mapPoint.X, mapPoint.Y);
 
        //    tsslLocation.Text = screenPoint + "       " + mapPointStr;
        //}
 
        private Point ToMapPoint(Point p)
        {
            Point[] ps = new Point[] { p };
            Matrix invertMatrix = Matrix.Clone();
            invertMatrix.Invert();
            invertMatrix.TransformPoints(ps);
 
            return ps[0];
        }
 
        private Point ToScreenPoint(Point p)
        {
            Point[] ps = new Point[] { p };
            Matrix.TransformPoints(ps);
 
            return ps[0];
        }
 
        #endregion
 
        #region 按键操作
        public void OnCanvasKeyPressed(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar == 27)  //Esc
            //{
            //    //if (MouseState == MouseState.SelectedElement)
            //    {
            //        MouseState = MouseState.Normal;
 
            //        //Elements.ForEach(ele =>
            //        foreach (IShapeElement ele in Elements)
            //        {
            //            ele.State = ElementState.Normal;
            //        }
            //        //);
            //    }
            //}
 
            //Invalidate();
        }
 
        public void OnCanvasKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 46) //delete键
            {
                //Elements.RemoveAll(u => u.ID == CurrentElementId);
 
                Elements.Remove(Elements.FirstOrDefault(u => u.ID == CurrentElementId));
            }
 
            Invalidate();
        }
 
        public void OnCanvasKeyDown(object sender, Keys key)
        {
            switch (key)
            {
                case Keys.Delete:
                    //Elements.RemoveAll(u => u.ID == CurrentElementId);
                    Elements.Remove(Elements.FirstOrDefault(u => u.ID == CurrentElementId));
                    break;
                case Keys.Escape:
                    //if (MouseState == MouseState.SelectedElement)
                    {
                        MouseState = MouseState.Normal;
 
                        //Elements.ForEach(ele =>
                        foreach (IShapeElement ele in Elements)
                        {
                            ele.State = ElementState.Normal;
                        }
                        //);
                    }
                    break;
            }
 
            Invalidate();
        }
        #endregion
 
        #region 基元的设备属性 运动设置和相机设置
        public Action<IShapeElement> SetElementDevicePara;
 
        public Action<IShapeElement> SetDeviceByElement;
        #endregion
 
        #region Grid
        private bool showGrid = false;
        public bool ShowGrid
        {
            get => showGrid;
            set
            {
                showGrid = value;
                Invalidate();
            }
        }
 
        private int gridValue = 0;
        public int GridValue
        {
            get => gridValue;
            set
            {
                gridValue = value;
                Invalidate();
            }
        }
 
        private Pen penGrid = new Pen(Color.FromArgb(120, Color.Red), 1.0f);
        public Pen Pen_Grid
        {
            get => penGrid;
            set
            {
                penGrid = value;
                Invalidate();
            }
        }
 
        readonly int _minGridStep = 10;
        #endregion
 
        #region Dispose
        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            MAP?.Dispose();
            Matrix?.Dispose();
            penGrid?.Dispose();
 
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #endregion
    }
}