修改检测区功能

This commit is contained in:
zqm
2026-01-08 13:50:50 +08:00
parent 72c79524b1
commit e4354d07bc
3 changed files with 313 additions and 3 deletions

View File

@@ -3295,6 +3295,11 @@ namespace JoyD.Windows.CS.Toprie
JoyD.Windows.CS.Setting.Form.ProjectPath = this.ProjectPath; JoyD.Windows.CS.Setting.Form.ProjectPath = this.ProjectPath;
JoyD.Windows.CS.Setting.Form.CurrentDetectionZone = this.CurrentDetectionZone; JoyD.Windows.CS.Setting.Form.CurrentDetectionZone = this.CurrentDetectionZone;
JoyD.Windows.CS.Setting.Form.ShowDialog(); JoyD.Windows.CS.Setting.Form.ShowDialog();
// 获取修改后的检测区配置
this.CurrentDetectionZone = JoyD.Windows.CS.Setting.Form.CurrentDetectionZone;
// 保存修改后的检测区配置到文件
SaveMenuConfig();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@@ -148,6 +148,16 @@ namespace JoyD.Windows.CS
this.toolStrip.AllowDrop = true; this.toolStrip.AllowDrop = true;
this.toolStrip.Dock = System.Windows.Forms.DockStyle.None; this.toolStrip.Dock = System.Windows.Forms.DockStyle.None;
this.toolStrip.ImageScalingSize = new System.Drawing.Size(20, 20); this.toolStrip.ImageScalingSize = new System.Drawing.Size(20, 20);
// 先实例化btnModifyDetectionZone避免ArgumentNullException
this.btnModifyDetectionZone = new System.Windows.Forms.ToolStripButton();
this.btnModifyDetectionZone.CheckOnClick = true;
this.btnModifyDetectionZone.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.btnModifyDetectionZone.ImageTransparentColor = System.Drawing.Color.Magenta;
this.btnModifyDetectionZone.Name = "btnModifyDetectionZone";
this.btnModifyDetectionZone.Size = new System.Drawing.Size(23, 4);
this.btnModifyDetectionZone.Text = "修改检测区";
this.btnModifyDetectionZone.ToolTipText = "修改检测区(点击开启/关闭)";
this.btnModifyDetectionZone.Click += new System.EventHandler(this.BtnModifyDetectionZone_Click);
this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.btnDrawRegion, this.btnDrawRegion,
this.btnSelectColor, this.btnSelectColor,
@@ -168,7 +178,8 @@ namespace JoyD.Windows.CS
this.btnSaveTempRegion, this.btnSaveTempRegion,
this.btnNewTempDiff, this.btnNewTempDiff,
this.btnLoadTempDiff, this.btnLoadTempDiff,
this.btnSaveTempDiff}); this.btnSaveTempDiff,
this.btnModifyDetectionZone});
this.toolStrip.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow; this.toolStrip.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
this.toolStrip.Location = new System.Drawing.Point(3, 0); this.toolStrip.Location = new System.Drawing.Point(3, 0);
this.toolStrip.MaximumSize = new System.Drawing.Size(0, 200); this.toolStrip.MaximumSize = new System.Drawing.Size(0, 200);
@@ -438,6 +449,7 @@ namespace JoyD.Windows.CS
private System.Windows.Forms.ToolStripButton btnNewTempDiff; private System.Windows.Forms.ToolStripButton btnNewTempDiff;
private System.Windows.Forms.ToolStripButton btnLoadTempDiff; private System.Windows.Forms.ToolStripButton btnLoadTempDiff;
private System.Windows.Forms.ToolStripButton btnSaveTempDiff; private System.Windows.Forms.ToolStripButton btnSaveTempDiff;
private System.Windows.Forms.ToolStripButton btnModifyDetectionZone;
private System.Windows.Forms.DataGridView dataGridViewTempDiff; private System.Windows.Forms.DataGridView dataGridViewTempDiff;
private System.Windows.Forms.ToolStripTextBox txtRegionNumber; private System.Windows.Forms.ToolStripTextBox txtRegionNumber;
} }

View File

@@ -82,6 +82,14 @@ namespace JoyD.Windows.CS
set { _detectionZone = value; } set { _detectionZone = value; }
} }
// 检测区修改相关变量
private bool _isModifyingDetectionZone = false; // 是否处于检测区修改模式
private DetectionZone _originalDetectionZone; // 原始检测区备份
private DetectionZone _tempDetectionZone; // 临时检测区(用于调整过程)
private bool _isAdjustingDetectionZone = false; // 是否正在调整检测区
private ResizeHandle _currentDetectionZoneHandle = ResizeHandle.None; // 当前检测区调整句柄
private Point _detectionZoneStartPoint; // 检测区调整开始点
// 定时器字段 // 定时器字段
private readonly Timer _timer; private readonly Timer _timer;
@@ -960,6 +968,31 @@ namespace JoyD.Windows.CS
private void PicBoxTemp_MouseDown(object sender, MouseEventArgs e) private void PicBoxTemp_MouseDown(object sender, MouseEventArgs e)
{ {
// 检查是否处于检测区修改模式且右击鼠标
if (_isModifyingDetectionZone && e.Button == MouseButtons.Right)
{
// 退出检测区修改模式,保存修改
_isModifyingDetectionZone = false;
btnModifyDetectionZone.Checked = false;
btnModifyDetectionZone.ToolTipText = "修改检测区(点击开启)";
// 保存修改后的检测区配置
_detectionZone.X = _tempDetectionZone.X;
_detectionZone.Y = _tempDetectionZone.Y;
_detectionZone.Width = _tempDetectionZone.Width;
_detectionZone.Height = _tempDetectionZone.Height;
// 恢复叠加层绘制,显示测温区和温差图
CreateRectangleOverlayImage();
// 更新按钮可见性
UpdateButtonsVisibility(0);
btnModifyDetectionZone.Visible = true;
// 刷新绘制
picBoxTemp.Invalidate();
return;
}
// 检查是否处于温差图绘制状态且右击鼠标 // 检查是否处于温差图绘制状态且右击鼠标
if (_isTempDiffDrawingMode && e.Button == MouseButtons.Right) if (_isTempDiffDrawingMode && e.Button == MouseButtons.Right)
{ {
@@ -1023,6 +1056,35 @@ namespace JoyD.Windows.CS
picBoxTemp.Invalidate(); picBoxTemp.Invalidate();
}
// 检测区修改模式下的点击处理
else if (_isModifyingDetectionZone && e.Button == MouseButtons.Left)
{
// 计算检测区在控件中的矩形
Rectangle detectionRect = new Rectangle(
_tempDetectionZone.X,
_tempDetectionZone.Y,
_tempDetectionZone.Width,
_tempDetectionZone.Height
);
Rectangle controlDetectionRect = ImageRectangleToControlRectangle(detectionRect);
// 检查是否点击在句柄上
_currentDetectionZoneHandle = GetHoveredHandle(controlDetectionRect, e.Location);
if (_currentDetectionZoneHandle != ResizeHandle.None)
{
// 开始调整检测区大小
_isAdjustingDetectionZone = true;
_detectionZoneStartPoint = e.Location;
}
else if (controlDetectionRect.Contains(e.Location))
{
// 开始移动检测区
_isAdjustingDetectionZone = true;
_detectionZoneStartPoint = e.Location;
picBoxTemp.Cursor = Cursors.SizeAll;
}
} }
// 处理选中区域的调整大小或移动 // 处理选中区域的调整大小或移动
else if (e.Button == MouseButtons.Left && !_isDrawingMode && _selectedRegionIndex != -1) else if (e.Button == MouseButtons.Left && !_isDrawingMode && _selectedRegionIndex != -1)
@@ -2059,6 +2121,61 @@ namespace JoyD.Windows.CS
return; // 温差图绘制模式下,不执行其他鼠标移动逻辑 return; // 温差图绘制模式下,不执行其他鼠标移动逻辑
} }
// 处理检测区调整
if (_isModifyingDetectionZone && _isAdjustingDetectionZone)
{
// 计算检测区在控件中的矩形
Rectangle detectionRect = new Rectangle(
_tempDetectionZone.X,
_tempDetectionZone.Y,
_tempDetectionZone.Width,
_tempDetectionZone.Height
);
Rectangle controlDetectionRect = ImageRectangleToControlRectangle(detectionRect);
if (_currentDetectionZoneHandle != ResizeHandle.None)
{
// 调整检测区大小
Rectangle newRect = CalculateNewRectangle(controlDetectionRect, _detectionZoneStartPoint, e.Location, _currentDetectionZoneHandle);
// 确保检测区有最小尺寸
if (newRect.Width > 20 && newRect.Height > 20)
{
// 转换为图像坐标并更新检测区
Point imageTopLeft = ControlPointToImagePoint(new Point(newRect.Left, newRect.Top));
Point imageBottomRight = ControlPointToImagePoint(new Point(newRect.Right, newRect.Bottom));
_tempDetectionZone.X = imageTopLeft.X;
_tempDetectionZone.Y = imageTopLeft.Y;
_tempDetectionZone.Width = imageBottomRight.X - imageTopLeft.X;
_tempDetectionZone.Height = imageBottomRight.Y - imageTopLeft.Y;
}
}
else
{
// 移动检测区
int deltaX = e.X - _detectionZoneStartPoint.X;
int deltaY = e.Y - _detectionZoneStartPoint.Y;
// 转换为图像坐标的移动距离
float scaleX = (float)picBoxTemp.Image.Width / picBoxTemp.ClientSize.Width;
float scaleY = (float)picBoxTemp.Image.Height / picBoxTemp.ClientSize.Height;
int imageDeltaX = (int)(deltaX * scaleX);
int imageDeltaY = (int)(deltaY * scaleY);
// 更新检测区位置
_tempDetectionZone.X += imageDeltaX;
_tempDetectionZone.Y += imageDeltaY;
// 更新起始点,以便下一次移动计算
_detectionZoneStartPoint = e.Location;
}
// 刷新绘制
picBoxTemp.Invalidate();
return;
}
// 处理调整大小 // 处理调整大小
if (_isResizing && !_isDrawingMode && _selectedRegionIndex != -1) if (_isResizing && !_isDrawingMode && _selectedRegionIndex != -1)
{ {
@@ -2422,6 +2539,14 @@ namespace JoyD.Windows.CS
return; return;
} }
// 结束检测区调整
if (_isModifyingDetectionZone && _isAdjustingDetectionZone && e.Button == MouseButtons.Left)
{
_isAdjustingDetectionZone = false;
_currentDetectionZoneHandle = ResizeHandle.None;
picBoxTemp.Cursor = Cursors.Default;
return;
}
// 结束移动区域 // 结束移动区域
if (_isMoving && e.Button == MouseButtons.Left) if (_isMoving && e.Button == MouseButtons.Left)
{ {
@@ -2513,6 +2638,35 @@ namespace JoyD.Windows.CS
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 检测区修改模式下的绘制
if (_isModifyingDetectionZone && picBoxTemp.Image != null)
{
// 只绘制检测区,不绘制测温区和温差图
// 计算检测区在控件中的矩形
Rectangle detectionRect = new Rectangle(
_tempDetectionZone.X,
_tempDetectionZone.Y,
_tempDetectionZone.Width,
_tempDetectionZone.Height
);
Rectangle controlDetectionRect = ImageRectangleToControlRectangle(detectionRect);
// 高亮显示检测区
using (Pen pen = new Pen(Color.Red, 3))
{
// 设置绘制质量为无抗锯齿,确保边界清晰
e.Graphics.SmoothingMode = SmoothingMode.None;
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
// 绘制检测区边框
e.Graphics.DrawRectangle(pen, controlDetectionRect);
}
// 绘制8个调整句柄
DrawHandles(e.Graphics, controlDetectionRect);
return; // 跳过后续的正常绘制
}
// 非修改模式下的正常绘制
// 图像合并机制: // 图像合并机制:
// 1. 先绘制温差层(根据控件尺寸进行缩放) // 1. 先绘制温差层(根据控件尺寸进行缩放)
if (_tempDiffOverlayImage != null && picBoxTemp.Image != null) if (_tempDiffOverlayImage != null && picBoxTemp.Image != null)
@@ -2774,10 +2928,69 @@ namespace JoyD.Windows.CS
btnDrawTempDiff.ImageTransparentColor = Color.Transparent; btnDrawTempDiff.ImageTransparentColor = Color.Transparent;
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("温差图按钮图标设置失败: " + ex.Message); Console.WriteLine("温差图按钮图标设置失败: " + ex.Message);
} }
// 设置修改检测区按钮的图标
try
{
Bitmap detectionZoneIcon = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(detectionZoneIcon))
{
// 设置高质量绘图
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 清除背景为透明
g.Clear(Color.Transparent);
// 绘制虚线矩形表示检测区
using (Pen dashedPen = new Pen(Color.Black, 1.5f))
{
dashedPen.DashPattern = new float[] { 2, 1 };
g.DrawRectangle(dashedPen, 6, 6, 20, 20);
}
// 绘制八个角和边的控制点
SolidBrush brush = new SolidBrush(Color.Black);
// 四个角
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.Black, 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);
}
}
}
btnModifyDetectionZone.Image = detectionZoneIcon;
btnModifyDetectionZone.ImageTransparentColor = Color.Transparent;
}
catch (Exception ex)
{
Console.WriteLine("修改检测区按钮图标设置失败: " + ex.Message);
}
// 设置擦除按钮的图标 // 设置擦除按钮的图标
try try
{ {
@@ -4627,7 +4840,87 @@ namespace JoyD.Windows.CS
} }
} }
/// <summary>
/// 修改检测区按钮点击事件
/// </summary>
private void BtnModifyDetectionZone_Click(object sender, EventArgs e)
{
try
{
// 切换检测区修改模式
_isModifyingDetectionZone = !_isModifyingDetectionZone;
btnModifyDetectionZone.Checked = _isModifyingDetectionZone;
if (_isModifyingDetectionZone)
{
// 进入修改模式
btnModifyDetectionZone.ToolTipText = "修改检测区(点击关闭)";
// 备份原始检测区配置
_originalDetectionZone = new DetectionZone
{
X = _detectionZone.X,
Y = _detectionZone.Y,
Width = _detectionZone.Width,
Height = _detectionZone.Height,
Color = _detectionZone.Color
};
// 创建临时检测区用于调整
_tempDetectionZone = new DetectionZone
{
X = _detectionZone.X,
Y = _detectionZone.Y,
Width = _detectionZone.Width,
Height = _detectionZone.Height,
Color = _detectionZone.Color
};
// 以透明色清除叠加层,隐藏测温区和温差图
if (_rectangleOverlayImage != null)
{
using (Graphics g = Graphics.FromImage(_rectangleOverlayImage))
{
g.Clear(Color.Transparent);
}
}
// 隐藏温差图例表格
dataGridViewTempDiff.Visible = false;
// 更新按钮可见性
UpdateButtonsVisibility(0);
btnModifyDetectionZone.Visible = true;
}
else
{
// 退出修改模式,保存修改后的检测区
btnModifyDetectionZone.ToolTipText = "修改检测区(点击开启)";
// 保存修改后的检测区配置
_detectionZone.X = _tempDetectionZone.X;
_detectionZone.Y = _tempDetectionZone.Y;
_detectionZone.Width = _tempDetectionZone.Width;
_detectionZone.Height = _tempDetectionZone.Height;
// 恢复叠加层绘制,显示测温区和温差图
CreateRectangleOverlayImage();
// 更新按钮可见性
UpdateButtonsVisibility(0);
btnModifyDetectionZone.Visible = true;
}
// 刷新绘制
picBoxTemp.Invalidate();
}
catch (Exception ex)
{
Console.WriteLine("修改检测区模式切换失败: " + ex.Message);
_isModifyingDetectionZone = false;
btnModifyDetectionZone.Checked = false;
}
}
private void BtnSaveTempDiff_Click(object sender, EventArgs e) private void BtnSaveTempDiff_Click(object sender, EventArgs e)
{ {