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

时间:2009-09-07 08:28:33

标签: c# .net windows

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

1 个答案:

答案 0 :(得分:0)

Windows组合框没有边框颜色属性,因此这意味着您必须创建一个继承自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将出现在您的工具箱中以便能够使用