将温差图例表中颜色列的按钮改为显示颜色块

This commit is contained in:
zqm
2025-11-10 09:55:20 +08:00
parent 88fd50290c
commit af75b9e8aa

View File

@@ -85,6 +85,9 @@ namespace JoyD.Windows.CS
/// <summary> /// <summary>
/// 鼠标按下事件 - 处理右击退出绘制状态、左击开始绘制矩形和开始调整区域大小 /// 鼠标按下事件 - 处理右击退出绘制状态、左击开始绘制矩形和开始调整区域大小
/// </summary> /// </summary>
// 存储温差值和对应颜色的数据结构
private List<Dictionary<string, object>> tempDiffData = new List<Dictionary<string, object>>();
private void InitializeTempDiffDataGridView() private void InitializeTempDiffDataGridView()
{ {
// 设置整个DataGridView为只读 // 设置整个DataGridView为只读
@@ -104,13 +107,13 @@ namespace JoyD.Windows.CS
}; };
// 添加列:颜色 // 添加列:颜色
DataGridViewButtonColumn columnColor = new DataGridViewButtonColumn DataGridViewTextBoxColumn columnColor = new DataGridViewTextBoxColumn
{ {
Name = "colorColumn", Name = "colorColumn",
HeaderText = "颜色", HeaderText = "颜色",
Width = 80, Width = 80,
Text = "选择", ReadOnly = true,
UseColumnTextForButtonValue = true CellTemplate = new DataGridViewTextBoxCell()
}; };
// 添加列到DataGridView // 添加列到DataGridView
@@ -118,8 +121,10 @@ namespace JoyD.Windows.CS
dataGridViewTempDiff.Columns.Add(columnTempDiff); dataGridViewTempDiff.Columns.Add(columnTempDiff);
dataGridViewTempDiff.Columns.Add(columnColor); dataGridViewTempDiff.Columns.Add(columnColor);
// 添加按钮点击事件 // 添加单元格绘制事件用于显示颜色块
dataGridViewTempDiff.CellContentClick += new DataGridViewCellEventHandler(DataGridViewTempDiff_CellContentClick); dataGridViewTempDiff.CellPainting += new DataGridViewCellPaintingEventHandler(DataGridViewTempDiff_CellPainting);
// 添加点击事件用于选择颜色
dataGridViewTempDiff.CellClick += new DataGridViewCellEventHandler(DataGridViewTempDiff_CellClick);
// 添加一些示例数据 // 添加一些示例数据
AddSampleTempDiffData(); AddSampleTempDiffData();
@@ -127,25 +132,88 @@ namespace JoyD.Windows.CS
private void AddSampleTempDiffData() private void AddSampleTempDiffData()
{ {
// 添加示例行 // 清除现有数据
dataGridViewTempDiff.Rows.Add("10°C", "选择"); tempDiffData.Clear();
dataGridViewTempDiff.Rows.Add("20°C", "选择"); dataGridViewTempDiff.Rows.Clear();
dataGridViewTempDiff.Rows.Add("30°C", "选择");
// 添加示例数据并设置默认颜色
AddTempDiffRow("10°C", Color.Blue);
AddTempDiffRow("20°C", Color.Green);
AddTempDiffRow("30°C", Color.Red);
} }
private void DataGridViewTempDiff_CellContentClick(object sender, DataGridViewCellEventArgs e) private void AddTempDiffRow(string tempDiffValue, Color color)
{ {
if (e.RowIndex >= 0) // 添加到数据列表
Dictionary<string, object> rowData = new Dictionary<string, object>
{ {
// 处理颜色选择按钮点击 { "tempDiffValue", tempDiffValue },
{ "color", color }
};
tempDiffData.Add(rowData);
// 添加到DataGridView
dataGridViewTempDiff.Rows.Add(tempDiffValue, "");
}
private void DataGridViewTempDiff_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// 只处理颜色列
if (e.ColumnIndex == dataGridViewTempDiff.Columns["colorColumn"].Index && e.RowIndex >= 0)
{
// 清空单元格内容
e.PaintBackground(e.CellBounds, true);
// 确保行索引有效
if (e.RowIndex < tempDiffData.Count)
{
// 获取当前行的颜色
Color color = (Color)tempDiffData[e.RowIndex]["color"];
// 创建颜色块的绘制区域(居中显示,留出边距)
Rectangle colorBlock = new Rectangle(
e.CellBounds.X + 10,
e.CellBounds.Y + 3,
e.CellBounds.Width - 20,
e.CellBounds.Height - 6
);
// 绘制颜色块
using (SolidBrush brush = new SolidBrush(color))
{
e.Graphics.FillRectangle(brush, colorBlock);
}
// 添加边框
using (Pen pen = new Pen(Color.Black))
{
e.Graphics.DrawRectangle(pen, colorBlock);
}
}
// 标记单元格已绘制
e.Handled = true;
}
}
private void DataGridViewTempDiff_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.RowIndex < tempDiffData.Count)
{
// 处理颜色列点击
if (e.ColumnIndex == dataGridViewTempDiff.Columns["colorColumn"].Index) if (e.ColumnIndex == dataGridViewTempDiff.Columns["colorColumn"].Index)
{ {
using (ColorDialog colorDialog = new ColorDialog()) using (ColorDialog colorDialog = new ColorDialog())
{ {
// 设置初始颜色为当前行的颜色
colorDialog.Color = (Color)tempDiffData[e.RowIndex]["color"];
if (colorDialog.ShowDialog() == DialogResult.OK) if (colorDialog.ShowDialog() == DialogResult.OK)
{ {
// 这里可以保存选择的颜色 // 更新颜色数据
Console.WriteLine("选择的颜色: " + colorDialog.Color.Name); tempDiffData[e.RowIndex]["color"] = colorDialog.Color;
// 刷新单元格以显示新颜色
dataGridViewTempDiff.Refresh();
} }
} }
} }