【问题标题】:How to Create bigger CheckBox如何创建更大的复选框
【发布时间】:2019-08-05 18:03:10
【问题描述】:

我想在 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);
    }
}

RadioButton 也尝试过,但我不知道如何应用颜色和突出显示文本。

【问题讨论】:

  • 复选框和单选按钮的大小由主题决定。改变你画一个矩形的大小不会改变控件的大小
  • 您的图片是否来自网站?不同的技术。 WinForms 使用 CheckBoxRenderer。 ControlPaint 是旧的。
  • 当我增加复选框文本大小时,框大小非常小。我必须增加我的复选框大小。
  • 我只需要在 windows 窗体中进行设计。我不应该使用任何图像
  • 那么您将不得不自己绘制整个东西,因为您有一个非常自定义的规范。

标签: c# winforms


【解决方案1】:
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsForms
{
   public class BiggerCheckBox : CheckBox
   {
        #region variables
        private int _boxsize = 14;
        private int _boxlocatx = 0;
        private int _boxlocaty = 0;
        private int _textX = 16;
        private int _textY = 1;
        private Color _boxBackColor = Color.Transparent;
        private Color _tickColor = Color.Black;
        private float _tickSize = 11f;
        private Color _boxColor = Color.Black;
        private float _tickLeftPosition = 0f;
        private float _tickTopPosition = 0f;
        #endregion

        #region Properties
        public int TextLocationX
        {
            get { return _textX; }
            set { _textX = value; Invalidate(); }
        }

        public int TextLocationY
        {
            get { return _textY; }
            set { _textY = value; Invalidate(); }
        }

        public int BoxSize
        {
            get { return _boxsize; }
            set { _boxsize = value; Invalidate(); }
        }

        public int BoxLocationX
        {
            get { return _boxlocatx; }
            set { _boxlocatx = value; Invalidate(); }
        }

        public int BoxLocationY
        {
            get { return _boxlocaty; }
            set { _boxlocaty = value; Invalidate(); }
        }
        public Color BoxBackColor
        {
            get { return _boxBackColor; }
            set { _boxBackColor = value; Invalidate(); }
        }
        public Color TickColor
        {
            get { return _tickColor; }
            set { _tickColor = value; Invalidate(); }
        }
        public float TickSize
        {
            get { return _tickSize; }
            set { _tickSize = value; Invalidate(); }
        }
        public Color BoxColor
        {
            get { return _boxColor; }
            set { _boxColor = value; Invalidate(); }
        }
        public float TickLeftPosition
        {
            get { return _tickLeftPosition; }
            set { _tickLeftPosition = value; Invalidate(); }
        }
        public float TickTopPosition
        {
            get { return _tickTopPosition; }
            set { _tickTopPosition = value; Invalidate(); }
        }
        #endregion

        #region Constrctors
        public BiggerCheckBox()
        {
            Appearance = Appearance.Button;
            FlatStyle = FlatStyle.Flat;
            TextAlign = ContentAlignment.MiddleRight;
            FlatAppearance.BorderSize = 0;
            AutoSize = false;
        }
        #endregion

        #region Methods
        protected override void OnPaint(PaintEventArgs pevent)
        {
            try
            {
                base.OnPaint(pevent);
                pevent.Graphics.Clear(BackColor);

                //checkbox text - using draw string method with specified location
                using (SolidBrush brushText = new SolidBrush(ForeColor))
                {
                    pevent.Graphics.DrawString(Text, Font, brushText, _textX, _textY);
                }

                //checkbox box -  using rectangle for checkbox box
                Rectangle _rectangleBox = new Rectangle(_boxlocatx, _boxlocaty, _boxsize, _boxsize);

                //checkbox box -  checckbox box back color and border color
                using (SolidBrush brushBackColor = new SolidBrush(_boxBackColor))
                {
                    pevent.Graphics.FillRectangle(brushBackColor, _rectangleBox);
                }
                using (Pen penBoxColor = new Pen(_boxColor))
                {
                    pevent.Graphics.DrawRectangle(penBoxColor, _rectangleBox);
                }

                //checkbox box -  check and uncheck
                if (Checked)
                {
                    using (SolidBrush brush = new SolidBrush(_tickColor))
                    {
                        using (Font wing = new Font("Wingdings", _tickSize))
                        {
                            pevent.Graphics.DrawString("ü", wing, brush, _tickLeftPosition, _tickTopPosition);
                        }
                    }
                }
                pevent.Dispose();
            }
            catch (Exception ex)
            {

            }
        }
        #endregion
    }
}

【讨论】:

  • 我写了上面的代码。它为我工作。但这里的问题是像“盒子大小”、“盒子位置 x 和 y”、“文本位置 x 和 y”、“刻度位置左侧和顶部”和“刻度大小”之类的一切,我用属性创建的每个东西都可以正确对齐一切。
  • 同时增加复选框的字体和大小。
猜你喜欢
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 2022-01-02
  • 2014-06-12
  • 2015-05-22
  • 2014-07-31
  • 2014-05-09
相关资源
最近更新 更多