如何在WinForms中增加复选框的大小?

Ami*_*abh 32 .net winforms

如何增加.Net WinForm中复选框的大小.我尝试了高度和宽度,但它不会增加Box的大小.

Han*_*ant 36

复选框大小在Windows窗体内是硬编码的,你不能搞砸它.一种可能的解决方法是在现有复选框的顶部绘制一个复选框.这不是一个很好的解决方案,因为自动调整大小不再可以工作,文本对齐混乱,但它是可用的.

在项目中添加一个新类并粘贴下面显示的代码.编译.将新控件从工具箱顶部拖放到表单上.调整控件的大小,以便获得所需的盒子大小,并确保其宽度足以适合文本.

using System;
using System.Drawing;
using System.Windows.Forms;

class MyCheckBox : CheckBox {
    public MyCheckBox() {
        this.TextAlign = ContentAlignment.MiddleRight;
    }
    public override bool AutoSize {
        get { return base.AutoSize; }
        set { base.AutoSize = false; }
    }
    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        int h = this.ClientSize.Height - 2;
        Rectangle rc = new Rectangle(new Point(0, 1), new Size(h, h));
        ControlPaint.DrawCheckBox(e.Graphics, rc,
            this.Checked ? ButtonState.Checked : ButtonState.Normal);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 调用`base.OnPaint(e);`还会在屏幕上留下一些像素,我宁愿调用`e.Graphics.Clear(this.BackColor);` (3认同)
  • `var rc = new Rectangle(new Point(0, this.Height / 2 - h / 2), new Size(h, h));` 这允许复选框垂直居中并保持其后面的复选框被覆盖. 此外,您可能希望根据您的用途将高度限制在 20 像素左右,之后看起来有点可笑。 (2认同)

ASe*_*aya 17

窗户有一个AutoSize选项Properties; 如果你通过改变它来关闭它False,你将能够修改你的大小CheckBox.

  • 我将AutoSize设置为false,我无法更改大小. (3认同)
  • 你不会有一个更大的复选框,你只会让它的边框更大,所以点击会变得更容易 (3认同)

bar*_*ost 6

C#版本,来自forum.codecall.net主题:

 class BigCheckBox : CheckBox
    {
        public BigCheckBox()
        {
            this.Text = "Approved";
            this.TextAlign = ContentAlignment.MiddleRight;              
        }

        public override bool AutoSize
        {
            set { base.AutoSize = false; }
            get { return base.AutoSize; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            this.Height = 100;
            this.Width = 200;
            int squareSide = 80;

            Rectangle rect = new Rectangle(new Point(0, 1), new Size(squareSide, squareSide));

            ControlPaint.DrawCheckBox(e.Graphics, rect, this.Checked ? ButtonState.Checked : ButtonState.Normal);
        }
    }
Run Code Online (Sandbox Code Playgroud)