C#Winform拓展控件之GroupBox

      感觉Winform自带的GroupBox有点丑,而且用着有点呆,很难满足我们的需求,所以就在原来的基础上进行一点拓展。

       先看效果

控件的自定义属性

 实现了改变边框的颜色,标题颜色和内部颜色,以及标题文字的位置的功能。

主要实现:

在OnPaint事件内,先清空原来的,然后再绘制出边框线以及标题文字。

注意标题文字的位置,在上下两条边上还好,在左右两条边上时,可以进行坐标的旋转,这里注意一下旋转的方向即可。其他估计也没什么难点。

所有代码附上:

    /// <summary>
    /// 拓展GroupBox
    /// </summary>
    [ToolboxBitmap(typeof(System.Windows.Forms.GroupBox))]
    [Description("拓展GroupBox, 可以设置边框、标题、字体等")]
    public partial class yGroupBox : GroupBox
    {
        #region 构造函数 

        /// <summary>
        /// 拓展GroupBox
        /// </summary>
        public yGroupBox()
        {
            InitializeComponent();
        }
        #endregion

        #region 变量定义

        private Color borderColor = Color.MediumSlateBlue;//边框颜色
        private int borderSize = 2;//边框粗细
        private Color contentColor = Color.Black;//标题颜色
        private ContentPosition contentPosition = ContentPosition.Top_Left;//标题位置
        private Font contentFont = new Font("微软雅黑", 9.5F);//标题字体 
        private string context = "标题文字";//标题文字
        // 标志位 用来判断是初次将控件拖进窗体还是后面修改text
        // 如果是初次拖入窗体 则 将text赋值为控件名 否则就是自己外部输入的text
        private bool flag = true;

        #endregion

        #region 自定属性
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Category("1、自定义属性"), Description("边框颜色")]
        public Color BorderColor
        {
            get
            {
                return borderColor;
            }
            set
            {
                if (value.IsEmpty) value = Color.Black;
                borderColor = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 边框粗细(1-5)
        /// </summary>
        [Category("1、自定义属性"), Description("边框粗细(1-5)")]
        public int BorderSize
        {
            get
            {
                return borderSize;
            }
            set
            {
                if (value < 1)  value = 1;
                if (value > 5)  value = 5;
                borderSize = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 标题颜色
        /// </summary>
        [Category("1、自定义属性"), Description("标题颜色")]
        public Color ContentColor
        {
            get
            {
                return contentColor;
            }
            set
            {
                if (value.IsEmpty)
                    value = Color.Black;
                contentColor = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 标题文字位置
        /// </summary>
        [Category("1、自定义属性"), Description("标题文字位置")]
        [Localizable(true)]
        public ContentPosition ContentPosition
        {
            get
            {
                return contentPosition;
            }
            set
            {
                contentPosition = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 标题字体
        /// </summary>
        [Category("1、自定义属性"), Description("标题字体")]
        public Font ContentFont
        {
            get
            {
                return contentFont;
            }
            set
            {
                value = value ?? new Font("微软雅黑", 9.5F);
                contentFont = value;
                this.Invalidate();
            }
        }

        /// <summary>
        ///  背景颜色
        /// </summary> 
        [Category("1、自定义属性"), Description("背景颜色")]
        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
            }
        }

        /// <summary>
        /// 把原来的Text隐藏掉
        /// </summary>
        [Browsable(false)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                Text = value;
                flag = false;
            }
        }

        /// <summary>
        /// 标题
        /// </summary>
        [Category("1、自定义属性"), Description("标题")]
        public string Context
        {
            get
            {
                return context;
            }

            set
            {
                context = value;
                flag = false;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 背景字体颜色
        /// </summary>
        [Category("1、自定义属性"), Description("背景字体颜色")]
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }
        /// <summary>
        /// 背景字体
        /// </summary>
        [Category("1、自定义属性"), Description("背景字体")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }

        #endregion

        #region 方法及重写
        /// <summary>
        /// 重绘
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (flag == true)
            {
                context = this.Name;
            }
            flag = false;
            Graphics g = e.Graphics;
            SizeF vSize = g.MeasureString(context, contentFont);
            float textWidth = vSize.Width;
            float textHeight = vSize.Height;
            g.Clear(this.BackColor);
            Matrix matrix = g.Transform;
            using (SolidBrush contentBrush = new SolidBrush(contentColor))
            using (Pen vPen = new Pen(borderColor, borderSize))
            {
                switch (contentPosition)
                {
                    case ContentPosition.Top_Left:
                        g.DrawString(context, contentFont, contentBrush, 10, 0);
                        g.DrawLine(vPen, 1, textHeight / 2, 8, textHeight / 2);
                        g.DrawLine(vPen, textWidth + 8, textHeight / 2, this.Width - 1, textHeight / 2);
                        g.DrawLine(vPen, 2, textHeight / 2, 2, this.Height - 1);
                        g.DrawLine(vPen, 1, this.Height - 2, this.Width - 1, this.Height - 2);
                        g.DrawLine(vPen, this.Width - 2, textHeight / 2, this.Width - 2, this.Height - 1);
                        break;
                    case ContentPosition.Top_Center:
                        g.DrawString(context, contentFont, contentBrush, (this.Width - textWidth) / 2, 0);
                        g.DrawLine(vPen, 1, textHeight / 2, (this.Width - textWidth) / 2 - 2, textHeight / 2);
                        g.DrawLine(vPen, (this.Width + textWidth) / 2 + 2, textHeight / 2, this.Width - 1, textHeight / 2);
                        g.DrawLine(vPen, 2, textHeight / 2, 2, this.Height - 1);
                        g.DrawLine(vPen, 1, this.Height - 2, this.Width - 1, this.Height - 2);
                        g.DrawLine(vPen, this.Width - 2, textHeight / 2, this.Width - 2, this.Height - 1);
                        break;
                    case ContentPosition.Top_Right:
                        g.DrawString(context, contentFont, contentBrush, this.Width - textWidth - 10, 0);
                        g.DrawLine(vPen, 1, textHeight / 2, this.Width - textWidth - 12, textHeight / 2);
                        g.DrawLine(vPen, this.Width - 8, textHeight / 2, this.Width - 1, textHeight / 2);
                        g.DrawLine(vPen, 2, textHeight / 2, 2, this.Height - 1);
                        g.DrawLine(vPen, 1, this.Height - 2, this.Width - 1, this.Height - 2);
                        g.DrawLine(vPen, this.Width - 2, textHeight / 2, this.Width - 2, this.Height - 1);
                        break;
                    case ContentPosition.Left_Top:
                        matrix.RotateAt(-90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawString(context, contentFont, contentBrush, -textWidth - 8, 0);
                        matrix.RotateAt(90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawLine(vPen, textHeight / 2, 2, this.Width - 1, 2);
                        g.DrawLine(vPen, textHeight / 2, 1, textHeight / 2, 8);
                        g.DrawLine(vPen, textHeight / 2, textWidth + 12, textHeight / 2, this.Height - 1);
                        g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - 1);
                        g.DrawLine(vPen, textHeight / 2, this.Height - 2, this.Width - 1, this.Height - 2);
                        break;
                    case ContentPosition.Left_Center:
                        matrix.RotateAt(-90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawString(context, contentFont, contentBrush, -(this.Height + textWidth) / 2, 0);
                        matrix.RotateAt(90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawLine(vPen, textHeight / 2, 2, this.Width - 2, 2);
                        g.DrawLine(vPen, textHeight / 2, 1, textHeight / 2, (this.Height - textWidth) / 2 - 2);
                        g.DrawLine(vPen, textHeight / 2, (this.Height + textWidth) / 2 + 2, textHeight / 2, this.Height - 1);
                        g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - 1);
                        g.DrawLine(vPen, textHeight / 2, this.Height - 2, this.Width - 1, this.Height - 2);
                        break;
                    case ContentPosition.Left_Bottom:
                        matrix.RotateAt(-90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawString(context, contentFont, contentBrush, -this.Height + 10, 0);
                        matrix.RotateAt(90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawLine(vPen, textHeight / 2, 2, this.Width - 2, 2);
                        g.DrawLine(vPen, textHeight / 2, 1, textHeight / 2, this.Height - textWidth - 12);
                        g.DrawLine(vPen, textHeight / 2, this.Height - 8, textHeight / 2, this.Height - 1);
                        g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - 1);
                        g.DrawLine(vPen, textHeight / 2, this.Height - 2, this.Width - 1, this.Height - 2);
                        break;
                    case ContentPosition.Bottom_Left:
                        g.DrawString(context, contentFont, contentBrush, 10, this.Height - textHeight);
                        g.DrawLine(vPen, 1, 2, this.Width - 1, 2);
                        g.DrawLine(vPen, 2, 1, 2, this.Height - textHeight / 2);
                        g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - textHeight / 2);
                        g.DrawLine(vPen, 1, this.Height - textHeight / 2, 8, this.Height - textHeight / 2);
                        g.DrawLine(vPen, 12 + textWidth, this.Height - textHeight / 2, this.Width - 1, this.Height - textHeight / 2);
                        break;
                    case ContentPosition.Bottom_Center:
                        g.DrawString(context, contentFont, contentBrush, (this.Width - textWidth) / 2, this.Height - textHeight);
                        g.DrawLine(vPen, 1, 2, this.Width - 1, 2);
                        g.DrawLine(vPen, 2, 1, 2, this.Height - textHeight / 2);
                        g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - textHeight / 2);
                        g.DrawLine(vPen, 1, this.Height - textHeight / 2, (this.Width - textWidth) / 2 - 2, this.Height - textHeight / 2);
                        g.DrawLine(vPen, (this.Width + textWidth) / 2 + 2, this.Height - textHeight / 2, this.Width - 1, this.Height - textHeight / 2);
                        break;
                    case ContentPosition.Bottom_Right:
                        g.DrawString(context, contentFont, contentBrush, this.Width - textWidth - 10, this.Height - textHeight);
                        g.DrawLine(vPen, 1, 2, this.Width - 1, 2);
                        g.DrawLine(vPen, 2, 1, 2, this.Height - textHeight / 2);
                        g.DrawLine(vPen, this.Width - 2, 1, this.Width - 2, this.Height - textHeight / 2);
                        g.DrawLine(vPen, 1, this.Height - textHeight / 2, this.Width - textWidth - 12, this.Height - textHeight / 2);
                        g.DrawLine(vPen, this.Width - 8, this.Height - textHeight / 2, this.Width - 1, this.Height - textHeight / 2);
                        break;
                    case ContentPosition.Right_Top:
                        matrix.RotateAt(90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawString(context, contentFont, contentBrush, 10, -this.Width);
                        matrix.RotateAt(-90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawLine(vPen, 1, 2, this.Width - textHeight / 2, 2);
                        g.DrawLine(vPen, 2, 1, 2, this.Height - 2);
                        g.DrawLine(vPen, this.Width - textHeight / 2, 1, this.Width - textHeight / 2, 8);
                        g.DrawLine(vPen, this.Width - textHeight / 2, textWidth + 12, this.Width - textHeight / 2, this.Height - 1);
                        g.DrawLine(vPen, 1, this.Height - 2, this.Width - textHeight / 2, this.Height - 2);
                        break;
                    case ContentPosition.Right_Center:
                        matrix.RotateAt(90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawString(context, contentFont, contentBrush, (this.Height - textWidth) / 2, -this.Width);
                        matrix.RotateAt(-90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawLine(vPen, 1, 2, this.Width - textHeight / 2, 2);
                        g.DrawLine(vPen, 2, 1, 2, this.Height - 2);
                        g.DrawLine(vPen, this.Width - textHeight / 2, 1, this.Width - textHeight / 2, (this.Height - textWidth) / 2 - 2);
                        g.DrawLine(vPen, this.Width - textHeight / 2, (this.Height + textWidth) / 2 + 2, this.Width - textHeight / 2, this.Height - 1);
                        g.DrawLine(vPen, 1, this.Height - 2, this.Width - textHeight / 2, this.Height - 2);
                        break;
                    case ContentPosition.Right_Bottom:
                        matrix.RotateAt(90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawString(context, contentFont, contentBrush, this.Height - textWidth - 10, -this.Width);
                        matrix.RotateAt(-90, new PointF(0, 0));
                        g.Transform = matrix;
                        g.DrawLine(vPen, 1, 2, this.Width - textHeight / 2, 2);
                        g.DrawLine(vPen, 2, 1, 2, this.Height - 2);
                        g.DrawLine(vPen, this.Width - textHeight / 2, 1, this.Width - textHeight / 2, this.Height - textWidth - 12);
                        g.DrawLine(vPen, this.Width - textHeight / 2, this.Height - 8, this.Width - textHeight / 2, this.Height - 1);
                        g.DrawLine(vPen, 1, this.Height - 2, this.Width - textHeight / 2, this.Height - 2);
                        break;
                }
            }
        }

        #endregion
    }

    /// <summary>
    /// 标题文字位置
    /// </summary>
    public enum ContentPosition
    {
        /// <summary>
        /// 上边靠左
        /// </summary>
        Top_Left = 0,
        /// <summary>
        /// 上边中间
        /// </summary>
        Top_Center,
        /// <summary>
        /// 上边靠右
        /// </summary>
        Top_Right,
        /// <summary>
        /// 左边靠上
        /// </summary>
        Left_Top,
        /// <summary>
        /// 左边中间
        /// </summary>
        Left_Center,
        /// <summary>
        /// 左边靠下
        /// </summary>
        Left_Bottom,
        /// <summary>
        /// 下边靠左
        /// </summary>
        Bottom_Left,
        /// <summary>
        /// 下边中间
        /// </summary>
        Bottom_Center,
        /// <summary>
        /// 下边靠右
        /// </summary>
        Bottom_Right,
        /// <summary>
        /// 右边靠上
        /// </summary>
        Right_Top,
        /// <summary>
        /// 右边中间
        /// </summary>
        Right_Center,
        /// <summary>
        /// 右边靠下
        /// </summary>
        Right_Bottom
    }

猜你喜欢

转载自blog.csdn.net/Iawfy_/article/details/125444624
今日推荐