【问题标题】:How can I change the color of the check mark of a CheckBox? [duplicate]如何更改 CheckBox 的复选标记的颜色? [复制]
【发布时间】:2016-05-17 02:19:16
【问题描述】:

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

  • checkBox1.Checked = false

  • checkBox1.Checked = true

非常感谢你们每个人都准确地回应了这个请求!

【问题讨论】:

  • 请不要这样做。使用每个人都会认识并知道如何使用的常规复选框。使用能够正确处理键盘导航的一种。并使用符合用户选择的主题的主题,这样它就不会像拇指酸痛一样突出。内置控件已经过彻底调试;您的一次性控制可能更像是一个方轮。您的用户通常不会分享您的个人审美意识。最好将注意力集中在功能上。

标签: c# winforms


【解决方案1】:

您可以简单地在 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 事件。这是一个例子:

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

您可能需要调整控件和字体的大小。如果您想扩展代码以支持 TextAlignCheckAlign 属性。

如果您需要一个三态控件,您可以调整代码以显示第三种状态外观,特别是如果您认为一个看起来比原始控件更好..

【讨论】:

  • 我认为这最后一个代码是我想要做的,但出来的只是一个带有彩色边框的正方形,上面有一个不允许任何交互的“平面”按钮。
  • 我错过了Checked 的检查,抱歉;更新。除此之外,它在这里工作..
  • 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;
  • 当您通过 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);..
  • 我已经修改了类以包含焦点矩形并进行所有文本绘制..
【解决方案2】:

您必须编写自己的复选框。通过制作一个自定义控件,其中有一个蓝色方块(可能继承自 Button),通过 OnClick 事件在选中和未选中图像之间切换,并在其旁边放置一个标签。

【讨论】:

    猜你喜欢
    • 2017-11-22
    • 2012-07-03
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多