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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3166244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:28:39  来源:igfitidea点击:

How to increase the size of checkbox in WinForms?

.netwinforms

提问by Amitabh

How do I increase the size of a checkbox in a .Net WinForm. I tried Height and Width but it does not increases the Box size.

如何增加 .Net WinForm 中复选框的大小。我尝试了 Height 和 Width 但它不会增加 Box 的大小。

回答by Hans Passant

The check box size is hardcoded inside Windows Forms, you cannot mess with it. One possible workaround is to draw a check box on top of the existing one. It is not a great solution since auto-sizing cannot work anymore as-is and text alignment is muddled, but it is serviceable.

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

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Adjust the size of the control so you get the desired box size and ensure it is wide enough to fit the text.

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

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

回答by Ketchup God

There's an AutoSizeoption in the Propertieswindows; if you turn that off by changing it to False, you will be able to modify the size of your CheckBox.

窗口中有一个AutoSize选项Properties;如果您通过将其更改为 来关闭它False,您将能够修改CheckBox.

回答by barbara.post

C# version, from a forum.codecall.net topic:

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

回答by Jim

If anyone needs VB.NET code, I adapted this code to it. I didn't fool with AutoSizein the class. The control should be added to the toolbox once the project is built. You can set AutoSizeto Falsethere the same as you would any other control.

如果有人需要 VB.NET 代码,我将这段代码改编成它。我没有AutoSize在课堂上胡说八道。构建项目后,应将控件添加到工具箱中。您可以像设置任何其他控件一样设置AutoSizeFalse那里。

If it matters, I just put the class code below the End Classof the form I was using it in.

如果重要的话,我只是将类代码放在End Class我使用它的表单的下方。

To clarify what AutoSizedoes, the advantage of this new control is that the "box" portion of the checkbox can be made bigger. In the normal checkbox, you cannot change the box portion.

为了澄清是什么AutoSize,这个新控件的优点是复选框的“框”部分可以做得更大。在普通复选框中,您不能更改框部分。

The only disadvantage of this new control that I see is that when you resize it the box portion overruns the text if left aligned; fix this with the TextAlignproperty.

我看到的这个新控件的唯一缺点是,当您调整它的大小时,如果左对齐,框部分会超出文本;用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

回答by Amritendu

A simple workaround is to use the appearance property of the CheckBox. Use the following code to look the checkbox like a button. It will function as a checkbox. Now the size can be easily changed along with the text size.

一个简单的解决方法是使用 CheckBox 的外观属性。使用以下代码使复选框看起来像一个按钮。它将用作复选框。现在可以轻松更改大小和文本大小。

checkBox1.Appearance = Appearance.Button;
checkBox1.Font = new Font("Microsoft Sans Serif", 16);
checkBox1.AutoSize = false;
checkBox1.Size = new Size(100, 100);

回答by Hotkey

It support Flat

它支持平面

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

回答by Graham Laight

Use a different control (e.g. label or button) and just program the onclick event to change its appearance in an acceptable way.

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