在事件Paint中写如下代码:
private void groupBox1_Paint(object sender, PaintEventArgs e)
{
GroupBox gb = (GroupBox)sender;
e.Graphics.Clear(gb.BackColor);
//标题字颜色
e.Graphics.DrawString(gb.Text,gb.Font,Brushes.Red,10,1);
var vSize = e.Graphics.MeasureString(gb.Text,gb.Font);
//标题字左边短横线
e.Graphics.DrawLine(Pens.Red,1,vSize.Height/2,8,vSize.Height/2);
//标题字右边横线
e.Graphics.DrawLine(Pens.Red, vSize.Width + 8, vSize.Height / 2, gb.Width - 2, vSize.Height / 2);
//自定义颜色
Pen pen = new Pen(Color.FromArgb(100, 100, 100));
//Group左边竖线
e.Graphics.DrawLine(pen, 1, vSize.Height / 2,1, gb.Width - 2);
//Group右边竖线
e.Graphics.DrawLine(pen, gb.Width - 2, vSize.Height / 2, gb.Width - 2, gb.Height - 2);
//Group底边横线
e.Graphics.DrawLine(pen, 1, gb.Height- 2, gb.Width - 2, gb.Height -2);
}
效果:

网友评论