-
在 Paint
事件中使用ControlPaint
来绘制边框,这个在之前写过,也是我目前使用的较多的方式。
private void panel1_Paint(object sender, PaintEventArgs e)
{
//上边框
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
Color.White, 0, ButtonBorderStyle.Solid, //左边
Color.White, 1, ButtonBorderStyle.Solid, //上边
Color.White, 0, ButtonBorderStyle.Solid, //右边
Color.White, 0, ButtonBorderStyle.Solid);//底边
}
-
也是在 Paint
事件中进行绘制,不过是使用DrawLine(s)
或者DrawRectangle
来绘制
private void panel2_Paint(object sender, PaintEventArgs e)
{
//全边框
//e.Graphics.DrawRectangle(new Pen(Color.White,1), panel2.ClientRectangle.X+1, panel2.ClientRectangle.Y+1, panel2.ClientRectangle.Width-2, panel2.ClientRectangle.Height-2);
//下边框
e.Graphics.DrawLine(new Pen(Color.White, 1), panel2.ClientRectangle.X, panel2.ClientRectangle.Y + panel2.ClientRectangle.Height-1, panel2.ClientRectangle.X + panel2.ClientRectangle.Width, panel2.ClientRectangle.Y + panel2.ClientRectangle.Height-1);
}
-
使用控件+Dock属性的方式,即将一个Panel或者其他控件的宽度或者高度设置为1,需要设置到哪个边框设置对应的Dock属性
private void DrawBorderLeft()
{
//左边框
Panel panel = new Panel();
panel.Dock= DockStyle.Left;
panel.BackColor = Color.White;
panel.BorderStyle = BorderStyle.None;
panel.Width = 1;
panel3.Controls.Add(panel);
}
-
使用 Padding
属性,即大控件套小控件,小控件设置Dock=Fill
,会根据设置的Padding
有一个间隔,这个方式用来做自定义窗体(一般会把原始边框去掉)的边框不错
private void DrawBorderRight()
{
//右边框
panel4.BackColor = Color.White;
panel4.Padding = new Padding(0,0,1,0);
Panel panel = new Panel();
panel.Dock = DockStyle.Fill;
panel.BackColor = Color.Black;
panel.BorderStyle = BorderStyle.None;
panel.Width = 1;
panel4.Controls.Add(panel);
}
实现效果: