windows应用程序中组框的边框颜色

如何在 Windows 应用程序中更改组框控件的边框颜色

stack overflow Border Color of GroupBox Control
原文答案

答案:

作者头像

windows groupbox 没有边框颜色属性,因此这意味着您必须创建一个继承自 groupbox 的新类并创建自己的边框颜色属性。这是您需要的代码;

    public class MyGroupBox : GroupBox
    {
        private Color _borderColor = Color.Black;

        public Color BorderColor
        {
            get { return this._borderColor; }
            set { this._borderColor = value; }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //get the text size in groupbox
            Size tSize = TextRenderer.MeasureText(this.Text, this.Font);

            Rectangle borderRect = e.ClipRectangle;
            borderRect.Y = (borderRect.Y + (tSize.Height / 2));
            borderRect.Height = (borderRect.Height - (tSize.Height / 2));
            ControlPaint.DrawBorder(e.Graphics, borderRect, this._borderColor, ButtonBorderStyle.Solid);

            Rectangle textRect = e.ClipRectangle;
            textRect.X = (textRect.X + 6);
            textRect.Width = tSize.Width;
            textRect.Height = tSize.Height;
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
        }
    }

将此代码添加到您的项目后单击构建解决方案,然后 MyGroupBox 将出现在您的工具箱中以便能够使用