Files
1258/JinYuan.ControlLib/LightweightGridPanel.cs
T

150 lines
5.0 KiB
C#
Raw Normal View History

2026-08-04 18:36:40 +08:00
using System;
using System.Drawing;
using System.Windows.Forms;
namespace JinYuan.ControlLib
{
public partial class LightweightGridPanel : Panel
{
public int Rows { get; set; }
public int Columns { get; set; }
public int CellSize { get; set; }
public int HeaderHeight { get; set; }
public int RowHeaderWidth { get; set; }
public int BorderWidth { get; set; } = 1;
private bool _styleSet = false;
public LightweightGridPanel(int rows, int cols, int cellSize, int headerHeight, int rowHeaderWidth)
{
Rows = rows;
Columns = cols;
CellSize = cellSize;
HeaderHeight = headerHeight;
RowHeaderWidth = rowHeaderWidth;
DoubleBuffered = true;
SetControlStyles();
UpdateStyles();
CalculateAndSetSize();
BackColor = Color.FromArgb(28, 40, 55);
BorderStyle = BorderStyle.FixedSingle;
}
/// <summary>
/// 尺寸计算
/// </summary>
private void CalculateAndSetSize()
{
// 总宽度 = 行号列宽度 + 列数*单元格尺寸 + (列数+1)*边框宽度
int totalWidth = RowHeaderWidth + (Columns * CellSize) + (Columns + 1) * BorderWidth;
// 总高度 = 表头高度 + 行数*单元格尺寸 + (行数+1)*边框宽度
int totalHeight = HeaderHeight + (Rows * CellSize) + (Rows + 1) * BorderWidth;
// 关键:设置最小/最大尺寸,同时允许手动调整(避免固定死导致适配问题)
MinimumSize = new Size(totalWidth, totalHeight);
Size = MinimumSize;
MaximumSize = new Size(totalWidth + 100, totalHeight + 100); // 留余量
}
private void SetControlStyles()
{
if (_styleSet) return;
// 仅保留必要的双缓冲样式,关闭ResizeRedraw
SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
SetStyle(ControlStyles.ResizeRedraw, false);
_styleSet = true;
}
/// <summary>
/// 控件位置/尺寸计算,确保在可视区域内
/// </summary>
public void AddControl(Control ctrl, int row, int col)
{
if (ctrl == null) return;
int x = 0, y = 0;
int ctrlWidth = 0, ctrlHeight = 0;
// 左上角角落(行\列)
if (row == 0 && col == 0)
{
x = BorderWidth;
y = BorderWidth;
ctrlWidth = RowHeaderWidth - BorderWidth * 2;
ctrlHeight = HeaderHeight - BorderWidth * 2;
}
// 表头列(列号)
else if (row == 0)
{
x = RowHeaderWidth + (col - 1) * (CellSize + BorderWidth) + BorderWidth;
y = BorderWidth;
ctrlWidth = CellSize - BorderWidth * 2;
ctrlHeight = HeaderHeight - BorderWidth * 2;
}
// 行号列
else if (col == 0)
{
x = BorderWidth;
y = HeaderHeight + (row - 1) * (CellSize + BorderWidth) + BorderWidth;
ctrlWidth = RowHeaderWidth - BorderWidth * 2;
ctrlHeight = CellSize - BorderWidth * 2;
}
// 电池控件(数据区)
else
{
x = RowHeaderWidth + (col - 1) * CellSize + (col) * BorderWidth;
y = HeaderHeight + (row - 1) * CellSize + (row) * BorderWidth;
ctrlWidth = CellSize - BorderWidth * 2;
ctrlHeight = CellSize - BorderWidth * 2;
}
// 强制设置控件尺寸(避免尺寸为0)
ctrl.Size = new Size(Math.Max(10, ctrlWidth), Math.Max(10, ctrlHeight));
ctrl.Location = new Point(x, y);
ctrl.Margin = new Padding(0);
ctrl.Padding = new Padding(0);
ctrl.Dock = DockStyle.None;
ctrl.Visible = true;
// 确保子控件在最上层绘制
ctrl.BringToFront();
Controls.Add(ctrl);
}
// 重写SetStyle,直接调用基类
protected new void SetStyle(ControlStyles flags, bool value)
{
base.SetStyle(flags, value);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
// 确保控件重新布局
if (Controls.Count > 0)
{
Invalidate(); // 触发重绘
}
}
// 添加网格线抗锯齿
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
base.OnPaint(e);
}
}
}