You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ControlPaint.DrawBorder绘制控件边框时如何设置边框粗细?

解决方案

你当前使用的是ControlPaint.DrawBorder的默认重载,该重载绘制的边框固定为1像素,要自定义边框粗细,只需切换到支持指定边框宽度的重载即可。

修改后的自定义方法代码

private void DrawControlBorder(PaintEventArgs Painteventargs, Control Control, Color Color, int Thickness)
{
    ControlPaint.DrawBorder(Painteventargs.Graphics,
        Control.ClientRectangle,
        // 四个边统一使用传入的颜色、粗细、实线样式
        Color, Thickness, ButtonBorderStyle.Solid,
        Color, Thickness, ButtonBorderStyle.Solid,
        Color, Thickness, ButtonBorderStyle.Solid,
        Color, Thickness, ButtonBorderStyle.Solid);
}

调用示例

在控件的Paint事件中直接调用即可,例如给pictureBox2绘制3像素宽的红色边框:

private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
    DrawControlBorder(e, pictureBox2, Color.Red, 3);
}

补充说明

  • 如果需要为上下左右四个边设置不同的颜色、粗细或样式,分别修改对应位置的参数即可。
  • 如果出现粗边框边缘被控件截断的情况,可以将绘制范围向内缩进1像素,把绘制矩形参数替换为new Rectangle(0, 0, Control.Width - 1, Control.Height - 1)即可。

内容的提问来源于stack exchange,提问作者Daniel Lip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 15:06:06