diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs index 3a4db55..0d1e763 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs @@ -85,6 +85,9 @@ namespace JoyD.Windows.CS /// /// 鼠标按下事件 - 处理右击退出绘制状态、左击开始绘制矩形和开始调整区域大小 /// + // 存储温差值和对应颜色的数据结构 + private List> tempDiffData = new List>(); + private void InitializeTempDiffDataGridView() { // 设置整个DataGridView为只读 @@ -104,13 +107,13 @@ namespace JoyD.Windows.CS }; // 添加列:颜色 - DataGridViewButtonColumn columnColor = new DataGridViewButtonColumn + DataGridViewTextBoxColumn columnColor = new DataGridViewTextBoxColumn { Name = "colorColumn", HeaderText = "颜色", Width = 80, - Text = "选择", - UseColumnTextForButtonValue = true + ReadOnly = true, + CellTemplate = new DataGridViewTextBoxCell() }; // 添加列到DataGridView @@ -118,8 +121,10 @@ namespace JoyD.Windows.CS dataGridViewTempDiff.Columns.Add(columnTempDiff); dataGridViewTempDiff.Columns.Add(columnColor); - // 添加按钮点击事件 - dataGridViewTempDiff.CellContentClick += new DataGridViewCellEventHandler(DataGridViewTempDiff_CellContentClick); + // 添加单元格绘制事件用于显示颜色块 + dataGridViewTempDiff.CellPainting += new DataGridViewCellPaintingEventHandler(DataGridViewTempDiff_CellPainting); + // 添加点击事件用于选择颜色 + dataGridViewTempDiff.CellClick += new DataGridViewCellEventHandler(DataGridViewTempDiff_CellClick); // 添加一些示例数据 AddSampleTempDiffData(); @@ -127,25 +132,88 @@ namespace JoyD.Windows.CS private void AddSampleTempDiffData() { - // 添加示例行 - dataGridViewTempDiff.Rows.Add("10°C", "选择"); - dataGridViewTempDiff.Rows.Add("20°C", "选择"); - dataGridViewTempDiff.Rows.Add("30°C", "选择"); + // 清除现有数据 + tempDiffData.Clear(); + dataGridViewTempDiff.Rows.Clear(); + + // 添加示例数据并设置默认颜色 + 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 rowData = new Dictionary { - // 处理颜色选择按钮点击 + { "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) { using (ColorDialog colorDialog = new ColorDialog()) { + // 设置初始颜色为当前行的颜色 + colorDialog.Color = (Color)tempDiffData[e.RowIndex]["color"]; + if (colorDialog.ShowDialog() == DialogResult.OK) { - // 这里可以保存选择的颜色 - Console.WriteLine("选择的颜色: " + colorDialog.Color.Name); + // 更新颜色数据 + tempDiffData[e.RowIndex]["color"] = colorDialog.Color; + // 刷新单元格以显示新颜色 + dataGridViewTempDiff.Refresh(); } } }