代码之家  ›  专栏  ›  技术社区  ›  Amitabh

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

  •  30
  • Amitabh  · 技术社区  · 14 年前

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

    6 回复  |  直到 14 年前
        1
  •  39
  •   Hans Passant    14 年前

    复选框的大小在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);
        }
    }
    
        2
  •  16
  •   ASertacAkkaya    9 年前

    有一个 AutoSize 中的选项 Properties 窗户;如果你把它改成 False ,您将能够修改 CheckBox

        3
  •  6
  •   barbara.post    10 年前

    C#版本,从 a forum.codecall.net topic :

     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);
            }
        }
    
        4
  •  1
  •   Ry- Vincenzo Alcamo    11 年前

    如果有人需要VB.NET代码,我会将此代码改编为它。我没骗你 AutoSize 自动大小 False 和其他控制一样。

    如果有关系的话,我只是把类代码放在 End Class 我使用它的形式。

    自动大小 是的,这个新控件的优点是复选框的“框”部分可以变大。在“普通”复选框中,不能更改框部分。

    TextAlign 财产。

    Public Class NewCheckBox
        Inherits CheckBox
    
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)
    
            'Make the box you check 3/4 the height
            Dim boxsize As Integer = Me.Height * 0.75
            Dim rect As New Rectangle(
                New Point(0, Me.Height / 2 - boxsize / 2),
                New Size(boxsize, boxsize)
            )
    
            ControlPaint.DrawCheckBox(e.Graphics, rect, If(Me.Checked, ButtonState.Checked, ButtonState.Normal))
        End Sub
    End Class
    
        5
  •  1
  •   Amritendu Mukhopadhyay    6 年前

    checkBox1.Appearance = Appearance.Button;
    checkBox1.Font = new Font("Microsoft Sans Serif", 16);
    checkBox1.AutoSize = false;
    checkBox1.Size = new Size(100, 100);
    
        6
  •  0
  •   Graham Laight    7 年前

    使用不同的控件(例如标签或按钮),只需编程onclick事件,就可以以可接受的方式更改其外观。

        7
  •  0
  •   Hotkey    5 年前

    It支撑平面

    using System.Drawing;
    using System.Windows.Forms;
    
    public class TodoCheckBox : CheckBox
    {
        public override bool AutoSize
        {
            get => base.AutoSize;
            set => base.AutoSize = false;
        }
    
        public TodoCheckBox()
        {
            this.TextAlign = ContentAlignment.MiddleRight;
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            int h = this.ClientSize.Height - 2;
            var rc = new Rectangle(new Point(-1, this.Height / 2 - h / 2), new Size(h, h));
            if (this.FlatStyle == FlatStyle.Flat)
            {
                ControlPaint.DrawCheckBox(e.Graphics, rc, this.Checked ? ButtonState.Flat | ButtonState.Checked : ButtonState.Flat | ButtonState.Normal);
            }
            else
            {
                ControlPaint.DrawCheckBox(e.Graphics, rc, this.Checked ? ButtonState.Checked : ButtonState.Normal);
            }
        }
    }