首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何更改分组框上边框的颜色?

如何更改分组框上边框的颜色?
EN

Stack Overflow用户
提问于 2008-09-16 20:16:03
回答 6查看 100.2K关注 0票数 34

在C#.NET中,我尝试以编程方式更改分组框中边框的颜色。

更新:在我们切换到.NET之前,我在winforms系统上工作时问过这个问题。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-04-29 10:12:53

在前一个答案的基础上,提出了一个更好的解决方案,其中包括分组框的标签:

代码语言:javascript
复制
groupBox1.Paint += PaintBorderlessGroupBox;

private void PaintBorderlessGroupBox(object sender, PaintEventArgs p)
{
  GroupBox box = (GroupBox)sender;
  p.Graphics.Clear(SystemColors.Control);
  p.Graphics.DrawString(box.Text, box.Font, Brushes.Black, 0, 0);
}

您可能想要调整文本的x/y,但对于我的使用来说,这恰到好处。

票数 24
EN

Stack Overflow用户

发布于 2013-11-18 15:10:51

只需添加paint事件。

代码语言:javascript
复制
    private void groupBox1_Paint(object sender, PaintEventArgs e)
    {
        GroupBox box = sender as GroupBox;
        DrawGroupBox(box, e.Graphics, Color.Red, Color.Blue);
    }


    private void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor)
    {
        if (box != null)
        {
            Brush textBrush = new SolidBrush(textColor);
            Brush borderBrush = new SolidBrush(borderColor);
            Pen borderPen = new Pen(borderBrush);
            SizeF strSize = g.MeasureString(box.Text, box.Font);
            Rectangle rect = new Rectangle(box.ClientRectangle.X,
                                           box.ClientRectangle.Y + (int)(strSize.Height / 2),
                                           box.ClientRectangle.Width - 1,
                                           box.ClientRectangle.Height - (int)(strSize.Height / 2) - 1);

            // Clear text and border
            g.Clear(this.BackColor);

            // Draw text
            g.DrawString(box.Text, box.Font, textBrush, box.Padding.Left, 0);

            // Drawing Border
            //Left
            g.DrawLine(borderPen, rect.Location, new Point(rect.X, rect.Y + rect.Height));
            //Right
            g.DrawLine(borderPen, new Point(rect.X + rect.Width, rect.Y), new Point(rect.X + rect.Width, rect.Y + rect.Height));
            //Bottom
            g.DrawLine(borderPen, new Point(rect.X, rect.Y + rect.Height), new Point(rect.X + rect.Width, rect.Y + rect.Height));
            //Top1
            g.DrawLine(borderPen, new Point(rect.X, rect.Y), new Point(rect.X + box.Padding.Left, rect.Y));
            //Top2
            g.DrawLine(borderPen, new Point(rect.X + box.Padding.Left + (int)(strSize.Width), rect.Y), new Point(rect.X + rect.Width, rect.Y));
        }
    }
票数 42
EN

Stack Overflow用户

发布于 2012-12-01 05:32:06

只需将任何对象(不仅仅是按钮)上的绘制操作设置为此方法即可绘制边框。

代码语言:javascript
复制
    private void UserControl1_Paint(object sender, PaintEventArgs e)
    {
        ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);

    }

它仍然不会像原来的那样漂亮和圆润,但它要简单得多。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/76455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档