如何更改复选框的形状?

时间:2016-12-08 15:58:54

标签: c# winforms

只是一个愚蠢的显示问题,但如何在WinForms中编辑我的复选框的形状?

具体而言,当我点击3状态复选框时,我想要一个正方形而不是复选标记。我在家庭作业中看到了这一点并且它只是显示,但我无法找到编辑它的位置。

我正在使用Visual Studio C#for Windows Forms btw。

enter image description here

http://imgur.com/a/SkHW9

这就是" Big"复选框应该看起来像

3 个答案:

答案 0 :(得分:4)

您可以尝试以下代码:

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

enter image description here

如果你想要别的东西,这应该很容易修改..

请注意,在更改字体时可能需要对其进行调整,并且在更改对齐时必须对其进行修改..!也在改变DPI时。或主题。或Windows版本。或者其他六件事。所以这是一个例子而不是推荐!

您还可以阅读更多相关复选框图的interesting comments here..this example ..

答案 1 :(得分:1)

为了修改形状任何控件,您需要使用Paint事件。例如,如果您在表单中添加两个单选按钮,并为每个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);
        }

}

您将看到以下图片:

Radio button behaviour code demo

答案 2 :(得分:0)

您必须使用复选框的https://stackoverflow.com/a/206682/6193608属性 使用已选中未选中不确定状态

一个非常严格的例子:

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(); 
 }
相关问题