近期项目(C#)中有个关于界面的需求,要给groupbox设置边框颜色,VS中没有找到直接能修改的属性。经过一番查询,找到了答案,在groupbox的Paint事件中修改:
groupBox1.Paint += new PaintEventHandler(FnWidget.setGroupBoxBorder);
//修改GroupBox的边框颜色
public static void setGroupBoxBorder(object sender, PaintEventArgs e)
{
GroupBox gbx = (GroupBox)sender;
e.Graphics.DrawLine(Pens.Gray, 1, 7, 8, 7);
e.Graphics.DrawLine(Pens.Gray, e.Graphics.MeasureString(gbx.Text, gbx.Font).Width + 8, 7, gbx.Width - 2, 7);
e.Graphics.DrawLine(Pens.Gray, 1, 7, 1, gbx.Height - 2);
e.Graphics.DrawLine(Pens.Gray, 1, gbx.Height - 2, gbx.Width - 2, gbx.Height - 2);
e.Graphics.DrawLine(Pens.Gray, gbx.Width - 2, 7, gbx.Width - 2, gbx.Height - 2);
}
效果(设置成灰色):