Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Drawing;
Using System. ComponentModel;
Using System. Windows. Forms;
Namespace Line
{
Public class LineControl: Control
{
Private LineType m_lineType = LineType. Horizontal;
Private Color m_lineColor = Color. Blue;
Private Color m_shadowColor = Color. Gray;
Private int m_lineWidth = 2;
Private Size m_shadowOffset = new Size (1, 1 );
Public LineControl ()
{
This. SetStyle (ControlStyles. ResizeRedraw | ControlStyles. UserPaint | ControlStyles. SupportsTransparentBackColor, true );
This. SetStyle (ControlStyles. UserMouse |
ControlStyles. Selectable |
ControlStyles. StandardClick |
ControlStyles. StandardDoubleClick, false );
}
[Category ("")]
[Description ("shadow offset")]
[DefaultValue (typeof (Size), "1, 1")]
Public Size ShadowOffset
{
Get {return m_shadowOffset ;}
Set
{
M_shadowOffset = value;
This. SetSize ();
This. Invalidate ();
}
}
[Category ("")]
[Description ("width")]
[DefaultValue (2)]
Public int LineWidth
{
Get {return m_lineWidth ;}
Set
{
If (value <1) value = 1;
M_lineWidth = value;
This. SetSize ();
This. Invalidate ();
}
}
[Category ("")]
[Description ("type")]
[DefaultValue (typeof (LineType), "Horizontal")]
Public LineType
{
Get {return m_lineType ;}
Set
{
If (m_lineType! = Value)
{
Int linelen = 0;
If (m_lineType = LineType. Horizontal)
{
Linelen = this. Width;
}
Else
{
Linelen = this. Height;
}
M_lineType = value;
If (value = LineType. Horizontal)
{
This. Width = linelen;
}
Else
{
This. Height = linelen;
}
This. SetSize ();
This. Invalidate ();
}
}
}
[Category ("")]
[Description ("color")]
[DefaultValue (typeof (Color), "Blue")]
Public Color LineColor
{
Get {return m_lineColor ;}
Set
{
M_lineColor = value;
This. Invalidate ();
}
}
[Category ("")]
[Description ("shadow color")]
[DefaultValue (typeof (Color), "Gray")]
Public Color ShadowColor
{
Get {return m_shadowColor ;}
Set
{
M_shadowColor = value;
This. Invalidate ();
}
}
Protected override void OnPaint (PaintEventArgs e)
{
Pen p1 = new Pen (this. LineColor, this. LineWidth );
Pen p2 = new Pen (this. ShadowColor, this. LineWidth );
If (this. LineType = LineType. Horizontal)
{
E. Graphics. DrawLine (p2, new Point (ShadowOffset. Width, ShadowOffset. Height), new Point (this. Right, ShadowOffset. Height ));
E. Graphics. DrawLine (p1, Point. Empty, new Point (this. Right-ShadowOffset. Width, 0 ));
}
Else
{
E. Graphics. DrawLine (p2, new Point (ShadowOffset. Width, ShadowOffset. Height), new Point (ShadowOffset. Width, this. Bottom ));
E. Graphics. DrawLine (p1, Point. Empty, new Point (0, this. Bottom-ShadowOffset. Height ));
}
P1.Dispose ();
P2.Dispose ();
}
Protected override void OnSizeChanged (EventArgs e)
{
Base. OnSizeChanged (e );
SetSize ();
}
Private void SetSize ()
{
If (this. LineType = LineType. Horizontal)
{
This. Height = LineWidth + ShadowOffset. Height;
}
Else
{
This. Width = LineWidth + ShadowOffset. Width;
}
}
}
/// <Summary>
/// Line type
/// </Summary>
Public enum LineType
{
/// <Summary>
/// Horizontal line
/// </Summary>
Horizontal,
/// <Summary>
/// Vertical bars
/// </Summary>
Vertical
}
}
From Allen Chen's column