我知道复选框的大小可以像这样改变。
checkBox1.Size = new Size(10, 10);
我想用 DataGridViewCheckBoxColumn 更改 DataGridview 中的复选框大小,我试图继承 DatagridviewCheckboxCell,但我发现有任何方法可以做到这一点。
class DGCBC : DataGridViewCheckBoxColumn
{
public DGCBC()
{
this.CellTemplate = new DatagridviewCheckboxCustomCell();
}
class DatagridviewCheckboxCustomCell : DataGridViewCheckBoxCell
{
public int row_index { get; set; }
/// <summary>
/// constructor
/// </summary>
///
public DatagridviewCheckboxCustomCell()
{
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState,
object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
*//I tried many way in there,but it's not work*
base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
}
要以您机器的当前样式绘制系统控件,您应该使用 ControlPaint class 中的许多便捷方法之一。 .
下面是一个示例,将三个 Checkboxes
绘制到 Panel
上:
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawCheckBox(e.Graphics, 11, 11, 22, 22, ButtonState.Checked);
ControlPaint.DrawCheckBox(e.Graphics, 11, 44, 33, 33, ButtonState.Checked);
ControlPaint.DrawCheckBox(e.Graphics, 11, 88, 44, 44, ButtonState.Checked);
}
当然,您需要在 CellPainting
事件中对此进行调整,以使用您想要的大小和单元格的坐标!
这是一个简单的示例,它几乎用 CheckBox
填充单元格:
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.ColumnIndex == 1 && e.RowIndex >= 0)
{
e.PaintBackground(e.CellBounds, true);
ControlPaint.DrawCheckBox(e.Graphics, e.CellBounds.X + 1, e.CellBounds.Y + 1,
e.CellBounds.Width - 2, e.CellBounds.Height - 2,
(bool) e.FormattedValue ? ButtonState.Checked : ButtonState.Normal);
e.Handled = true;
}
您会想要找到适合您需要的尺寸..
请注意,您可以组合一些 ButtonState .因此,要实现平面外观,这是 DataGridView CheckBoxCells 的默认设置,您可以编写 ButtonState.Checked | ButtonState.Flat
等..:
ControlPaint.DrawCheckBox(e.Graphics, 11, 11, 22, 22, ButtonState.Checked);
ControlPaint.DrawCheckBox(e.Graphics, 11, 44, 33, 33, ButtonState.Checked | ButtonState.Flat);
ControlPaint.DrawCheckBox(e.Graphics, 11, 88, 44, 44, ButtonState.Checked | ButtonState.Flat | ButtonState.Inactive);
我是一名优秀的程序员,十分优秀!