Before introducing the application example of the C # Draw line control, we need to understand that there is no draw Line Control in C #. Here we have written one for you to share. There are two controls that draw a horizontal line and a vertical line respectively. You can share with us about how to draw a diagonal line.
C # horizontal line of the draw Line Control
Using system;
Using system. collections;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. Windows. forms;
Namespace Jiashi. wincontrols
{
///
/// LinEx
///
Public class LinEx: system. Windows. Forms. usercontrol
{
# Region attribute Definition
Private system. Drawing. Color linecolor;
Private int linewidth;
///
/// Color attribute of the line
///
Public System. Drawing. Color linecolor
{
Set
{
This. linecolor = value;
System. Windows. Forms. painteventargs Ep =
New painteventargs (this. creategraphics (),
This. clientrectangle );
This. linex_paint (this, EP );
}
Get {return this. linecolor ;}
}
///
/// Line width
///
Public int linewidth
{
Set
{
This. linewidth = value;
System. Windows. Forms. painteventargs Ep =
New painteventargs (this. creategraphics (),
This. clientrectangle );
This. linex_paint (this, EP );
}
Get {return this. linewidth ;}
}
# Endregion
Private system. componentmodel. Container components = NULL;
///
/// The initial color and line width of the constructor
///
Public LinEx ()
{
Initializecomponent ();
This. linecolor = This. forecolor;
This. linewidth = 1;
}
///
/// Clear all resources in use.
///
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Code generated by the region component designer
///
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
///
Private void initializecomponent ()
{
//
// LinEx
//
This. Name = "LinEx ";
This. Resize + = new system. eventhandler (this. linex_resize );
This. Paint + =
New system. Windows. Forms. painteventhandler (this. linex_paint );
}
# Endregion
Private void linex_paint (Object sender,
System. Windows. Forms. painteventargs E)
{
Graphics G = E. graphics;
Pen mypen = new pen (this. linecolor );
Mypen. width = This. linewidth * 2;
This. Height = This. linewidth;
G. drawline (mypen, 0, 0, E. cliprectangle. Right, 0 );
}
Private void linex_resize (Object sender, system. eventargs E)
{
This. Height = This. linewidth;
}
}
}
C # vertical line of the draw Line Control
Using system;
Using system. collections;
Using system. componentmodel;
Using system. drawing;
Using system. Data;
Using system. Windows. forms;
Namespace Jiashi. wincontrols
{
///
/// Liney draws a vertical bar Control
///
Public class Liney: system. Windows. Forms. usercontrol
{
# Region attribute Definition
Private system. Drawing. Color linecolor;
Private int linewidth;
///
/// Color attribute of the line
///
Public System. Drawing. Color linecolor
{
Set
{
This. linecolor = value;
System. Windows. Forms. painteventargs Ep =
New painteventargs (this. creategraphics (),
This. clientrectangle );
This. liney_paint (this, EP );
}
Get {return this. linecolor ;}
}
///
/// Line width
///
Public int linewidth
{
Set
{
This. linewidth = value;
System. Windows. Forms. painteventargs Ep =
New painteventargs (this. creategraphics (),
This. clientrectangle );
This. liney_paint (this, EP );
}
Get {return this. linewidth ;}
}
# Endregion
Private system. componentmodel. Container components = NULL;
///
/// The initial color and line width of the constructor
///
Public Liney ()
{
Initializecomponent ();
This. linecolor = This. forecolor;
This. linewidth = 1;
}
///
/// Clear all resources in use.
///
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Code generated by the region component designer
///
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
///
Private void initializecomponent ()
{
//
// Liney
//
This. Name = "Liney ";
This. Resize + =
New system. eventhandler (this. liney_resize );
This. Paint + =
New system. Windows. Forms. painteventhandler (this. liney_paint );
}
# Endregion
Private void liney_paint (
Object sender, system. Windows. Forms. painteventargs E)
{
Graphics G = E. graphics;
Pen mypen = new pen (this. linecolor );
Mypen. width = This. linewidth * 2;
This. width = This. linewidth;
G. drawline (mypen, 0, 0, 0, E. cliprectangle. Bottom );
}
Private void liney_resize (
Object sender, system. eventargs E)
{
This. width = This. linewidth;
}
}
}
The development of the C # Draw line control will introduce you here, hoping to help you understand and learn the C # Draw line control.