C#Winform修改Panel边框颜色
1.设置Panel控件的边框颜色,先设置的BorderStyle属性为None,在其Paint事件中代码如下:
private void panel1_Paint(object sender, PaintEventArgs e)
{
//方法一:
ControlPaint.DrawBorder(e.Graphics,
this.panel1.ClientRectangle,
Color.LightSeaGreen,//7f9db9
1,
ButtonBorderStyle.Solid,
Color.LightSeaGreen,
1,
ButtonBorderStyle.Solid,
Color.LightSeaGreen,
1,
ButtonBorderStyle.Solid,
Color.LightSeaGreen,
1,
ButtonBorderStyle.Solid);
//方法二:
//Pen pen1 = new Pen(Color.Red, 1);
//pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
//pen1.DashPattern = new float[] { 4f, 2f };
//e.Graphics.DrawRectangle(pen1, 0, 0, this.panel1.Width - 1, this.panel1.Height - 1);
//方法三:
//e.Graphics.DrawLine(new Pen(Color.Red), 0, 0, 0, panel1.Height); //左
//e.Graphics.DrawLine(new Pen(Color.Red), 0, 0, panel1.Width, 0); //上
//e.Graphics.DrawLine(new Pen(Color.Red), 0, panel1.Height - 1, panel1.Width, panel1.Height - 1); //下
//e.Graphics.DrawLine(new Pen(Color.Red), panel1.Width - 1, 0, panel1.Width - 1, panel1.Height); //右
}
2.设置GroupBox控件的颜色:
private void groupBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(groupBox1.BackColor);
e.Graphics.DrawString(groupBox1.Text, groupBox1.Font, Brushes.Red, 10, 1);
e.Graphics.DrawLine(Pens.Red, 1, 7, 8, 7);
e.Graphics.DrawLine(Pens.Red, e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 8, 7, groupBox1.Width - 2, 7);
e.Graphics.DrawLine(Pens.Red, 1, 7, 1, groupBox1.Height - 2);
e.Graphics.DrawLine(Pens.Red, 1, groupBox1.Height - 2, groupBox1.Width - 2, groupBox1.Height - 2);
e.Graphics.DrawLine(Pens.Red, groupBox1.Width - 2, 7, groupBox1.Width - 2, groupBox1.Height - 2);
}
总结:如果需要批量使用,可以先定义成自定义控件在使用;
panel外框重绘:https://blog.csdn.net/iteye_13270/article/details/82006079