猪头少年 - 云南AI专家 - 云南独立开发者

Engineering note

Winform中设置控件边框

本文将会介绍在Winform中如何设置控件的边框,可应用于Form和Panel等。

Panel 控件为例,注册控件的 Paint 事件,使用 DrawBorder 方法来绘制边框。该方法有多个重载,其中最常用的是统一设置边框和分别为四条边单独设置边框。

统一设置边框

使用 ButtonBorderStyle.Solid 枚举项可以直接设置边框

private void panel1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, ClientRectangle, 
           Color.Ivory, ButtonBorderStyle.Solid);
}

单独设置边框

可以为上下左右四条边单独设置边框大小和颜色,这样可以实现四条边不相同的效果。

private void panel1_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, panel1.ClientRectangle,
           Color.White, 1, ButtonBorderStyle.Solid, //左边
           Color.White, 1, ButtonBorderStyle.Solid, //上边
           Color.DimGray, 1, ButtonBorderStyle.Solid, //右边
           Color.DimGray, 1, ButtonBorderStyle.Solid);//底边
}
主专题 尚未归入核心专题

这篇文章仍可通过文章索引访问,但不会被重复塞进多个主专题。

选择一条专题路线

Next step

不要停在单篇文章。

沿主专题继续阅读,查看同一问题从概念到实战的完整路径。

选择一条专题路线