简体   繁体   English

如何更改复选框的形状?

[英]How do I change the shape of a checkbox?

Just a silly display question, but how do I edit the shape of my checkbox in WinForms? 只是一个愚蠢的显示问题,但是如何在WinForms中编辑复选框的形状?

To be specific, instead of the checkmark when I click on the 3 state checkbox, I would like a square. 具体来说,我想要一个正方形,而不是单击3状态复选框时的复选标记。 I saw this in a homework assignment and it's purely display, but I just can't find where to edit it. 我在家庭作业中看到了这一点,它纯粹是显示的,但是我找不到在哪里编辑它。

I'm using Visual Studio C# for Windows Forms btw. 我正在使用Visual Studio C#for Windows Forms btw。

在此处输入图片说明

http://imgur.com/a/SkHW9 http://imgur.com/a/SkHW9

This is what the "Big" checkbox should look like 这就是“大”复选框的外观

You can try this code: 您可以尝试以下代码:

    private void checkBox1_Paint(object sender, PaintEventArgs e)
    {
        CheckState cs = checkBox1.CheckState;
        if (cs == CheckState.Indeterminate)
        {
            using (SolidBrush brush = new SolidBrush(checkBox2.BackColor))
                e.Graphics.FillRectangle(brush, 0, 1, 14, 14);
            e.Graphics.FillRectangle(Brushes.Green, 3, 4, 8, 8);
            e.Graphics.DrawRectangle(Pens.Black, 0, 1, 13, 13);
        }
    }

在此处输入图片说明

This should be easy to modify if you want something else.. 如果您需要其他功能,应该可以轻松修改。

Note that you may need to adapt it when changing fonts and surely will have to modify it when changing the alignments..! 请注意,更改字体时可能需要对其进行调整,并且在更改对齐方式时肯定会对其进行修改。 Also when changing DPI. 更改DPI时也是如此。 Or themes. 或主题。 Or Windows versions. 或Windows版本。 Or half a dozen other things. 或其他六件事。 So this is more an example than a recommendation! 因此,这不是推荐而是一个示例!

You may also read the interesting comments here.. and this example of more involved checkbox drawing.. 您还可以在这里阅读有趣的评论。以及更多涉及复选框绘制的示例

In order to modify shape any control you need to use Paint event. 为了修改形状,任何控件都需要使用Paint事件。 For example if you add two radio buttons at form, and for each Paint event bind following code: 例如,如果您在窗体上添加两个单选按钮,并且为每个Paint事件绑定以下代码:

private void radioButton_Paint(object sender, PaintEventArgs e)
{
        Graphics graphics = e.Graphics;
        graphics.Clear(BackColor);

        int offset = 2;
        SizeF stringMeasure = graphics.MeasureString(radioButton1.Name, Font);
        // calculate offsets
        int leftOffset = offset + Padding.Left;
        int topOffset = (int)(ClientRectangle.Height - stringMeasure.Height) / 2;

        if (topOffset < 0)
        {
            topOffset = offset + Padding.Top;
        }
        else
        {
            topOffset += Padding.Top;
        }

        graphics.FillRectangle(new SolidBrush(Color.AliceBlue), 0, 0, leftOffset + 10, topOffset + 10);
        graphics.DrawRectangle(new Pen(Color.Green), new Rectangle(0, 0, leftOffset + 10, leftOffset + 10));

        graphics.DrawString(radioButton1.Text, (sender as RadioButton).Font, new SolidBrush(Color.IndianRed), 15, 0);

        if( (sender as RadioButton).Checked)
        {
            graphics.FillRectangle(new SolidBrush(Color.Yellow), 1, 1, leftOffset + 8, 10);
        }

}

you'll see following picture: 您将看到以下图片:

单选按钮行为代码演示

You have to play with the CheckState property of the checkbox using the Checked , Unchecked or Indeterminate state 您必须使用CheckedUncheckedIndeterminate状态来使用复选框的CheckState属性

a pretty str.forw example: 一个漂亮的str.forw示例:

private void AdjustMyCheckBoxProperties()
 {
    // Change the ThreeState and CheckAlign properties on every other click.
    if (!checkBox1.ThreeState)
    {
       checkBox1.ThreeState = true;
       checkBox1.CheckAlign = ContentAlignment.MiddleRight;
    }
    else
    {
       checkBox1.ThreeState = false;
       checkBox1.CheckAlign = ContentAlignment.MiddleLeft;
    }

    // Concatenate the property values together on three lines.
    label1.Text = "ThreeState: " + checkBox1.ThreeState.ToString() + "\n" +
                  "Checked: " + checkBox1.Checked.ToString() + "\n" +
                  "CheckState: " + checkBox1.CheckState.ToString(); 
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM