From 72b96782d06c97b5a2a86b334033f59b56d07eaf Mon Sep 17 00:00:00 2001 From: zqm Date: Wed, 25 Mar 2026 15:09:30 +0800 Subject: [PATCH] =?UTF-8?q?Led=E5=8C=BA=E7=BC=96=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CS/Framework4.0/Camera/Camera/Camera.cs | 148 +++++-- .../Camera/Camera/Setting.Designer.cs | 14 +- .../CS/Framework4.0/Camera/Camera/Setting.cs | 365 ++++++++++++++---- 3 files changed, 435 insertions(+), 92 deletions(-) diff --git a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs index 6c9b899..c4e127c 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Concurrent; +using System.Collections.Generic; using System.Drawing; using System.Timers; using System.Net; @@ -25,9 +27,9 @@ namespace Camera // 配置数据 private Rectangle _detectionZone; - private Rectangle _ledZone; + private ConcurrentDictionary _ledZones = new ConcurrentDictionary(); + private ConcurrentDictionary _ledZoneColors = new ConcurrentDictionary(); private Color _detectionZoneColor = Color.Black; - private Color _ledZoneColor = Color.Lime; // 定时器 private System.Timers.Timer _timer; @@ -135,11 +137,61 @@ namespace Camera } /// - /// 获取Led区域 + /// 获取Led区域列表 + /// + public ConcurrentDictionary GetLedZones() + { + return _ledZones; + } + + /// + /// 获取指定索引的Led区域 + /// + public Rectangle GetLedZone(int index) + { + if (_ledZones.ContainsKey(index)) + return _ledZones[index]; + return new Rectangle(0, 0, 0, 0); + } + + /// + /// 获取默认Led区域(索引为1) /// public Rectangle GetLedZone() { - return _ledZone; + if (_ledZones.ContainsKey(1)) + return _ledZones[1]; + return new Rectangle(0, 0, 0, 0); + } + + /// + /// 设置默认Led区域(索引为1) + /// + public void SetLedZone(Rectangle zone) + { + _ledZones[1] = zone; + if (!_ledZoneColors.ContainsKey(1)) + { + _ledZoneColors[1] = Color.Lime; + } + } + + /// + /// 获取默认Led区域颜色(索引为1) + /// + public Color GetLedZoneColor() + { + if (_ledZoneColors.ContainsKey(1)) + return _ledZoneColors[1]; + return Color.Lime; + } + + /// + /// 设置默认Led区域颜色(索引为1) + /// + public void SetLedZoneColor(Color color) + { + _ledZoneColors[1] = color; } /// @@ -151,11 +203,53 @@ namespace Camera } /// - /// 设置Led区域 + /// 添加Led区域 /// - public void SetLedZone(Rectangle zone) + public void AddLedZone(int index, Rectangle zone, Color color) { - _ledZone = zone; + _ledZones[index] = zone; + _ledZoneColors[index] = color; + } + + /// + /// 设置指定索引的Led区域 + /// + public void SetLedZone(int index, Rectangle zone) + { + if (_ledZones.ContainsKey(index)) + { + _ledZones[index] = zone; + } + } + + /// + /// 移除Led区域 + /// + public void RemoveLedZone(int index) + { + Rectangle r; + Color c; + _ledZones.TryRemove(index, out r); + _ledZoneColors.TryRemove(index, out c); + } + + /// + /// 设置指定索引的Led区域颜色 + /// + public void SetLedZoneColor(int index, Color color) + { + if (_ledZoneColors.ContainsKey(index)) + { + _ledZoneColors[index] = color; + } + } + + /// + /// 获取Led区域数量 + /// + public int GetLedZoneCount() + { + return _ledZones.Count; } public Color GetDetectionZoneColor() @@ -163,19 +257,24 @@ namespace Camera return _detectionZoneColor; } + public ConcurrentDictionary GetLedZoneColors() + { + return _ledZoneColors; + } + public void SetDetectionZoneColor(Color color) { _detectionZoneColor = color; } - public Color GetLedZoneColor() + /// + /// 获取指定索引的Led区域颜色 + /// + public Color GetLedZoneColor(int index) { - return _ledZoneColor; - } - - public void SetLedZoneColor(Color color) - { - _ledZoneColor = color; + if (_ledZoneColors.ContainsKey(index)) + return _ledZoneColors[index]; + return Color.Lime; } /// @@ -202,10 +301,13 @@ namespace Camera writer.WriteLine("索引,X坐标,Y坐标,宽度,高度,颜色"); - if (_ledZone.Width > 0 && _ledZone.Height > 0) + foreach (var kvp in _ledZones) { - string colorStr = ColorTranslator.ToHtml(_ledZoneColor); - writer.WriteLine(string.Format("1,{0},{1},{2},{3},{4}", _ledZone.X, _ledZone.Y, _ledZone.Width, _ledZone.Height, colorStr)); + int index = kvp.Key; + Rectangle ledZone = kvp.Value; + Color ledColor = _ledZoneColors[index]; + string colorStr = ColorTranslator.ToHtml(ledColor); + writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, colorStr)); } } } @@ -220,9 +322,9 @@ namespace Camera if (string.IsNullOrEmpty(_configPath)) return; _detectionZone = new Rectangle(0, 0, 2880, 1620); - _ledZone = new Rectangle(0, 0, 0, 0); + _ledZones.Clear(); + _ledZoneColors.Clear(); _detectionZoneColor = Color.Black; - _ledZoneColor = Color.Lime; string configFile = Path.Combine(_configPath, "Camera.csv"); try @@ -263,17 +365,19 @@ namespace Camera else { if (!int.TryParse(parts[0], out int index)) continue; - if (index == 1 && parts.Length >= 5) + if (parts.Length >= 6) { if (!int.TryParse(parts[1], out int x)) continue; if (!int.TryParse(parts[2], out int y)) continue; if (!int.TryParse(parts[3], out int width)) continue; if (!int.TryParse(parts[4], out int height)) continue; - _ledZone = new Rectangle(x, y, width, height); + Color zoneColor = Color.Lime; if (parts.Length >= 6 && !string.IsNullOrEmpty(parts[5])) { - try { _ledZoneColor = ColorTranslator.FromHtml(parts[5]); } catch { } + try { zoneColor = ColorTranslator.FromHtml(parts[5]); } catch { } } + _ledZones[index] = new Rectangle(x, y, width, height); + _ledZoneColors[index] = zoneColor; } } } diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs index 9f08c87..6456cc9 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs @@ -25,6 +25,7 @@ namespace Camera this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.toolStripButton2 = new System.Windows.Forms.ToolStripButton(); this.toolStripButton3 = new System.Windows.Forms.ToolStripButton(); + this.toolStripButton4 = new System.Windows.Forms.ToolStripButton(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -99,7 +100,8 @@ namespace Camera this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripButton1, this.toolStripButton2, - this.toolStripButton3}); + this.toolStripButton3, + this.toolStripButton4}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; this.toolStrip1.Size = new System.Drawing.Size(80, 40); @@ -133,6 +135,15 @@ namespace Camera this.toolStripButton3.Visible = false; this.toolStripButton3.Click += new System.EventHandler(this.ToolStripButton3_Click); // + // toolStripButton4 + // + this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; + this.toolStripButton4.Name = "toolStripButton4"; + this.toolStripButton4.Size = new System.Drawing.Size(36, 36); + this.toolStripButton4.ToolTipText = "删除Led区"; + this.toolStripButton4.Visible = false; + this.toolStripButton4.Click += new System.EventHandler(this.ToolStripButton4_Click); + // // dataGridView1 // this.dataGridView1.AllowUserToAddRows = false; @@ -187,6 +198,7 @@ namespace Camera private System.Windows.Forms.ToolStripButton toolStripButton1; private System.Windows.Forms.ToolStripButton toolStripButton2; private System.Windows.Forms.ToolStripButton toolStripButton3; + private System.Windows.Forms.ToolStripButton toolStripButton4; private System.Windows.Forms.DataGridView dataGridView1; } } \ No newline at end of file diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 2e0e3ae..1855998 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -18,15 +19,17 @@ namespace Camera private bool _isMoving = false; private bool _isResizing = false; private Point _startPoint; + private Point _currentMousePoint; private Rectangle _originalZone; - private Rectangle _tempZone; private bool _isEditingDetectionZone = false; private bool _isEditingLedZone = false; + private int _ledZoneState = 0; private Color _detectionZoneColor = Color.Black; - private Color _ledZoneColor = Color.Lime; + private ConcurrentDictionary _ledZoneColors = new ConcurrentDictionary(); private Point _resizeStartPoint; private Rectangle _originalResizeRect; private int _hoveredHandle = -1; + private int _currentLedIndex = -1; private int[] _handleOffsetsX = { 0, 1, 2, 2, 2, 1, 0, 0 }; private int[] _handleOffsetsY = { 0, 0, 0, 1, 2, 2, 2, 1 }; @@ -94,7 +97,7 @@ namespace Camera { _camera.ImageCaptured += Camera_ImageCaptured; _detectionZoneColor = _camera.GetDetectionZoneColor(); - _ledZoneColor = _camera.GetLedZoneColor(); + _ledZoneColors = _camera.GetLedZoneColors(); } picBoxCamera.BackColor = Color.Gray; splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2; @@ -182,6 +185,21 @@ namespace Camera } toolStripButton2.Image = ledIcon; toolStripButton2.ImageTransparentColor = Color.Transparent; + + Bitmap deleteIcon = new Bitmap(32, 32); + using (Graphics g = Graphics.FromImage(deleteIcon)) + { + g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; + g.Clear(Color.Transparent); + + using (Pen pen = new Pen(Color.Red, 3)) + { + g.DrawLine(pen, 8, 8, 24, 24); + g.DrawLine(pen, 24, 8, 8, 24); + } + } + toolStripButton4.Image = deleteIcon; + toolStripButton4.ImageTransparentColor = Color.Transparent; } catch { } } @@ -222,17 +240,17 @@ namespace Camera if (_camera != null) { - Rectangle detectionZone = _camera.GetDetectionZone(); - Rectangle ledZone = _camera.GetLedZone(); - - if (detectionZone.Width > 0 && detectionZone.Height > 0) + var ledZones = _camera.GetLedZones(); + foreach (var kvp in ledZones) { - dataGridView1.Rows.Add(0, detectionZone.X, detectionZone.Y, detectionZone.Width, detectionZone.Height, "#FF0000"); - } + int index = kvp.Key; + Rectangle ledZone = kvp.Value; + Color ledColor = _camera.GetLedZoneColor(index); - if (ledZone.Width > 0 && ledZone.Height > 0) - { - dataGridView1.Rows.Add(1, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, "#00FF00"); + if (ledZone.Width > 0 && ledZone.Height > 0) + { + dataGridView1.Rows.Add(index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor)); + } } } @@ -281,7 +299,6 @@ namespace Camera float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight; Rectangle detectionZone = _camera != null ? _camera.GetDetectionZone() : new Rectangle(0, 0, 0, 0); - Rectangle ledZone = _camera != null ? _camera.GetLedZone() : new Rectangle(0, 0, 0, 0); Rectangle scaledDetectionZone = new Rectangle( (int)(detectionZone.X * scaleX), @@ -290,13 +307,6 @@ namespace Camera (int)(detectionZone.Height * scaleY) ); - Rectangle scaledLedZone = new Rectangle( - (int)(ledZone.X * scaleX), - (int)(ledZone.Y * scaleY), - (int)(ledZone.Width * scaleX), - (int)(ledZone.Height * scaleY) - ); - Rectangle currentEditZone = new Rectangle(0, 0, 0, 0); bool isEditing = false; Color editColor = _detectionZoneColor; @@ -307,14 +317,87 @@ namespace Camera isEditing = true; editColor = _detectionZoneColor; } - else if (_isEditingLedZone) + else if (_isEditingLedZone && _currentLedIndex >= 0) { - currentEditZone = scaledLedZone; + Rectangle ledZone = _camera.GetLedZone(_currentLedIndex); + currentEditZone = new Rectangle( + (int)(ledZone.X * scaleX), + (int)(ledZone.Y * scaleY), + (int)(ledZone.Width * scaleX), + (int)(ledZone.Height * scaleY) + ); isEditing = true; - editColor = _ledZoneColor; + editColor = _camera.GetLedZoneColor(_currentLedIndex); } - if (isEditing && currentEditZone.Width > 0 && currentEditZone.Height > 0) + if (_isDrawing && _isEditingLedZone && _startPoint != null && _currentMousePoint != null) + { + using (Pen pen = new Pen(_detectionZoneColor, 2)) + { + pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; + g.DrawRectangle(pen, scaledDetectionZone); + } + + int x = (int)(Math.Min(_startPoint.X, _startPoint.X + (_currentMousePoint.X - _resizeStartPoint.X) / scaleX) * scaleX); + int y = (int)(Math.Min(_startPoint.Y, _startPoint.Y + (_currentMousePoint.Y - _resizeStartPoint.Y) / scaleY) * scaleY); + int w = (int)(Math.Abs(_currentMousePoint.X - _resizeStartPoint.X)); + int h = (int)(Math.Abs(_currentMousePoint.Y - _resizeStartPoint.Y)); + + if (w > 5 && h > 5) + { + Rectangle drawRect = new Rectangle(x, y, w, h); + Color currentLedColor = _currentLedIndex >= 0 && _camera != null ? _camera.GetLedZoneColor(_currentLedIndex) : Color.Lime; + using (Pen pen = new Pen(currentLedColor, 2)) + { + pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; + g.DrawRectangle(pen, drawRect); + } + } + } + else if (_isEditingLedZone && _currentLedIndex >= 0) + { + using (Pen pen = new Pen(_detectionZoneColor, 2)) + { + pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; + g.DrawRectangle(pen, scaledDetectionZone); + } + + var ledZones = _camera.GetLedZones(); + foreach (var kvp in ledZones) + { + int index = kvp.Key; + Rectangle ledZone = kvp.Value; + Color ledColor = _camera.GetLedZoneColor(index); + + Rectangle scaledLed = new Rectangle( + (int)(ledZone.X * scaleX), + (int)(ledZone.Y * scaleY), + (int)(ledZone.Width * scaleX), + (int)(ledZone.Height * scaleY) + ); + + using (Pen pen = new Pen(ledColor, 2)) + { + g.DrawRectangle(pen, scaledLed); + } + + using (Font font = new Font("Arial", 10)) + { + g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15); + } + } + + if (currentEditZone.Width > 0 && currentEditZone.Height > 0) + { + using (Pen pen = new Pen(editColor, 2)) + { + pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; + g.DrawRectangle(pen, currentEditZone); + } + DrawEditHandles(g, currentEditZone, editColor); + } + } + else if (isEditing && currentEditZone.Width > 0 && currentEditZone.Height > 0) { using (Pen pen = new Pen(editColor, 2)) { @@ -331,14 +414,32 @@ namespace Camera g.DrawRectangle(pen, scaledDetectionZone); } - using (Pen pen = new Pen(_ledZoneColor, 2)) + if (_camera != null) { - g.DrawRectangle(pen, scaledLedZone); - } + var ledZones = _camera.GetLedZones(); + foreach (var kvp in ledZones) + { + int index = kvp.Key; + Rectangle ledZone = kvp.Value; + Color ledColor = _camera.GetLedZoneColor(index); - using (Font font = new Font("Arial", 10)) - { - g.DrawString("Led区", font, new SolidBrush(_ledZoneColor), scaledLedZone.X, scaledLedZone.Y - 15); + Rectangle scaledLed = new Rectangle( + (int)(ledZone.X * scaleX), + (int)(ledZone.Y * scaleY), + (int)(ledZone.Width * scaleX), + (int)(ledZone.Height * scaleY) + ); + + using (Pen pen = new Pen(ledColor, 2)) + { + g.DrawRectangle(pen, scaledLed); + } + + using (Font font = new Font("Arial", 10)) + { + g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15); + } + } } } } @@ -416,12 +517,69 @@ namespace Camera (int)(detectionZone.Height * scaleY) ); - Rectangle scaledLedZone = new Rectangle( - (int)(ledZone.X * scaleX), - (int)(ledZone.Y * scaleY), - (int)(ledZone.Width * scaleX), - (int)(ledZone.Height * scaleY) - ); + var ledZones = _camera.GetLedZones(); + bool clickedOnLedZone = false; + int clickedLedIndex = -1; + + foreach (var kvp in ledZones) + { + Rectangle led = kvp.Value; + Rectangle scaledLed = new Rectangle( + (int)(led.X * scaleX), + (int)(led.Y * scaleY), + (int)(led.Width * scaleX), + (int)(led.Height * scaleY) + ); + + if (scaledLed.Width > 0 && scaledLed.Height > 0 && scaledLed.Contains(controlPoint)) + { + if (!_isEditingDetectionZone && !_isEditingLedZone) + { + clickedOnLedZone = true; + clickedLedIndex = kvp.Key; + ledZone = led; + break; + } + else if (_isEditingLedZone) + { + clickedOnLedZone = true; + clickedLedIndex = kvp.Key; + ledZone = led; + break; + } + } + } + + if (clickedOnLedZone) + { + _isEditingLedZone = true; + _currentLedIndex = clickedLedIndex; + _selectedZoneIndex = clickedLedIndex; + _ledZoneState = 1; // 选中状态 + toolStripButton2.Checked = true; + toolStripButton2.Visible = false; + toolStripButton1.Visible = false; + UpdateLedZoneButtonsVisibility(1); + UpdateColorButtonIcon(); + _isMoving = true; + _startPoint = imagePoint; + _resizeStartPoint = controlPoint; + _originalZone = ledZone; + picBoxCamera.Invalidate(); + return; + } + + Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0); + if (_isEditingLedZone && _currentLedIndex >= 0) + { + Rectangle led = _camera.GetLedZone(_currentLedIndex); + scaledLedZone = new Rectangle( + (int)(led.X * scaleX), + (int)(led.Y * scaleY), + (int)(led.Width * scaleX), + (int)(led.Height * scaleY) + ); + } if (_isEditingDetectionZone || _isEditingLedZone) { @@ -434,13 +592,21 @@ namespace Camera _resizeStartPoint = controlPoint; _originalResizeRect = editRect; } - else if (editRect.Contains(controlPoint)) + else if (editRect.Width > 0 && editRect.Height > 0 && editRect.Contains(controlPoint)) { _isMoving = true; _startPoint = imagePoint; _resizeStartPoint = controlPoint; _originalZone = _isEditingDetectionZone ? detectionZone : ledZone; } + else if (_isEditingLedZone) + { + _currentLedIndex = -1; + _selectedZoneIndex = -1; + _ledZoneState = 2; // 切换到绘制状态 + UpdateLedZoneButtonsVisibility(2); + picBoxCamera.Invalidate(); + } } else { @@ -452,14 +618,6 @@ namespace Camera _resizeStartPoint = controlPoint; _originalZone = detectionZone; } - else if (ledZone.Contains(imagePoint)) - { - _selectedZoneIndex = 1; - _isMoving = true; - _startPoint = imagePoint; - _resizeStartPoint = controlPoint; - _originalZone = ledZone; - } else { _selectedZoneIndex = -1; @@ -474,6 +632,7 @@ namespace Camera private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e) { + _currentMousePoint = e.Location; Image currentImage = picBoxCamera.Image; if (currentImage == null || _camera == null) return; @@ -501,12 +660,17 @@ namespace Camera (int)(detectionZone.Height * scaleY) ); - Rectangle scaledLedZone = new Rectangle( - (int)(ledZone.X * scaleX), - (int)(ledZone.Y * scaleY), - (int)(ledZone.Width * scaleX), - (int)(ledZone.Height * scaleY) - ); + Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0); + if (_currentLedIndex >= 0) + { + Rectangle led = _camera.GetLedZone(_currentLedIndex); + scaledLedZone = new Rectangle( + (int)(led.X * scaleX), + (int)(led.Y * scaleY), + (int)(led.Width * scaleX), + (int)(led.Height * scaleY) + ); + } if (_isEditingDetectionZone || _isEditingLedZone) { @@ -540,7 +704,7 @@ namespace Camera } else { - _camera.SetLedZone(newZone); + _camera.SetLedZone(_currentLedIndex, newZone); } picBoxCamera.Update(); } @@ -569,7 +733,7 @@ namespace Camera } else { - _camera.SetLedZone(newZone); + _camera.SetLedZone(_currentLedIndex, newZone); } picBoxCamera.Update(); } @@ -612,9 +776,9 @@ namespace Camera { _camera.SetDetectionZone(newZone); } - else if (_selectedZoneIndex == 1) + else if (_selectedZoneIndex > 0) { - _camera.SetLedZone(newZone); + _camera.SetLedZone(_selectedZoneIndex, newZone); } } @@ -644,10 +808,13 @@ namespace Camera _isEditingLedZone = false; _isMoving = false; _isResizing = false; + _currentLedIndex = -1; + _selectedZoneIndex = -1; + _ledZoneState = 0; // 就绪状态 toolStripButton2.Checked = false; toolStripButton2.ToolTipText = "绘制Led区(点击开启)"; toolStripButton1.Visible = true; - toolStripButton3.Visible = false; + UpdateLedZoneButtonsVisibility(0); picBoxCamera.Invalidate(); return; } @@ -682,8 +849,14 @@ namespace Camera { if (toolStripButton2.Checked) { - _camera.SetLedZone(new Rectangle(x, y, width, height)); - _selectedZoneIndex = 1; + int newIndex = _camera.GetLedZoneCount() + 1; + _camera.AddLedZone(newIndex, new Rectangle(x, y, width, height), Color.Lime); + _selectedZoneIndex = newIndex; + _currentLedIndex = newIndex; + _isEditingLedZone = true; + _ledZoneState = 1; // 选中状态 + UpdateLedZoneButtonsVisibility(1); + UpdateColorButtonIcon(); } else { @@ -817,7 +990,6 @@ namespace Camera toolStripButton3.Visible = true; UpdateColorButtonIcon(); toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)"; - _tempZone = _camera.GetDetectionZone(); } else { @@ -837,22 +1009,25 @@ namespace Camera { _isEditingLedZone = true; _isEditingDetectionZone = false; - _selectedZoneIndex = 1; + _selectedZoneIndex = -1; + _currentLedIndex = -1; + _ledZoneState = 2; // 绘制状态 toolStripButton2.ToolTipText = "绘制Led区(点击关闭)"; toolStripButton1.Checked = false; toolStripButton1.Visible = false; - toolStripButton3.Visible = true; + UpdateLedZoneButtonsVisibility(2); UpdateColorButtonIcon(); toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)"; - _tempZone = _camera.GetLedZone(); } else { _isEditingLedZone = false; + _currentLedIndex = -1; _selectedZoneIndex = -1; + _ledZoneState = 0; // 就绪状态 toolStripButton2.ToolTipText = "绘制Led区(点击开启)"; toolStripButton1.Visible = true; - toolStripButton3.Visible = false; + UpdateLedZoneButtonsVisibility(0); } picBoxCamera.Invalidate(); UpdateDataGridView(); @@ -867,9 +1042,9 @@ namespace Camera { colorDialog.Color = _detectionZoneColor; } - else if (_isEditingLedZone) + else if (_isEditingLedZone && _currentLedIndex >= 0) { - colorDialog.Color = _ledZoneColor; + colorDialog.Color = _camera.GetLedZoneColor(_currentLedIndex); } if (colorDialog.ShowDialog() == DialogResult.OK) @@ -878,9 +1053,9 @@ namespace Camera { _detectionZoneColor = colorDialog.Color; } - else if (_isEditingLedZone) + else if (_isEditingLedZone && _currentLedIndex >= 0) { - _ledZoneColor = colorDialog.Color; + _camera.SetLedZoneColor(_currentLedIndex, colorDialog.Color); } UpdateColorButtonIcon(); picBoxCamera.Invalidate(); @@ -888,9 +1063,40 @@ namespace Camera } } + private void ToolStripButton4_Click(object sender, EventArgs e) + { + if (_isEditingLedZone && _currentLedIndex >= 0) + { + _camera.RemoveLedZone(_currentLedIndex); + _currentLedIndex = -1; + _selectedZoneIndex = -1; + _isEditingLedZone = false; + _ledZoneState = 0; // 就绪状态 + toolStripButton2.Checked = false; + toolStripButton2.Visible = true; + toolStripButton2.ToolTipText = "绘制Led区(点击开启)"; + toolStripButton1.Visible = true; + UpdateLedZoneButtonsVisibility(0); + UpdateDataGridView(); + picBoxCamera.Invalidate(); + } + } + private void UpdateColorButtonIcon() { - Color currentColor = _isEditingDetectionZone ? _detectionZoneColor : _ledZoneColor; + Color currentColor; + if (_isEditingDetectionZone) + { + currentColor = _detectionZoneColor; + } + else if (_isEditingLedZone && _currentLedIndex >= 0) + { + currentColor = _camera.GetLedZoneColor(_currentLedIndex); + } + else + { + currentColor = _camera.GetLedZoneColor(); + } Bitmap bmp = new Bitmap(24, 24); using (Graphics g = Graphics.FromImage(bmp)) { @@ -903,12 +1109,33 @@ namespace Camera toolStripButton3.Image = bmp; } + private void UpdateLedZoneButtonsVisibility(int state) + { + switch (state) + { + case 0: // 就绪状态 + toolStripButton2.Visible = true; + toolStripButton3.Visible = false; + toolStripButton4.Visible = false; + break; + case 1: // 选中状态 + toolStripButton2.Visible = false; + toolStripButton3.Visible = true; + toolStripButton4.Visible = true; + break; + case 2: // 绘制状态 + toolStripButton2.Visible = true; + toolStripButton3.Visible = true; + toolStripButton4.Visible = false; + break; + } + } + private void Setting_FormClosed(object sender, FormClosedEventArgs e) { if (_camera != null) { _camera.SetDetectionZoneColor(_detectionZoneColor); - _camera.SetLedZoneColor(_ledZoneColor); _camera.SaveConfig(); _camera.ImageCaptured -= Camera_ImageCaptured; }