修复btnSelectColor字段未初始化警告,添加颜色选择功能

This commit is contained in:
zqm
2025-11-06 14:50:36 +08:00
parent af1301ffe6
commit 6d2de5e6c6
2 changed files with 49 additions and 10 deletions

View File

@@ -28,13 +28,13 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Setting));
this.splitContainer = new System.Windows.Forms.SplitContainer(); this.splitContainer = new System.Windows.Forms.SplitContainer();
this.groupBoxTempDisplay = new System.Windows.Forms.GroupBox(); this.groupBoxTempDisplay = new System.Windows.Forms.GroupBox();
this.picBoxTemp = new System.Windows.Forms.PictureBox(); this.picBoxTemp = new System.Windows.Forms.PictureBox();
this.toolStripContainer = new System.Windows.Forms.ToolStripContainer(); this.toolStripContainer = new System.Windows.Forms.ToolStripContainer();
this.toolStrip = new System.Windows.Forms.ToolStrip(); this.toolStrip = new System.Windows.Forms.ToolStrip();
this.btnDrawRegion = new System.Windows.Forms.ToolStripButton(); this.btnDrawRegion = new System.Windows.Forms.ToolStripButton();
this.btnSelectColor = new System.Windows.Forms.ToolStripButton();
((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
this.splitContainer.Panel1.SuspendLayout(); this.splitContainer.Panel1.SuspendLayout();
this.splitContainer.Panel2.SuspendLayout(); this.splitContainer.Panel2.SuspendLayout();
@@ -103,10 +103,12 @@
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);
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.toolStrip.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
this.toolStrip.Location = new System.Drawing.Point(0, 0); this.toolStrip.Location = new System.Drawing.Point(0, 0);
this.toolStrip.Name = "toolStrip"; this.toolStrip.Name = "toolStrip";
this.toolStrip.Size = new System.Drawing.Size(75, 27); this.toolStrip.Size = new System.Drawing.Size(24, 7);
this.toolStrip.TabIndex = 1; this.toolStrip.TabIndex = 1;
this.toolStrip.Text = "toolStrip"; this.toolStrip.Text = "toolStrip";
// //
@@ -116,10 +118,18 @@
this.btnDrawRegion.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.btnDrawRegion.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.btnDrawRegion.ImageTransparentColor = System.Drawing.Color.Transparent; this.btnDrawRegion.ImageTransparentColor = System.Drawing.Color.Transparent;
this.btnDrawRegion.Name = "btnDrawRegion"; this.btnDrawRegion.Name = "btnDrawRegion";
this.btnDrawRegion.Size = new System.Drawing.Size(24, 24); this.btnDrawRegion.Size = new System.Drawing.Size(23, 4);
this.btnDrawRegion.Text = "画框"; this.btnDrawRegion.Text = "画框";
this.btnDrawRegion.ToolTipText = "绘制温度检测区域(点击开启/关闭)"; this.btnDrawRegion.ToolTipText = "绘制温度检测区域(点击开启/关闭)";
// //
// btnSelectColor
//
this.btnSelectColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
this.btnSelectColor.Name = "btnSelectColor";
this.btnSelectColor.Size = new System.Drawing.Size(75, 22);
this.btnSelectColor.Text = "选择颜色";
this.btnSelectColor.ToolTipText = "选择绘制区域的颜色";
//
// Setting // Setting
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
@@ -152,5 +162,6 @@
private System.Windows.Forms.ToolStripContainer toolStripContainer; private System.Windows.Forms.ToolStripContainer toolStripContainer;
private System.Windows.Forms.ToolStrip toolStrip; private System.Windows.Forms.ToolStrip toolStrip;
private System.Windows.Forms.ToolStripButton btnDrawRegion; private System.Windows.Forms.ToolStripButton btnDrawRegion;
private System.Windows.Forms.ToolStripButton btnSelectColor;
} }
} }

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@@ -27,6 +27,7 @@ namespace JoyD.Windows.CS
private Rectangle _currentRectangle = Rectangle.Empty; private Rectangle _currentRectangle = Rectangle.Empty;
private bool _isDrawing = false; private bool _isDrawing = false;
private List<Rectangle> _drawnRectangles = new List<Rectangle>(); private List<Rectangle> _drawnRectangles = new List<Rectangle>();
private Color _selectedColor = Color.Red;
public Setting() public Setting()
{ {
@@ -45,6 +46,7 @@ namespace JoyD.Windows.CS
// 注册按钮点击事件 // 注册按钮点击事件
btnDrawRegion.Click += BtnDrawRegion_Click; btnDrawRegion.Click += BtnDrawRegion_Click;
btnSelectColor.Click += BtnSelectColor_Click;
} }
/// <summary> /// <summary>
@@ -145,7 +147,7 @@ namespace JoyD.Windows.CS
// 绘制所有已完成的矩形 // 绘制所有已完成的矩形
foreach (Rectangle rect in _drawnRectangles) foreach (Rectangle rect in _drawnRectangles)
{ {
e.Graphics.DrawRectangle(new Pen(Color.Red, 2), rect); e.Graphics.DrawRectangle(new Pen(_selectedColor, 2), rect);
} }
// 绘制当前正在绘制的矩形 // 绘制当前正在绘制的矩形
@@ -203,9 +205,35 @@ namespace JoyD.Windows.CS
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic, System.Reflection.BindingFlags.NonPublic,
null, picBoxTemp, new object[] { true }); null, picBoxTemp, new object[] { true });
}
// 注册Paint事件
picBoxTemp.Paint += PicBoxTemp_Paint; /// <summary>
/// 颜色选择按钮点击事件
/// </summary>
private void BtnSelectColor_Click(object sender, EventArgs e)
{
using (ColorDialog colorDialog = new ColorDialog())
{
// 设置初始颜色为当前选中的颜色
colorDialog.Color = _selectedColor;
// 允许自定义颜色
colorDialog.AllowFullOpen = true;
colorDialog.FullOpen = true;
// 显示颜色选择对话框
if (colorDialog.ShowDialog() == DialogResult.OK)
{
// 更新选中的颜色
_selectedColor = colorDialog.Color;
// 重绘图片区域,显示新颜色的矩形
picBoxTemp.Invalidate();
// 更新按钮文本,显示当前选中的颜色
btnSelectColor.Text = string.Format("颜色: {0}", _selectedColor.Name);
}
}
} }
/// <summary> /// <summary>
@@ -374,4 +402,4 @@ namespace JoyD.Windows.CS
} }
} }
} }
} }