如何仅在控件上绘制选定的边框

时间:2014-06-27 08:38:44

标签: c# .net winforms

我正在构建一个自定义控件,我只需要绘制顶部边框。怎么做到呢?

修改:目前我正在使用此代码:

protected override void OnPaint(PaintEventArgs e)
{
    if (!this.DesignMode)
    {
        Rectangle bounds = this.ClientRectangle;
        GraphicsPath topEdge = new GraphicsPath();
        topEdge.StartFigure();
        topEdge.AddLine(bounds.X, bounds.Y, bounds.X + bounds.Width, bounds.Y);
        topEdge.CloseFigure();
        e.Graphics.DrawPath(new Pen(SystemColors.ActiveBorder, 1), topEdge);
    }
    base.OnPaint(e);
}

当我的自定义控件中没有嵌套控件时,这很有效。一旦我开始添加控件,它们似乎会过度绘制边框线。

1 个答案:

答案 0 :(得分:5)

使用ControlPaint.DrawBorder Method。在本文Draw a Border around any C# Winform Control中:以下内容在控件周围添加了一个边框:

protected override void OnPaint(PaintEventArgs e)
{
  base.OnPaint(e);
  ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
                            Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
                            Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
                            Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset,
                            Color.Black, BORDER_SIZE, ButtonBorderStyle.Inset);
}