如何更改CheckBox复选标记的颜色? [英] How can I change the color of the check mark of a CheckBox?

查看:52
本文介绍了如何更改CheckBox复选标记的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改边框颜色和正方形的背景以及复选标记的颜色,但不能更改文本.为了更好地理解,我想完成的是以下示例:

I want to change the border color and the background of the square and the color of the check mark, but not the text. To better understand, what I want to accomplish is the following example:

  • checkBox1.Checked = false

  • checkBox1.Checked = true

非常感谢大家对这个要求的答复!

Thanks so much to everyone of you responds exactly to this request!

推荐答案

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

You can simply draw the checkmark in the Paint event:

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

要使其正常工作,您需要:

for this to work you need to:

  • 设置 Apperance = Appearance.Button
  • 设置 FlatStyle = FlatStyle.Flat
  • 设置 TextAlign = ContentAlignment.MiddleRight
  • 设置 FlatAppearance.BorderSize = 0
  • 设置 AutoSize = false

如果要重复使用此功能,最好将其子类化,并在其中覆盖 OnPaint 事件.这是一个示例:

If you want to re-use this it will be best to subclass the checkbox and override the OnPaint event there. Here is an example:

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

您可能需要调整控件和字体的大小..如果要扩展代码,以使用 TextAlign CheckAlign 属性.

You may need to tweek the sizes of the control and the font.. And if you want to you expand the code to honor the TextAlign and the CheckAlign properties.

如果您需要三态控件,则可以修改代码以显示第三态外观,尤其是当您想到的外观比原始外观更好时..

And if you need a three-state control you can adapt the code to display a third state appearance, especially if you think of one that looks better than the original..

这篇关于如何更改CheckBox复选标记的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆