我正在构建一个自定义控件,我需要它只绘制顶部边框.怎么做到呢?
编辑:目前我正在使用此代码:
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); }
当我的自定义控件中没有嵌套控件时,这很有效.一旦我开始添加控件,他们似乎过度绘制边框线.
使用ControlPaint.DrawBorder方法.从本文中围绕任何C#Winform控件绘制边框:以下内容在控件周围添加边框:
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); }