专注VB编程开发20年

专注VB编程开发20年

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

 

最佳答案

 

复选框的大小在Windows窗体内是硬编码的,您不能将其弄乱。一种可能的解决方法是在现有复选框的上方绘制一个复选框。这不是一个很好的解决方案,因为自动调整大小无法按原样工作,并且文本对齐困惑了,但是可以使用。

 

在您的项目中添加一个新类,并粘贴以下代码。编译。将新控件从工具箱的顶部拖放到窗体上。调整控件的大小,以便获得所需的框大小,并确保它足够宽以适合文本。

<code>

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

    }

}

</code>

01-07 19:33