如何改变 CheckBox 中勾选标记的颜色?

12
我想改变正方形的边框颜色和背景以及勾选标记的颜色,但不想改变文本。 为了更好地理解,我想要实现以下示例:

  • checkBox1.Checked = false

  • checkBox1.Checked = true

非常感谢每位回应此请求的人!


1
请不要这样做。使用每个人都能识别和知道如何使用的常规复选框。使用一个可以正确处理键盘导航的复选框。并且使用符合用户选择主题的复选框,以便它不会像醒目的疼痛一样突出。内置控件已经经过彻底的调试;您的单独控件可能更像方轮。您个人的审美感通常不被用户所共享。最好将您的重点放在功能上。 - Cody Gray
2个回答

22

您可以在Paint事件中简单地绘制复选标记:

输入图片说明

private void checkBox1_Paint(object sender, PaintEventArgs e)
{
    Point pt = new  Point(e.ClipRectangle.Left + 2, e.ClipRectangle.Top + 4);
    Rectangle rect = new Rectangle(pt, new Size(22, 22));
    if (checkBox1.Checked)
    {
       using (Font wing = new Font("Wingdings", 14f))
          e.Graphics.DrawString("ü", wing, Brushes.DarkOrange,rect);
    }
    e.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect);
}
为了使它起作用,您需要:
  • 设置 Apperance = Appearance.Button
  • 设置 FlatStyle = FlatStyle.Flat
  • 设置 TextAlign = ContentAlignment.MiddleRight
  • 设置 FlatAppearance.BorderSize = 0
  • 设置 AutoSize = false
如果要重复使用此功能,最好创建复选框的子类并在那里覆盖OnPaint事件。以下是一个示例:

enter image description here

public ColorCheckBox()
{
    Appearance = System.Windows.Forms.Appearance.Button;
    FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    TextAlign = ContentAlignment.MiddleRight;
    FlatAppearance.BorderSize = 0;
    AutoSize = false;
    Height = 16;
}

protected override void OnPaint(PaintEventArgs pevent)
{
    //base.OnPaint(pevent);

    pevent.Graphics.Clear(BackColor);

    using (SolidBrush brush = new SolidBrush(ForeColor))
        pevent.Graphics.DrawString(Text, Font, brush, 27, 4);

    Point pt = new Point( 4 ,  4);
    Rectangle rect = new Rectangle(pt, new Size(16, 16));

    pevent.Graphics.FillRectangle(Brushes.Beige, rect);

    if (Checked)
    {
        using (SolidBrush brush = new SolidBrush(ccol))
        using (Font wing = new Font("Wingdings", 12f))
            pevent.Graphics.DrawString("ü", wing, brush, 1,2);
    }
    pevent.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect);

    Rectangle fRect = ClientRectangle;

    if (Focused)
    {
        fRect.Inflate(-1, -1);
        using (Pen pen = new Pen(Brushes.Gray) { DashStyle = DashStyle.Dot })
            pevent.Graphics.DrawRectangle(pen, fRect);
    }
}
你可能需要调整控件的尺寸和字体大小。如果你想要扩展代码,以支持和属性,也可以这样做。
如果你需要一个三态控件,你可以修改代码以显示第三状态的外观,特别是如果你认为有比原来更好看的样式。

很抱歉,但它仍然无法工作。为什么必须将其视为按钮? - user5894584
这样做是为了避免它绘制自己的复选标记。标准用法是将其视为保持按下状态的按钮。添加 if (Checked) 子句后,什么不起作用?你设置了 CheckColor 属性吗? - TaW
1
`base.OnPaint(pevent); Point pt = new Point(pevent.ClipRectangle.Left , pevent.ClipRectangle.Top + 4); Rectangle rect = new Rectangle(pt, new Size(16, 16)); pevent.Graphics.FillRectangle(Brushes.Beige, rect); if (Checked) { using (SolidBrush brush = new SolidBrush(ccol)) using (Font wing = new Font("Wingdings", 12f)) pevent.Graphics.DrawString("ü", wing, brush, -1,2); } pevent.Graphics.DrawRectangle(Pens.DarkSlateBlue, rect); Rectangle fRect = ClientRectangle;` - TaW
1
当您通过TAB键到达控件时,它会显示“焦点矩形”。如果您愿意,可以以任何想要的方式自己绘制它,但是那么您需要__全部__进行绘制:protected override void OnPaint(PaintEventArgs pevent) { //base.OnPaint(pevent); pevent.Graphics.Clear(SystemColors.Control); using (SolidBrush brush = new SolidBrush(ForeColor)) pevent.Graphics.DrawString(Text, Font, brush, 22, 4); } - TaW
2
我已经修改了这个类,包括焦点矩形和所有文本绘制。 - TaW
显示剩余13条评论

3

你需要编写自己的复选框。通过创建一个自定义控件,在里面包含一个蓝色正方形(可能继承自Button),并通过OnClick事件切换选中和未选中图像,然后将标签放在其旁边。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接