【问题标题】:Border color of Group Box in windows applicationwindows应用程序中组框的边框颜色
【发布时间】:2009-09-07 08:28:33
【问题描述】:

如何在windows应用中改变分组框控件的边框颜色

【问题讨论】:

    标签: c# .net windows


    【解决方案1】:

    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将出现在您的工具箱中以便能够使用

    【讨论】:

    • WhatsThePoint,这与我想要的很接近,但我遇到了两个我无法弄清楚的问题:1) 缺少组框名称和 2) 中的所有控件组框也有黑色边框 - 我如何取回名称并在组框本身周围有修改后的边框而不影响其中的控件? (顺便说一句,我发现标签的文本出现了奇怪的加倍,所以我不得不注释掉除 ControPaint 行之外的所有内容)
    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 2012-06-08
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    相关资源
    最近更新 更多