Draw a straight line in C #

Source: Internet
Author: User

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.