前两天发了一个相关的视频,效果不是很好,直接上代码,就不用看那十几分钟的视频啦
public class Line:Panel
{
public enum Direction
{
Vertical,
Horizon
}
private float _line_width = 1;
private Color _line_color = Color.Black;
private Direction _direction = Direction.Horizon;
public Line()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
[Description("设置线宽")]
[Category("自定义")]
public float LineWidth
{
get { return _line_width; }
set
{
_line_width = value <= 0 ? 1 : value;
this.Invalidate();
}
}
[Description("设置线的颜色")]
[Category("自定义")]
public Color LineColor
{
get { return _line_color; }
set
{
this._line_color = value;
this.Invalidate();
}
}
[Description("设置线的方向")]
[Category("自定义")]
public Direction LineDirection
{
get { return _direction; }
set
{
_direction = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rec = this.ClientRectangle;
Point pStart;
Point pEnd ;
switch (_direction)
{
case Direction.Vertical:
int x = rec.Width/ 2;
int xd2 = x;
pStart = new Point(x, rec.Y);
pEnd = new Point(x, rec.Height);
e.Graphics.DrawLine(new Pen(_line_color, _line_width), pStart, pEnd);
break;
case Direction.Horizon:
int y = rec.Height / 2;
int yd2 = y;
pStart = new Point(rec.X, y);
pEnd = new Point(rec.Width, y);
e.Graphics.DrawLine(new Pen(_line_color, _line_width), pStart, pEnd);
break;
default:
break;
}
}
}
运行效果图:

主页有那个很长的视频,有兴趣的也可以看下哦(不建议看,太费时间!)