gpt4 book ai didi

c# - 如何更改 DatagridviewCheckboxCell 中的复选框大小

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:49 25 4
gpt4 key购买 nike

我知道复选框的大小可以像这样改变。

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 上:

enter image description here

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填充单元格:

enter image description here

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);

enter image description here

关于c# - 如何更改 DatagridviewCheckboxCell 中的复选框大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36171250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com