领胜LDS 键盘AOI检测项目
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
using Bro.Common.Factory;
using Bro.Common.Helper;
using Bro.Common.Interface;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Bro.Common.Helper.EnumHelper;
 
namespace Bro.UI.Model.Winform
{
    public partial class Canvas : UserControl
    {
        readonly CanvasImage cvImage = new CanvasImage();
 
        #region Grid
        readonly GridCtrl gridCtrl = new GridCtrl();
 
        private void GridValueChanged(int gridValue)
        {
            cvImage.GridValue = gridValue;
        }
 
        private void ShowGridChanged(bool isShowGrid)
        {
            cvImage.ShowGrid = isShowGrid;
        }
 
        private void GridColorChanged(Color obj)
        {
            cvImage.Pen_Grid.Color = obj;
        }
        #endregion
 
        public Canvas()
        {
            InitializeComponent();
            DoubleBuffered = true;
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                            ControlStyles.ResizeRedraw |
                            ControlStyles.AllPaintingInWmPaint, true);
 
            cvImage.Dock = DockStyle.Fill;
            scMain.Panel1.Controls.Add(cvImage);
 
            dgElements.AutoGenerateColumns = false;
 
            var checkHead = new DataGridViewCheckboxHeaderCell();
            checkHead.OnCheckBoxClicked += CheckHead_OnCheckBoxClicked;
            dgElements.Columns[0].HeaderCell = checkHead;
            dgElements.Columns[0].HeaderCell.Value = string.Empty;
 
            Elements.CollectionChanged += Elements_CollectionChanged;
 
            cvImage.OnMouseLocationUpdated = OnMouseLocationUpdated;
 
            gridCtrl.IsShowGridChanged = ShowGridChanged;
            gridCtrl.GridValueChanged = GridValueChanged;
            gridCtrl.GridColorChanged = GridColorChanged;
            gridCtrl.Padding = new Padding(0, 5, 0, 5);
            tsTool.Items.Add(new GridCtrlHost(gridCtrl));
            tsTool.Invalidate();
        }
 
        #region 底部状态栏
        public bool IsShowStatusBar
        {
            get => stsStatus.Visible;
            set => stsStatus.Visible = value;
        }
 
        private void OnMouseLocationUpdated(Point screenPoint, Point imagePoint, string colorDesc)
        {
            //await Task.Run(() => tsslLocation.Text = $"屏幕坐标X:{screenPoint.X},Y:{screenPoint.Y}    图片坐标X:{imagePoint.X},Y:{imagePoint.Y}   颜色:{colorDesc}");
            this.Invoke(new Action(() =>
            {
                tsslLocation.Text = $"屏幕坐标X:{screenPoint.X},Y:{screenPoint.Y}    图片坐标X:{imagePoint.X},Y:{imagePoint.Y}   颜色:{colorDesc}";
            }));
        }
 
        //private void MouseLocationUpdated(Point screenPoint, Point imagePoint, string colorDesc)
        //{
        //    if (InvokeRequired)
        //    {
        //        Invoke(new Action<Point, Point, string>(MouseLocationUpdated), screenPoint, imagePoint);
        //    }
        //    else
        //    {
        //        tsslLocation.Text = $"屏幕坐标X:{screenPoint.X},Y:{screenPoint.Y}    图片坐标X:{imagePoint.X},Y:{imagePoint.Y}   颜色:{colorDesc}";
        //    }
        //}
        #endregion
 
        #region 属性
        #region CanvasImage相关
        //private MouseState mouseState = MouseState.Normal;
        //public MouseState MouseState
        //{
        //    get
        //    {
        //        return mouseState;
        //    }
        //    set
        //    {
        //        if (mouseState != value)
        //        {
        //            mouseState = value;
 
        //            tsslMouseState.Text = mouseState.ToString();
 
        //            if (mouseState >= MouseState.SelectionZone)
        //            {
        //                tsBtnModeSelection.Checked = true;
        //            }
        //            else
        //            {
        //                tsBtnModeNormal.Checked = true;
        //            }
        //        }
        //    }
        //}
 
        public string ImageFilePath { get; set; }
 
        public Bitmap MAP
        {
            get => cvImage.MAP;
            set => cvImage.MAP = value;
        }
        public Matrix Matrix
        {
            get => cvImage.Matrix;
            set => cvImage.Matrix = value;
        }
        public MouseState MouseState
        {
            get => cvImage.MouseState;
            set => cvImage.MouseState = value;
        }
        public ObservableCollection<IShapeElement> Elements
        {
            get => cvImage.Elements;
        }
        #endregion
 
        private bool isShowElementList = false;
        public bool IsShowElementList
        {
            get => isShowElementList;
            set
            {
                //if (isShowElementList != value)
                {
                    isShowElementList = value;
                    scMain.Panel2Collapsed = !value;
                }
            }
        }
 
        private bool isShowROITool = false;
        public bool IsShowROITool
        {
            get => isShowROITool;
            set
            {
                Task.Run(() =>
                {
                    while (!tsROIs.IsHandleCreated)
                    {
                        Thread.Sleep(100);
                    }
                    tsROIs.Invoke(new Action(() =>
                    {
                        tsROIs.Visible = isShowROITool = value;
                    }));
                });
            }
        }
        #endregion
 
        #region 图片操作
        /// <summary>
        /// 载入图片
        /// </summary>
        /// <param name="map"></param>
        public void LoadImage(Bitmap map)
        {
            cvImage.LoadImage(map);
        }
 
        /// <summary>
        /// 设置图片为原始尺寸
        /// </summary>
        public void SetMapSize()
        {
            cvImage.SetMapSize();
        }
 
        /// <summary>
        /// 设置图片为适配尺寸
        /// </summary>
        public void SetScreenSize()
        {
            cvImage.SetScreenSize();
        }
        #endregion
 
        #region 基元列表
        private void chkShowChecked_CheckedChanged(object sender, EventArgs e)
        {
            //dgElements.DataSource = null;
 
            //if (Elements != null && Elements.Count > 0)
            //{
            //    if (chkShowChecked.Checked)
            //    {
            //        dgElements.DataSource = Elements.Where(u => u.ShowList && u.IsEnabled).ToList();
            //    }
            //    else
            //    {
            //        dgElements.DataSource = Elements.Where(u => u.ShowList).ToList();
            //    }
            //}
        }
 
        private void CheckHead_OnCheckBoxClicked(object sender, DataGridViewCheckboxHeaderEventArgs e)
        {
            foreach (IShapeElement ele in Elements)
            {
                ele.IsEnabled = e.CheckedState;
            }
            OnElementChanged(null);
        }
 
        private void dgElements_SelectionChanged(object sender, EventArgs e)
        {
            if (dgElements.SelectedRows.Count > 0)
            {
                var ele = Elements.FirstOrDefault(u => u.ID == dgElements.SelectedRows[0].Cells["colID"].Value.ToString());
                propGridElement.SelectedObject = ele;
            }
        }
 
        private void dgElements_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (dgElements.SelectedRows.Count > 0)
            //{
            //    var ele = Elements.FirstOrDefault(u => u.ID == dgElements.SelectedRows[0].Cells["colID"].Value.ToString());
 
            //    foreach (ElementBase u in Elements)
            //    {
            //        u.State = ElementState.Normal;
            //    }
 
            //    ele.State = ElementState.Selected;
 
            //    SetDeviceByElement?.Invoke(ele);
 
            //    Invalidate();
            //}
        }
 
        private void Elements_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            OnElementChanged(null);
 
            foreach (IShapeElement ele in Elements)
            {
                ele.PropertyChanged -= Ele_PropertyChanged;
                ele.PropertyChanged += Ele_PropertyChanged;
            }
        }
 
        private void Ele_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnElementChanged(sender as IShapeElement);
        }
 
        private void OnElementChanged(IShapeElement ele)
        {
            if (ele != null)
                OnElementChangedHandle?.Invoke(ele);
 
            if (InvokeRequired)
            {
                Invoke(new Action(() => OnElementChanged(ele)));
            }
            else
            {
                //dgElements.DataSource = null;
                //if (Elements != null && Elements.Count > 0)
                //{
                //    if (chkShowChecked.Checked)
                //    {
                //        dgElements.DataSource = Elements.Where(u => u.IsEnabled && u.ShowList).ToList();
                //    }
                //    else
                //    {
                //        dgElements.DataSource = Elements.Where(u => u.ShowList).ToList();
                //    }
                //}
                Invalidate();
            }
        }
 
        #region 基元列表右键菜单
        private void tsmiResort_Click(object sender, EventArgs e)
        {
            //Elements.Sort();
        }
 
        private void tsmiClearActualValue_Click(object sender, EventArgs e)
        {
            //foreach (IShapeElement ele in Elements)
            //{
            //    if (ele.IsEnabled)
            //    {
            //        ele.SetActualValue(0.0);
            //    }
            //}
        }
 
        private void tsmiInitialState_Click(object sender, EventArgs e)
        {
            //foreach (IShapeElement ele in Elements)
            //{
            //    if (ele.IsEnabled)
            //    {
            //        ele.State = ElementState.Normal;
            //        ele.InitialMeasureResult();
            //    }
            //}
        }
 
        private void tsmiClearStandardValue_Click(object sender, EventArgs e)
        {
            //foreach (IShapeElement ele in Elements)
            //{
            //    if (ele.IsEnabled)
            //    {
            //        ele.SetStandardValue(0.0);
            //    }
            //}
        }
        #endregion
        #endregion
 
        #region 图像区域右键菜单
        private void tsmiKeepUnselected_Click(object sender, EventArgs e)
        {
            KeepElements(false);
        }
 
        private void tsmiKeepSelected_Click(object sender, EventArgs e)
        {
            KeepElements(true);
        }
 
        private void KeepElements(bool isKeepSelected)
        {
            foreach (IShapeElement ele in Elements)
            {
                if (ele.IsEnabled)
                {
                    if (ele.State == ElementState.Selected)
                    {
                        ele.IsEnabled = isKeepSelected;
                    }
                    else
                    {
                        ele.IsEnabled = !isKeepSelected;
                    }
                }
            }
        }
 
        private void tsmiUnselectElements_Click(object sender, EventArgs e)
        {
            //if (MouseState == MouseState.SelectedElement)
            //{
            //    MouseState = MouseState.Normal;
 
            //    //Elements.ForEach(ele =>
            //    foreach (IShapeElement ele in Elements)
            //    {
            //        ele.State = ElementState.Normal;
            //    }
            //    //);
            //}
        }
        #endregion
 
        #region 基元的设备属性 运动设置和相机设置
        public Action<IShapeElement> SetElementDevicePara;
 
        public Action<IShapeElement> SetDeviceByElement;
        #endregion
 
        #region 一般操作工具条
        public bool IsShowToolBar
        {
            get => tsTool.Visible;
            set => tsTool.Visible = value;
        }
 
        private void tsBtnLoadImage_Click(object sender, EventArgs e)
        {
            Thread InvokeThread = new Thread(new ThreadStart(OpenLoadImage));
            InvokeThread.SetApartmentState(ApartmentState.STA);
            InvokeThread.Start();
            InvokeThread.Join();
        }
 
        private void OpenLoadImage()
        {
            ImageFilePath = "";
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                ImageFilePath = ofd.FileName;
                LoadImage((Bitmap)Bitmap.FromFile(ImageFilePath));
            }
        }
 
        private void tsBtnSaveImage_Click(object sender, EventArgs e)
        {
            if (MAP == null)
                return;
 
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "JPG文件|*.jpg|BMP文件|*.bmp";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string filePath = sfd.FileName;
                if (filePath.EndsWith("bmp"))
                {
                    MAP.Save(filePath, ImageFormat.Bmp);
                }
                else
                {
                    MAP.Save(filePath, ImageFormat.Jpeg);
                }
                MessageBox.Show("图片保存成功!");
            }
        }
 
        private void tsBtnSaveImageWithElements_Click(object sender, EventArgs e)
        {
            if (MAP == null)
                return;
 
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "JPG文件|*.jpg|BMP文件|*.bmp";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string filePath = sfd.FileName;
 
                Bitmap bmp = new Bitmap(MAP.Width, MAP.Height, PixelFormat.Format32bppArgb);
                //Bitmap bmp = new Bitmap(MAP.Width, MAP.Height, MAP.PixelFormat);
                //Bitmap bmp = new Bitmap(MAP.Width, MAP.Height);
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.DrawImage(MAP, 0, 0);
 
                    foreach (IShapeElement ele in Elements)
                    {
                        if (ele.IsEnabled)
                            ele.Draw(g);
                    }
                }
 
                if (filePath.EndsWith("bmp"))
                {
                    bmp.Save(filePath, ImageFormat.Bmp);
                }
                else
                {
                    bmp.Save(filePath, ImageFormat.Jpeg);
                }
                MessageBox.Show("带基元图片保存成功!");
            }
        }
 
        private void tsBtnMapSize_Click(object sender, EventArgs e)
        {
            SetMapSize();
        }
 
        private void tsBtnModeNormal_CheckedChanged(object sender, EventArgs e)
        {
            var btn = sender as ToolStripButton;
            if (btn.Checked)
            {
                btn.BackColor = SystemColors.ActiveCaption;
 
                foreach (ToolStripItem c in tsTool.Items)
                {
                    if (c is ToolStripButton)
                    {
                        if (c.Name.StartsWith("tsBtnMode") && c.Name != btn.Name)
                        {
                            var temp = c as ToolStripButton;
                            temp.Checked = false;
                            temp.BackColor = SystemColors.Control;
                        }
                    }
                }
 
                switch (btn.Name)
                {
                    case "tsBtnModeNormal":
                        MouseState = MouseState.Normal;
                        break;
                    case "tsBtnModeSelection":
                        MouseState = MouseState.SelectionZone;
                        break;
                }
            }
        }
 
        private void tsBtnScreenSize_Click(object sender, EventArgs e)
        {
            SetScreenSize();
        }
        #endregion
 
        #region 基元工具条
        private void LoadElementTools()
        {
            var eleDict = ElementFactory.GetAllElementsInfo();
 
            foreach (KeyValuePair<ElementAttribute, Type> pair in eleDict)
            {
 
            }
        }
        #endregion
 
        #region 事件
        public event Action<IShapeElement> OnElementChangedHandle;
        #endregion
    }
}