简体   繁体   English

GroupBox 控件的边框颜色

[英]Border Color of GroupBox Control

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

The windows groupbox doesnt have a border color property, so this means you will have to create a new class inheriting from groupbox and create your own border color property. windows groupbox 没有边框颜色属性,因此这意味着您必须创建一个继承自 groupbox 的新类并创建自己的边框颜色属性。 here is the code you will need;这是您需要的代码;

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

after adding this code to your project click build solution, then MyGroupBox will appear in your toolbox to be able to use将此代码添加到您的项目后单击构建解决方案,然后MyGroupBox将出现在您的工具箱中以便能够使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM