Files
JoyD/Windows/CS/Framework4.0/Camera/Camera/Setting.cs
2026-03-30 09:40:11 +08:00

1777 lines
69 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Camera
{
// 自定义ToolStripNumericUpDown控件
[System.ComponentModel.ToolboxItem(false)]
[System.ComponentModel.DesignerCategory("Code")]
public class ToolStripNumericUpDown : ToolStripControlHost
{
private NumericUpDown _numericUpDown;
// 公开的无参构造函数 - 设计器必需
public ToolStripNumericUpDown() : this(new NumericUpDown()) { }
// 私有的带参数构造函数 - 仅用于无参构造函数调用
private ToolStripNumericUpDown(NumericUpDown numericUpDown) : base(numericUpDown)
{
_numericUpDown = numericUpDown ?? throw new ArgumentNullException(nameof(numericUpDown));
this.AutoSize = false;
// 设置NumericUpDown的基本属性
_numericUpDown.Size = new Size(60, 22);
_numericUpDown.TextAlign = HorizontalAlignment.Right;
// 转发ValueChanged事件
_numericUpDown.ValueChanged += (s, e) => ValueChanged?.Invoke(s, e);
}
[System.ComponentModel.Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public NumericUpDown NumericUpDownControl => _numericUpDown;
// 下面的属性、事件都保持不变
public decimal Value
{
get => _numericUpDown.Value;
set => _numericUpDown.Value = value;
}
public decimal Minimum
{
get => _numericUpDown.Minimum;
set => _numericUpDown.Minimum = value;
}
public decimal Maximum
{
get => _numericUpDown.Maximum;
set => _numericUpDown.Maximum = value;
}
public decimal Increment
{
get => _numericUpDown.Increment;
set => _numericUpDown.Increment = value;
}
public int DecimalPlaces
{
get => _numericUpDown.DecimalPlaces;
set => _numericUpDown.DecimalPlaces = value;
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new string Text
{
get => _numericUpDown.Text;
set => _numericUpDown.Text = value;
}
public event EventHandler ValueChanged;
protected override void OnSubscribeControlEvents(Control control)
{
base.OnSubscribeControlEvents(control);
if (control is NumericUpDown nud)
nud.ValueChanged += Nud_ValueChanged;
}
protected override void OnUnsubscribeControlEvents(Control control)
{
base.OnUnsubscribeControlEvents(control);
if (control is NumericUpDown nud)
nud.ValueChanged -= Nud_ValueChanged;
}
private void Nud_ValueChanged(object sender, EventArgs e)
{
ValueChanged?.Invoke(this, e);
}
protected override Size DefaultSize => new Size(60, 27);
// 重写GetPreferredSize以改进布局
public override Size GetPreferredSize(Size constrainingSize)
{
return new Size(base.GetPreferredSize(constrainingSize).Width, 27);
}
// 清理资源
protected override void Dispose(bool disposing)
{
if (disposing && _numericUpDown != null)
{
_numericUpDown.Dispose();
}
base.Dispose(disposing);
}
}
public partial class Setting : Form
{
private Camera _camera;
private int _selectedZoneIndex = -1;
private bool _isDrawing = false;
private bool _isMoving = false;
private bool _isResizing = false;
private Point? _startPoint;
private Point? _currentMousePoint;
private Rectangle _originalZone;
private bool _isEditingDetectionZone = false;
private bool _isEditingLedZone = false;
private bool _isDrawingLedMode = false;
private int _ledZoneState = 0;
private Color _detectionZoneColor = Color.Black;
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
private ConcurrentDictionary<int, string> _ledZoneDetectionResults = new ConcurrentDictionary<int, string>();
private Point _resizeStartPoint;
private Rectangle _originalResizeRect;
private int _hoveredHandle = -1;
private int _currentLedIndex = -1;
private int[] _handleOffsetsX = { 0, 1, 2, 2, 1, 0, 0 };
private int[] _handleOffsetsY = { 0, 0, 0, 1, 2, 2, 2, 1 };
private int _lastSelectedZoneIndex = -1;
private bool _isLoadingSettings = false;
private bool _isUpdatingDataGridView = false;
public Setting()
{
InitializeComponent();
InitializeDataGridView();
}
public void SetCamera(Camera camera)
{
_camera = camera;
}
public void SetImage(Image image)
{
try
{
if (picBoxCamera.Image != null)
{
picBoxCamera.Image.Dispose();
}
// 不设置picBoxCamera.Image避免双重绘制
// 只在Paint事件中绘制缩放后的图像
picBoxCamera.Invalidate();
}
catch { }
}
public void SetConfigPath(string path)
{
if (_camera != null)
{
_camera.SetConfigPath(path);
}
}
private void InitializeDataGridView()
{
if (dataGridView1.Columns.Count == 0)
{
dataGridView1.Columns.Add("Index", "索引");
dataGridView1.Columns.Add("X", "X坐标");
dataGridView1.Columns.Add("Y", "Y坐标");
dataGridView1.Columns.Add("Width", "宽度");
dataGridView1.Columns.Add("Height", "高度");
dataGridView1.Columns.Add("Color", "颜色");
dataGridView1.Columns.Add("Detection", "检测结果");
dataGridView1.Columns["Index"].Width = 50;
dataGridView1.Columns["X"].Width = 60;
dataGridView1.Columns["Y"].Width = 60;
dataGridView1.Columns["Width"].Width = 60;
dataGridView1.Columns["Height"].Width = 60;
dataGridView1.Columns["Color"].Width = 70;
dataGridView1.Columns["Detection"].Width = 80;
}
if (dataGridView2.Columns.Count == 0)
{
dataGridView2.Columns.Add("Index", "索引");
dataGridView2.Columns.Add("H", "H值");
dataGridView2.Columns.Add("S", "S值");
dataGridView2.Columns.Add("V", "V值");
dataGridView2.Columns["Index"].Width = 50;
dataGridView2.Columns["H"].Width = 80;
dataGridView2.Columns["S"].Width = 80;
dataGridView2.Columns["V"].Width = 80;
}
}
private void Setting_Load(object sender, EventArgs e)
{
CreateToolIcons();
if (_camera != null)
{
System.Diagnostics.Debug.WriteLine("Setting加载阈值: BrightLimit=" + _camera.GetBrightLimit() + ", SatLimit=" + _camera.GetSatLimit());
_camera.ImageCaptured += Camera_ImageCaptured;
_detectionZoneColor = _camera.GetDetectionZoneColor();
_ledZoneColors = _camera.GetLedZoneColors();
_isLoadingSettings = true;
toolStripNumericUpDown5.Value = _camera.GetBrightLimit();
toolStripNumericUpDown6.Value = _camera.GetSatLimit();
toolStripNumericUpDown7.Value = _camera.GetRedMin();
toolStripNumericUpDown8.Value = _camera.GetRedMax();
toolStripNumericUpDown9.Value = _camera.GetGreenMin();
toolStripNumericUpDown10.Value = _camera.GetGreenMax();
toolStripNumericUpDown11.Value = _camera.GetBlueMin();
toolStripNumericUpDown12.Value = _camera.GetBlueMax();
_isLoadingSettings = false;
}
picBoxCamera.BackColor = Color.Gray;
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
splitContainer1.SplitterDistance = splitContainer1.Width - splitContainer1.Panel2MinSize;
this.BeginInvoke(new Action(() =>
{
if (splitContainer2.Height > 0)
splitContainer2.SplitterDistance = splitContainer2.Height / 2;
}));
UpdateDataGridView();
UpdateLedZoneButtonsVisibility(0);
}
private void CreateToolIcons()
{
try
{
Bitmap detectionIcon = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(detectionIcon))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.Clear(Color.Transparent);
using (Pen dashedPen = new Pen(Color.Red, 1.5f))
{
dashedPen.DashPattern = new float[] { 2, 1 };
g.DrawRectangle(dashedPen, 6, 6, 20, 20);
}
SolidBrush brush = new SolidBrush(Color.Red);
g.FillEllipse(brush, 6, 6, 4, 4);
g.FillEllipse(brush, 22, 6, 4, 4);
g.FillEllipse(brush, 6, 22, 4, 4);
g.FillEllipse(brush, 22, 22, 4, 4);
g.FillEllipse(brush, 14, 6, 4, 4);
g.FillEllipse(brush, 14, 22, 4, 4);
g.FillEllipse(brush, 6, 14, 4, 4);
g.FillEllipse(brush, 22, 14, 4, 4);
using (Pen gearPen = new Pen(Color.Red, 1.5f))
{
g.DrawEllipse(gearPen, 12, 12, 8, 8);
for (int i = 0; i < 6; i++)
{
double angle = i * Math.PI / 3;
int x1 = 16 + (int)(12 * Math.Cos(angle));
int y1 = 16 + (int)(12 * Math.Sin(angle));
int x2 = 16 + (int)(8 * Math.Cos(angle));
int y2 = 16 + (int)(8 * Math.Sin(angle));
g.DrawLine(gearPen, x1, y1, x2, y2);
}
}
}
toolStripButton1.Image = detectionIcon;
toolStripButton1.ImageTransparentColor = Color.Transparent;
Bitmap ledIcon = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(ledIcon))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.Clear(Color.Transparent);
using (Pen dashedPen = new Pen(Color.Lime, 1.5f))
{
dashedPen.DashPattern = new float[] { 2, 1 };
g.DrawRectangle(dashedPen, 6, 6, 20, 20);
}
SolidBrush brush = new SolidBrush(Color.Lime);
g.FillEllipse(brush, 6, 6, 4, 4);
g.FillEllipse(brush, 22, 6, 4, 4);
g.FillEllipse(brush, 6, 22, 4, 4);
g.FillEllipse(brush, 22, 22, 4, 4);
g.FillEllipse(brush, 14, 6, 4, 4);
g.FillEllipse(brush, 14, 22, 4, 4);
g.FillEllipse(brush, 6, 14, 4, 4);
g.FillEllipse(brush, 22, 14, 4, 4);
using (Pen pen = new Pen(Color.Lime, 2))
{
g.DrawLine(pen, 12, 10, 12, 14);
g.DrawLine(pen, 16, 10, 16, 14);
g.DrawLine(pen, 20, 10, 20, 14);
g.DrawLine(pen, 10, 16, 22, 16);
g.DrawLine(pen, 10, 20, 22, 20);
}
}
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 { }
}
private void Camera_ImageCaptured(object sender, ImageEventArgs e)
{
if (picBoxCamera.InvokeRequired)
{
picBoxCamera.Invoke(new Action<Image>(UpdateImage), e.Image);
picBoxCamera.Invoke(new Action(UpdateDetectionResults));
}
else
{
UpdateImage(e.Image);
UpdateDetectionResults();
}
}
private void UpdateImage(Image image)
{
try
{
if (picBoxCamera.Image != null)
{
picBoxCamera.Image.Dispose();
}
// 不设置picBoxCamera.Image避免双重绘制
// 只在Paint事件中绘制缩放后的图像
picBoxCamera.Invalidate();
}
catch { }
}
private void UpdateDataGridView()
{
if (_isUpdatingDataGridView) return;
_isUpdatingDataGridView = true;
dataGridView1.Rows.Clear();
if (_camera != null)
{
Rectangle detectionZone = _camera.GetDetectionZone();
var allLedZones = _camera.GetLedZones();
var hsvValues = _camera.GetLedZoneHsvValues();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (index == _selectedZoneIndex && _selectedZoneIndex >= 0 && !_camera.GetLedZoneVisibility(_selectedZoneIndex)) continue;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
string detectionResult = _camera.GetLedZoneDetectionResult(index);
if (ledZone.Width > 0 && ledZone.Height > 0)
{
dataGridView1.Rows.Add(index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult);
}
}
dataGridView2.Rows.Clear();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (hsvValues.ContainsKey(index))
{
var hsv = hsvValues[index];
dataGridView2.Rows.Add(index, hsv.Item1.ToString("F1"), hsv.Item2.ToString("F1"), hsv.Item3.ToString("F1"));
}
else
{
dataGridView2.Rows.Add(index, "0", "0", "0");
}
}
}
picBoxCamera.Invalidate();
_isUpdatingDataGridView = false;
}
private void UpdateDetectionResults()
{
if (_isUpdatingDataGridView) return;
if (_camera == null || dataGridView1.Rows.Count == 0) return;
try
{
_isUpdatingDataGridView = true;
int selectedIndex = -1;
if (dataGridView1.SelectedRows.Count > 0)
{
selectedIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
}
var allLedZones = _camera.GetLedZones();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
int index = Convert.ToInt32(row.Cells["Index"].Value);
if (allLedZones.ContainsKey(index))
{
string detectionResult = _camera.GetLedZoneDetectionResult(index);
row.Cells["Detection"].Value = detectionResult;
}
}
UpdateHsvResults();
if (selectedIndex >= 0)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToInt32(row.Cells["Index"].Value) == selectedIndex)
{
row.Selected = true;
break;
}
}
}
}
finally
{
_isUpdatingDataGridView = false;
}
}
private void UpdateHsvResults()
{
if (_camera == null || dataGridView2.Rows.Count == 0) return;
var allLedZones = _camera.GetLedZones();
var hsvValues = _camera.GetLedZoneHsvValues();
if (dataGridView2.Rows.Count != allLedZones.Count)
{
dataGridView2.Rows.Clear();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (hsvValues.ContainsKey(index))
{
var hsv = hsvValues[index];
dataGridView2.Rows.Add(index, hsv.Item1.ToString("F1"), hsv.Item2.ToString("F1"), hsv.Item3.ToString("F1"));
}
else
{
dataGridView2.Rows.Add(index, "0", "0", "0");
}
}
}
else
{
foreach (DataGridViewRow row in dataGridView2.Rows)
{
int index = Convert.ToInt32(row.Cells["Index"].Value);
if (hsvValues.ContainsKey(index))
{
var hsv = hsvValues[index];
row.Cells["H"].Value = hsv.Item1.ToString("F1");
row.Cells["S"].Value = hsv.Item2.ToString("F1");
row.Cells["V"].Value = hsv.Item3.ToString("F1");
}
}
}
if (dataGridView1.SelectedRows.Count > 0)
{
int selectedIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (Convert.ToInt32(row.Cells["Index"].Value) == selectedIndex)
{
row.Selected = true;
break;
}
}
}
}
private void PicBoxCamera_Paint(object sender, PaintEventArgs e)
{
Image currentImage = null;
try
{
// 从Camera获取当前图像
currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null) return;
}
catch
{
return;
}
int imageWidth = 0, imageHeight = 0;
bool imageValid = false;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
if (imageWidth > 0 && imageHeight > 0)
{
imageValid = true;
}
}
catch
{
return;
}
if (!imageValid) return;
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// 绘制缩放后的图像
Rectangle destRect = new Rectangle(0, 0, picBoxCamera.ClientSize.Width, picBoxCamera.ClientSize.Height);
g.DrawImage(currentImage, destRect, 0, 0, currentImage.Width, currentImage.Height, GraphicsUnit.Pixel);
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Rectangle detectionZone = _camera != null ? _camera.GetDetectionZone() : new Rectangle(0, 0, 0, 0);
Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
(int)(detectionZone.Y * scaleY),
(int)(detectionZone.Width * scaleX),
(int)(detectionZone.Height * scaleY)
);
Rectangle currentEditZone = new Rectangle(0, 0, 0, 0);
bool isEditing = false;
Color editColor = _detectionZoneColor;
if (_isEditingDetectionZone)
{
currentEditZone = scaledDetectionZone;
isEditing = true;
editColor = _detectionZoneColor;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
currentEditZone = new Rectangle(
(int)((detectionZone.X + ledZone.X) * scaleX),
(int)((detectionZone.Y + ledZone.Y) * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
isEditing = true;
editColor = _camera.GetLedZoneColor(_currentLedIndex);
}
if (_isDrawing && (_isEditingLedZone || _isDrawingLedMode) && _startPoint != null && _currentMousePoint != null)
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
var allLedZones = _camera.GetLedZones();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (index == _selectedZoneIndex && _selectedZoneIndex >= 0 && !_camera.GetLedZoneVisibility(_selectedZoneIndex)) continue;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
Rectangle scaledLed = new Rectangle(
(int)((detectionZone.X + ledZone.X) * scaleX),
(int)((detectionZone.Y + ledZone.Y) * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
using (Pen ledPen = new Pen(ledColor, 2))
{
g.DrawRectangle(ledPen, scaledLed);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15);
}
}
int x = 0, y = 0, w = 0, h = 0;
if (_startPoint.HasValue && _currentMousePoint.HasValue)
{
x = (int)(Math.Min(_startPoint.Value.X, _startPoint.Value.X + (_currentMousePoint.Value.X - _resizeStartPoint.X) / scaleX) * scaleX);
y = (int)(Math.Min(_startPoint.Value.Y, _startPoint.Value.Y + (_currentMousePoint.Value.Y - _resizeStartPoint.Y) / scaleY) * scaleY);
w = (int)(Math.Abs(_currentMousePoint.Value.X - _resizeStartPoint.X));
h = (int)(Math.Abs(_currentMousePoint.Value.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 || _isDrawingLedMode) && _currentLedIndex >= 0)
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
var allLedZones = _camera.GetLedZones();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (index == _selectedZoneIndex && _selectedZoneIndex >= 0 && !_camera.GetLedZoneVisibility(_selectedZoneIndex)) continue;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
Rectangle scaledLed = new Rectangle(
(int)((detectionZone.X + ledZone.X) * scaleX),
(int)((detectionZone.Y + 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))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, currentEditZone);
}
DrawEditHandles(g, currentEditZone, editColor);
}
else
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
if (_camera != null)
{
var allLedZones = _camera.GetLedZones();
foreach (var kvp in allLedZones)
{
int index = kvp.Key;
if (!_camera.GetLedZoneVisibility(index)) continue;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
Rectangle scaledLed = new Rectangle(
(int)((detectionZone.X + ledZone.X) * scaleX),
(int)((detectionZone.Y + 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);
}
}
}
}
}
private void DrawEditHandles(Graphics g, Rectangle rect, Color handleColor)
{
int handleSize = 8;
using (SolidBrush handleBrush = new SolidBrush(Color.White))
using (Pen handleBorderPen = new Pen(Color.Black, 1))
{
int[] xPoints = { rect.X, rect.X + rect.Width / 2, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width / 2, rect.X, rect.X };
int[] yPoints = { rect.Y, rect.Y, rect.Y, rect.Y + rect.Height / 2, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height / 2 };
for (int i = 0; i < 8; i++)
{
Rectangle handleRect = new Rectangle(
xPoints[i] - handleSize / 2,
yPoints[i] - handleSize / 2,
handleSize,
handleSize
);
g.FillRectangle(handleBrush, handleRect);
g.DrawRectangle(handleBorderPen, handleRect);
}
}
}
private void DrawSelectionHandles(Graphics g, Rectangle rect, Color handleColor)
{
int handleSize = 8;
using (Brush brush = new SolidBrush(Color.White))
{
g.FillRectangle(brush, rect.X - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.FillRectangle(brush, rect.X + rect.Width - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.FillRectangle(brush, rect.X - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
g.FillRectangle(brush, rect.X + rect.Width - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
}
using (Pen pen = new Pen(handleColor, 1))
{
g.DrawRectangle(pen, rect.X - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.DrawRectangle(pen, rect.X + rect.Width - handleSize / 2, rect.Y - handleSize / 2, handleSize, handleSize);
g.DrawRectangle(pen, rect.X - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
g.DrawRectangle(pen, rect.X + rect.Width - handleSize / 2, rect.Y + rect.Height - handleSize / 2, handleSize, handleSize);
}
}
private void PicBoxCamera_MouseDown(object sender, MouseEventArgs e)
{
// 处理右键点击
if (e.Button == MouseButtons.Right)
{
return; // 右键点击由MouseUp事件处理
}
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Point imagePoint = new Point((int)(e.Location.X / scaleX), (int)(e.Location.Y / scaleY));
Point controlPoint = e.Location;
Rectangle detectionZone = _camera.GetDetectionZone();
Rectangle ledZone = _camera.GetLedZone();
Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
(int)(detectionZone.Y * scaleY),
(int)(detectionZone.Width * scaleX),
(int)(detectionZone.Height * scaleY)
);
var allLedZones = _camera.GetLedZones();
bool clickedOnLedZone = false;
int clickedLedIndex = -1;
foreach (var kvp in allLedZones)
{
Rectangle led = kvp.Value;
Rectangle scaledLed = new Rectangle(
(int)((detectionZone.X + led.X) * scaleX),
(int)((detectionZone.Y + led.Y) * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
if (scaledLed.Width > 0 && scaledLed.Height > 0 && scaledLed.Contains(controlPoint))
{
if (!_isEditingDetectionZone && !_isEditingLedZone && !_isDrawingLedMode)
{
clickedOnLedZone = true;
clickedLedIndex = kvp.Key;
ledZone = led;
break;
}
else if (_isEditingLedZone || _isDrawingLedMode)
{
clickedOnLedZone = true;
clickedLedIndex = kvp.Key;
ledZone = led;
break;
}
}
}
if (clickedOnLedZone)
{
_isEditingLedZone = true;
_isDrawingLedMode = false;
_currentLedIndex = clickedLedIndex;
_selectedZoneIndex = clickedLedIndex;
toolStripButton1.Visible = false;
UpdateLedZoneButtonsVisibility(1);
UpdateColorButtonIcon();
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = ledZone;
_isDrawing = false;
picBoxCamera.Invalidate();
return;
}
Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0);
if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle(
(int)((detectionZone.X + led.X) * scaleX),
(int)((detectionZone.Y + led.Y) * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
}
if (_isEditingDetectionZone || _isEditingLedZone || _isDrawingLedMode)
{
Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
_hoveredHandle = GetHoveredHandle(editRect, controlPoint);
if (_hoveredHandle >= 0)
{
_isResizing = true;
_resizeStartPoint = controlPoint;
_originalResizeRect = editRect;
}
else if (editRect.Width > 0 && editRect.Height > 0 && editRect.Contains(controlPoint))
{
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = _isEditingDetectionZone ? detectionZone : ledZone;
}
else if ((_isDrawingLedMode || _isEditingLedZone) && !clickedOnLedZone)
{
_currentLedIndex = -1;
_selectedZoneIndex = -1;
if (_isEditingLedZone)
{
_isEditingLedZone = false;
}
_isDrawing = true;
_isDrawingLedMode = true; // 确保处于LED绘制模式
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
UpdateLedZoneButtonsVisibility(2);
}
}
else
{
if (_isEditingDetectionZone && detectionZone.Contains(imagePoint))
{
_selectedZoneIndex = 0;
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = detectionZone;
}
else if (!_isEditingDetectionZone && !_isEditingLedZone && !_isDrawingLedMode)
{
_selectedZoneIndex = -1;
_isDrawing = false;
picBoxCamera.Invalidate();
}
}
UpdateDataGridView();
picBoxCamera.Invalidate();
}
private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e)
{
_currentMousePoint = e.Location;
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Rectangle detectionZone = _camera.GetDetectionZone();
Rectangle ledZone = _camera.GetLedZone();
Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
(int)(detectionZone.Y * scaleY),
(int)(detectionZone.Width * scaleX),
(int)(detectionZone.Height * scaleY)
);
Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0);
if (_currentLedIndex >= 0)
{
Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle(
(int)((detectionZone.X + led.X) * scaleX),
(int)((detectionZone.Y + led.Y) * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
}
if (_isEditingDetectionZone || _isEditingLedZone || _isDrawingLedMode)
{
Rectangle editRect = _isEditingDetectionZone ? scaledDetectionZone : scaledLedZone;
int handle = GetHoveredHandle(editRect, e.Location);
if (_isResizing)
{
picBoxCamera.Cursor = GetResizeCursor(_hoveredHandle);
int dx = e.Location.X - _resizeStartPoint.X;
int dy = e.Location.Y - _resizeStartPoint.Y;
Rectangle newRect = AdjustRectangle(_originalResizeRect, _hoveredHandle, dx, dy);
Rectangle newZone = new Rectangle(
(int)(newRect.X / scaleX),
(int)(newRect.Y / scaleY),
(int)(newRect.Width / scaleX),
(int)(newRect.Height / scaleY)
);
if (newZone.Width > 10 && newZone.Height > 10 &&
newZone.X >= 0 && newZone.Y >= 0 &&
newZone.X + newZone.Width <= currentImage.Width &&
newZone.Y + newZone.Height <= currentImage.Height)
{
if (_isEditingDetectionZone)
{
_camera.SetDetectionZone(newZone);
}
else if (_isEditingLedZone)
{
_camera.SetLedZone(_currentLedIndex, newZone);
}
picBoxCamera.Update();
}
}
else if (_isMoving)
{
picBoxCamera.Cursor = Cursors.SizeAll;
int dx = e.Location.X - _resizeStartPoint.X;
int dy = e.Location.Y - _resizeStartPoint.Y;
Rectangle newZone = new Rectangle(
_originalZone.X + (int)(dx / scaleX),
_originalZone.Y + (int)(dy / scaleY),
_originalZone.Width,
_originalZone.Height
);
if (newZone.X >= 0 && newZone.Y >= 0 &&
newZone.X + newZone.Width <= currentImage.Width &&
newZone.Y + newZone.Height <= currentImage.Height)
{
if (_isEditingDetectionZone)
{
_camera.SetDetectionZone(newZone);
}
else if (_isEditingLedZone)
{
_camera.SetLedZone(_currentLedIndex, newZone);
}
picBoxCamera.Update();
}
}
else
{
if (handle >= 0)
{
picBoxCamera.Cursor = GetResizeCursor(handle);
}
else if (editRect.Contains(e.Location))
{
picBoxCamera.Cursor = Cursors.SizeAll;
}
else
{
picBoxCamera.Cursor = Cursors.Default;
}
}
}
else if (_isMoving)
{
picBoxCamera.Cursor = Cursors.SizeAll;
int dx = e.Location.X - _resizeStartPoint.X;
int dy = e.Location.Y - _resizeStartPoint.Y;
Rectangle newZone = new Rectangle(
_originalZone.X + (int)(dx / scaleX),
_originalZone.Y + (int)(dy / scaleY),
_originalZone.Width,
_originalZone.Height
);
if (newZone.X >= 0 && newZone.Y >= 0 &&
newZone.X + newZone.Width <= currentImage.Width &&
newZone.Y + newZone.Height <= currentImage.Height)
{
if (_selectedZoneIndex == 0)
{
_camera.SetDetectionZone(newZone);
}
else if (_selectedZoneIndex > 0)
{
_camera.SetLedZone(_selectedZoneIndex, newZone);
}
}
UpdateDataGridView();
picBoxCamera.Update();
}
}
private void PicBoxCamera_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (_isEditingDetectionZone)
{
_isEditingDetectionZone = false;
_isMoving = false;
_isResizing = false;
toolStripButton1.Checked = false;
toolStripButton1.ToolTipText = "修改检测区(点击开启)";
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
picBoxCamera.Invalidate();
return;
}
if (_isDrawingLedMode || _isEditingLedZone || _isDrawing)
{
_isDrawingLedMode = false;
_isEditingLedZone = false;
_isMoving = false;
_isResizing = false;
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_isDrawing = false;
toolStripButton2.Checked = false;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
UpdateLedZoneButtonsVisibility(0);
picBoxCamera.Invalidate();
return;
}
}
Image currentImage = _camera != null ? _camera.GetCurrentImage() : null;
if (currentImage == null || _camera == null) return;
int imageWidth, imageHeight;
try
{
imageWidth = currentImage.Width;
imageHeight = currentImage.Height;
}
catch
{
return;
}
float scaleX = (float)picBoxCamera.ClientSize.Width / imageWidth;
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Point imagePoint = new Point((int)(e.Location.X / scaleX), (int)(e.Location.Y / scaleY));
if (_isDrawing)
{
int x = 0, y = 0, width = 0, height = 0;
if (_startPoint.HasValue)
{
x = Math.Min(_startPoint.Value.X, imagePoint.X);
y = Math.Min(_startPoint.Value.Y, imagePoint.Y);
width = Math.Abs(imagePoint.X - _startPoint.Value.X);
height = Math.Abs(imagePoint.Y - _startPoint.Value.Y);
}
if (width > 5 && height > 5)
{
if (_isDrawingLedMode)
{
int newIndex = _camera.GetLedZoneCount() + 1;
while (_camera.GetLedZones().ContainsKey(newIndex))
{
newIndex++;
}
Rectangle detectionZone = _camera.GetDetectionZone();
int relativeX = x - detectionZone.X;
int relativeY = y - detectionZone.Y;
_camera.AddLedZone(newIndex, new Rectangle(relativeX, relativeY, width, height), Color.Lime);
_selectedZoneIndex = newIndex;
_currentLedIndex = newIndex;
_isEditingLedZone = true;
_isDrawingLedMode = true;
UpdateLedZoneButtonsVisibility(1);
UpdateColorButtonIcon();
}
else
{
_camera.SetDetectionZone(new Rectangle(x, y, width, height));
_selectedZoneIndex = 0;
}
}
}
_isDrawing = false;
_isMoving = false;
_isResizing = false;
_hoveredHandle = -1;
UpdateDataGridView();
picBoxCamera.Invalidate();
}
private int GetHoveredHandle(Rectangle rect, Point point)
{
int handleSize = 8;
int[] xPoints = { rect.X, rect.X + rect.Width / 2, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width, rect.X + rect.Width / 2, rect.X, rect.X };
int[] yPoints = { rect.Y, rect.Y, rect.Y, rect.Y + rect.Height / 2, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height, rect.Y + rect.Height / 2 };
for (int i = 0; i < 8; i++)
{
Rectangle handleRect = new Rectangle(
xPoints[i] - handleSize / 2,
yPoints[i] - handleSize / 2,
handleSize,
handleSize
);
if (handleRect.Contains(point))
{
return i;
}
}
return -1;
}
private Rectangle AdjustRectangle(Rectangle rect, int handle, int dx, int dy)
{
int x = rect.X;
int y = rect.Y;
int width = rect.Width;
int height = rect.Height;
switch (handle)
{
case 0:
x += dx;
y += dy;
width -= dx;
height -= dy;
break;
case 1:
y += dy;
height -= dy;
break;
case 2:
y += dy;
width += dx;
height -= dy;
break;
case 3:
width += dx;
break;
case 4:
width += dx;
height += dy;
break;
case 5:
height += dy;
break;
case 6:
x += dx;
width -= dx;
height += dy;
break;
case 7:
x += dx;
width -= dx;
break;
}
if (width < 10) width = 10;
if (height < 10) height = 10;
return new Rectangle(x, y, width, height);
}
private Cursor GetResizeCursor(int handle)
{
switch (handle)
{
case 0:
case 4:
return Cursors.SizeNWSE;
case 2:
case 6:
return Cursors.SizeNESW;
case 1:
case 5:
return Cursors.SizeNS;
case 3:
case 7:
return Cursors.SizeWE;
default:
return Cursors.Default;
}
}
private void DataGridView1_SelectionChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("DataGridView1_SelectionChanged 被触发, SelectedRows.Count=" + dataGridView1.SelectedRows.Count);
if (dataGridView1.SelectedRows.Count > 0)
{
int newSelectedZoneIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
System.Diagnostics.Debug.WriteLine("列表选中索引: " + newSelectedZoneIndex);
if (newSelectedZoneIndex != _lastSelectedZoneIndex)
{
_lastSelectedZoneIndex = newSelectedZoneIndex;
_selectedZoneIndex = newSelectedZoneIndex;
if (_selectedZoneIndex >= 0 && _camera != null)
{
Rectangle zone = _camera.GetLedZone(_selectedZoneIndex);
System.Diagnostics.Debug.WriteLine("GetLedZone(" + _selectedZoneIndex + ") 返回: X=" + zone.X + ", Y=" + zone.Y + ", W=" + zone.Width + ", H=" + zone.Height);
toolStripNumericUpDown1.Value = zone.X;
toolStripNumericUpDown2.Value = zone.Y;
toolStripNumericUpDown3.Value = zone.Width;
toolStripNumericUpDown4.Value = zone.Height;
toolStripButton5.Checked = _camera.GetLedZoneVisibility(_selectedZoneIndex);
}
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (Convert.ToInt32(row.Cells["Index"].Value) == newSelectedZoneIndex)
{
row.Selected = true;
break;
}
}
picBoxCamera.Invalidate();
}
}
}
private void ToolStripButton1_Click(object sender, EventArgs e)
{
if (toolStripButton1.Checked)
{
_isEditingDetectionZone = true;
_isEditingLedZone = false;
_selectedZoneIndex = 0;
toolStripButton1.ToolTipText = "修改检测区(点击关闭)";
toolStripButton3.Visible = true;
UpdateColorButtonIcon();
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
}
else
{
_isEditingDetectionZone = false;
_selectedZoneIndex = -1;
toolStripButton1.ToolTipText = "修改检测区(点击开启)";
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
}
picBoxCamera.Invalidate();
UpdateDataGridView();
}
private void ToolStripButton2_Click(object sender, EventArgs e)
{
if (toolStripButton2.Checked)
{
_isDrawingLedMode = true;
_isEditingDetectionZone = false;
_isMoving = false;
_isResizing = false;
_isDrawing = false;
_selectedZoneIndex = -1;
_currentLedIndex = -1;
toolStripButton2.ToolTipText = "绘制Led区(点击关闭)";
toolStripButton1.Checked = false;
toolStripButton1.Visible = false;
UpdateLedZoneButtonsVisibility(2);
UpdateColorButtonIcon();
toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)";
}
else
{
_isDrawingLedMode = false;
_isEditingLedZone = false;
_currentLedIndex = -1;
_selectedZoneIndex = -1;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
UpdateLedZoneButtonsVisibility(0);
_isDrawing = false;
}
picBoxCamera.Invalidate();
UpdateDataGridView();
}
private void ToolStripButton3_Click(object sender, EventArgs e)
{
using (ColorDialog colorDialog = new ColorDialog())
{
colorDialog.FullOpen = true;
if (_isEditingDetectionZone)
{
colorDialog.Color = _detectionZoneColor;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
colorDialog.Color = _camera.GetLedZoneColor(_currentLedIndex);
}
if (colorDialog.ShowDialog() == DialogResult.OK)
{
if (_isEditingDetectionZone)
{
_detectionZoneColor = colorDialog.Color;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
_camera.SetLedZoneColor(_currentLedIndex, colorDialog.Color);
}
UpdateColorButtonIcon();
picBoxCamera.Invalidate();
}
}
}
private void ToolStripButton4_Click(object sender, EventArgs e)
{
if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
_camera.RemoveLedZone(_currentLedIndex);
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_isEditingLedZone = false;
_isDrawingLedMode = false;
toolStripButton2.Checked = false;
toolStripButton2.Visible = true;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
toolStripTextBox1.Visible = false;
toolStripTextBox1.Text = "";
UpdateLedZoneButtonsVisibility(0);
UpdateDataGridView();
picBoxCamera.Invalidate();
}
}
private void ToolStripTextBox1_TextChanged(object sender, EventArgs e)
{
if (_currentLedIndex >= 0 && int.TryParse(toolStripTextBox1.Text, out int newIndex))
{
if (newIndex > 0)
{
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
Color ledColor = _camera.GetLedZoneColor(_currentLedIndex);
_camera.RemoveLedZone(_currentLedIndex);
_camera.AddLedZone(newIndex, ledZone, ledColor);
_currentLedIndex = newIndex;
_selectedZoneIndex = newIndex;
UpdateDataGridView();
picBoxCamera.Invalidate();
}
}
}
private void ToolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (_currentLedIndex >= 0 && int.TryParse(toolStripTextBox1.Text, out int newIndex))
{
if (newIndex > 0)
{
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
Color ledColor = _camera.GetLedZoneColor(_currentLedIndex);
_camera.RemoveLedZone(_currentLedIndex);
_camera.AddLedZone(newIndex, ledZone, ledColor);
_currentLedIndex = newIndex;
_selectedZoneIndex = newIndex;
toolStripTextBox1.Text = newIndex.ToString();
UpdateDataGridView();
picBoxCamera.Invalidate();
}
}
toolStripTextBox1.SelectAll();
e.SuppressKeyPress = true;
}
}
private void PicBoxCamera_SizeChanged(object sender, EventArgs e)
{
picBoxCamera.Invalidate();
}
private void UpdateColorButtonIcon()
{
Color currentColor;
if (_isEditingDetectionZone)
{
currentColor = _detectionZoneColor;
}
else if ((_isEditingLedZone || _isDrawingLedMode) && _currentLedIndex >= 0)
{
currentColor = _camera.GetLedZoneColor(_currentLedIndex);
}
else
{
currentColor = _camera.GetLedZoneColor();
}
Bitmap bmp = new Bitmap(24, 24);
using (Graphics g = Graphics.FromImage(bmp))
{
using (Brush brush = new SolidBrush(currentColor))
{
g.FillRectangle(Brushes.White, 0, 0, 24, 24);
g.FillRectangle(brush, 2, 2, 20, 20);
}
}
toolStripButton3.Image = bmp;
}
private void UpdateLedZoneButtonsVisibility(int state)
{
_ledZoneState = state;
switch (state)
{
case 0: // 就绪状态
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
toolStripButton4.Visible = false;
toolStripTextBox1.Visible = false;
toolStripTextBox1.Text = "";
toolStrip3.Visible = true;
toolStrip4.Visible = true;
// 如果有LED区域则显示位置和宽高设置框
if (_camera != null && _camera.GetLedZoneCount() > 0)
{
toolStrip2.Visible = true;
int targetIndex = -1;
if (dataGridView1.SelectedRows.Count > 0)
{
targetIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
}
else if (_selectedZoneIndex >= 0)
{
targetIndex = _selectedZoneIndex;
}
if (targetIndex >= 0)
{
Rectangle zone = _camera.GetLedZone(targetIndex);
toolStripNumericUpDown1.Value = zone.X;
toolStripNumericUpDown2.Value = zone.Y;
toolStripNumericUpDown3.Value = zone.Width;
toolStripNumericUpDown4.Value = zone.Height;
}
}
else
{
toolStrip2.Visible = false;
}
break;
case 1: // 选中状态
toolStripButton2.Visible = false;
toolStripButton3.Visible = true;
toolStripButton4.Visible = true;
toolStripTextBox1.Visible = true;
toolStripTextBox1.Text = _currentLedIndex.ToString();
// 不显示位置和宽高设置框
toolStrip2.Visible = false;
toolStrip3.Visible = false;
toolStrip4.Visible = false;
break;
case 2: // 绘制状态
toolStripButton2.Visible = true;
toolStripButton3.Visible = true;
toolStripButton4.Visible = false;
toolStripTextBox1.Visible = false;
toolStripTextBox1.Text = "";
toolStrip2.Visible = false;
toolStrip3.Visible = false;
toolStrip4.Visible = false;
break;
}
}
private void Setting_FormClosed(object sender, FormClosedEventArgs e)
{
if (_camera != null)
{
_camera.SetDetectionZoneColor(_detectionZoneColor);
_camera.SaveConfig();
_camera.ImageCaptured -= Camera_ImageCaptured;
}
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
// X坐标相关事件
private void ToolStripNumericUpDown1_ValueChanged(object sender, EventArgs e)
{
int index = -1;
if (dataGridView1.SelectedRows.Count > 0)
{
index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
}
else if (_currentLedIndex >= 0)
{
index = _currentLedIndex;
}
else if (_selectedZoneIndex >= 0)
{
index = _selectedZoneIndex;
}
if (index < 0 || _camera == null) return;
int x = (int)toolStripNumericUpDown1.Value;
Rectangle zone = _camera.GetLedZone(index);
zone.X = x;
_camera.SetLedZone(index, zone);
UpdateDataGridViewRow(index, zone);
picBoxCamera.Invalidate();
}
// Y坐标相关事件
private void ToolStripNumericUpDown2_ValueChanged(object sender, EventArgs e)
{
int index = -1;
if (dataGridView1.SelectedRows.Count > 0)
{
index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
}
else if (_currentLedIndex >= 0)
{
index = _currentLedIndex;
}
else if (_selectedZoneIndex >= 0)
{
index = _selectedZoneIndex;
}
if (index < 0 || _camera == null) return;
int y = (int)toolStripNumericUpDown2.Value;
Rectangle zone = _camera.GetLedZone(index);
zone.Y = y;
_camera.SetLedZone(index, zone);
UpdateDataGridViewRow(index, zone);
picBoxCamera.Invalidate();
}
// 宽度相关事件
private void ToolStripNumericUpDown3_ValueChanged(object sender, EventArgs e)
{
int index = -1;
if (dataGridView1.SelectedRows.Count > 0)
{
index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
}
else if (_currentLedIndex >= 0)
{
index = _currentLedIndex;
}
else if (_selectedZoneIndex >= 0)
{
index = _selectedZoneIndex;
}
if (index < 0 || _camera == null) return;
int width = (int)toolStripNumericUpDown3.Value;
Rectangle zone = _camera.GetLedZone(index);
zone.Width = width;
_camera.SetLedZone(index, zone);
UpdateDataGridViewRow(index, zone);
picBoxCamera.Invalidate();
}
private void UpdateDataGridViewRow(int index, Rectangle zone)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (Convert.ToInt32(row.Cells["Index"].Value) == index)
{
row.Cells["X"].Value = zone.X;
row.Cells["Y"].Value = zone.Y;
row.Cells["Width"].Value = zone.Width;
row.Cells["Height"].Value = zone.Height;
break;
}
}
}
// 高度相关事件
private void ToolStripNumericUpDown4_ValueChanged(object sender, EventArgs e)
{
int index = -1;
if (dataGridView1.SelectedRows.Count > 0)
{
index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
}
else if (_currentLedIndex >= 0)
{
index = _currentLedIndex;
}
else if (_selectedZoneIndex >= 0)
{
index = _selectedZoneIndex;
}
if (index < 0 || _camera == null) return;
int height = (int)toolStripNumericUpDown4.Value;
Rectangle zone = _camera.GetLedZone(index);
zone.Height = height;
_camera.SetLedZone(index, zone);
UpdateDataGridViewRow(index, zone);
picBoxCamera.Invalidate();
}
// 亮度阈值事件
private void ToolStripNumericUpDown5_ValueChanged(object sender, EventArgs e)
{
if (_camera == null || _isLoadingSettings) return;
int brightLimit = (int)toolStripNumericUpDown5.Value;
int satLimit = (int)toolStripNumericUpDown6.Value;
_camera.SetLedDetectorParams(brightLimit, satLimit);
}
// 饱和度阈值事件
private void ToolStripNumericUpDown6_ValueChanged(object sender, EventArgs e)
{
if (_camera == null || _isLoadingSettings) return;
int brightLimit = (int)toolStripNumericUpDown5.Value;
int satLimit = (int)toolStripNumericUpDown6.Value;
_camera.SetLedDetectorParams(brightLimit, satLimit);
}
// 显示Led框复选框事件
private void ToolStripButton5_Click(object sender, EventArgs e)
{
if (_camera != null && _selectedZoneIndex >= 0)
{
_camera.SetLedZoneVisibility(_selectedZoneIndex, toolStripButton5.Checked);
}
picBoxCamera.Refresh();
}
// 红绿蓝阈值事件
private void ToolStripNumericUpDown7_ValueChanged(object sender, EventArgs e)
{
if (_camera == null) return;
UpdateColorThresholds();
}
private void ToolStripNumericUpDown8_ValueChanged(object sender, EventArgs e)
{
if (_camera == null) return;
UpdateColorThresholds();
}
private void ToolStripNumericUpDown9_ValueChanged(object sender, EventArgs e)
{
if (_camera == null) return;
UpdateColorThresholds();
}
private void ToolStripNumericUpDown10_ValueChanged(object sender, EventArgs e)
{
if (_camera == null) return;
UpdateColorThresholds();
}
private void ToolStripNumericUpDown11_ValueChanged(object sender, EventArgs e)
{
if (_camera == null) return;
UpdateColorThresholds();
}
private void ToolStripNumericUpDown12_ValueChanged(object sender, EventArgs e)
{
if (_camera == null) return;
UpdateColorThresholds();
}
private void UpdateColorThresholds()
{
if (_camera == null || _isLoadingSettings) return;
int redMin = (int)toolStripNumericUpDown7.Value;
int redMax = (int)toolStripNumericUpDown8.Value;
int greenMin = (int)toolStripNumericUpDown9.Value;
int greenMax = (int)toolStripNumericUpDown10.Value;
int blueMin = (int)toolStripNumericUpDown11.Value;
int blueMax = (int)toolStripNumericUpDown12.Value;
_camera.SetColorThresholds(redMin, redMax, greenMin, greenMax, blueMin, blueMax);
}
}
}