
程序代码:
using System;
using System.Collections.Generic;
using using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class StraightLine : Control
{
private Enums.StraightLineTypes lineType;
private bool antiAlias;
private int thickness;
protected Pen pen;
public StraightLine()
{
lineType = Enums.StraightLineTypes.水平;
thickness = 1;
antiAlias = true;
this.Paint += StraightLine_Paint;
}
protected override void Dispose(bool disposing)
{
if (disposing && pen != null)
{
pen.Dispose();
}
base.Dispose(disposing);
}
[Category("连线属性"), DefaultValue(typeof(Enums.StraightLineTypes), "StraightLineTypes.Horizontal")]
public Enums.StraightLineTypes LineType
{
get
{
return lineType;
}
set
{
lineType = value;
Invalidate();
}
}
[Category("连线属性"), DefaultValue(1)]
public int Thickness
{
get
{
return thickness;
}
set
{
thickness = value;
Invalidate();
}
}
[Category("连线属性"), DefaultValue(true)]
public bool AntiAlias
{
get
{
return antiAlias;
}
set
{
antiAlias = value;
Invalidate();
}
}
/// <summary>
/// 重写画线部分GDI实现
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void StraightLine_Paint(object sender, PaintEventArgs e)
{
pen = new Pen(ForeColor, Thickness);
if (AntiAlias)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
}
switch (lineType)
{
case Enums.StraightLineTypes.水平:
DrawCenteredHorizontalLine(e.Graphics);
break;
case Enums.StraightLineTypes.垂直:
DrawCenteredVerticalLine(e.Graphics);
break;
case Enums.StraightLineTypes.斜线:
DrawCenteredDiagonalAscendingLine(e.Graphics);
break;
case Enums.StraightLineTypes.反斜线:
DrawCenteredDiagonalDescendingLine(e.Graphics);
break;
default: break;
}
}
#region 画线函数
private void DrawCenteredHorizontalLine(Graphics g)
{
g.DrawLine(pen, 0, Height / 2, Width, Height / 2);
Graphics graphics = this.CreateGraphics();
Point[] mypoints = { new Point(0, Height / 2 + 1), new Point(0, Height / 2 - 1), new Point(Width, Height / 2 - 1), new Point(Width, Height / 2 + 1) };
RegionControl(mypoints);
}
private void DrawCenteredVerticalLine(Graphics g)
{
g.DrawLine(pen, Width / 2, 0, Width / 2, Height);
Point[] mypoints = { new Point(Width / 2 - 1, 0), new Point(Width / 2 + 1, 0), new Point(Width / 2 + 1, Height), new Point(Width / 2 - 1, Height) };
RegionControl(mypoints);
}
private void DrawCenteredDiagonalAscendingLine(Graphics g)
{
g.DrawLine(pen, 0, Height, Width, 0);
Point[] mypoints = { new Point(1, Height), new Point(0, Height - 1), new Point(Width - 1, 0), new Point(Width, 1) };
RegionControl(mypoints);
}
private void DrawCenteredDiagonalDescendingLine(Graphics g)
{
g.DrawLine(pen, 0, 0, Width, Height);
Point[] mypoints = { new Point(0, 1), new Point(1, 0), new Point(Width, Height - 1), new Point(Width - 1, Height) };
RegionControl(mypoints);
}
/// <summary>
/// 按照点集合生成异形窗体
/// </summary>
/// <param name="points"></param>
private void RegionControl(Point[] points)
{
GraphicsPath mygraphicsPath = new GraphicsPath();
mygraphicsPath.AddPolygon(points);
Region myregion = new Region(mygraphicsPath);
this.Region = myregion;
}
#endregion
}
public class Enums
{
public enum StraightLineTypes
{
//Auto,
水平,
垂直,
反斜线,
斜线,
}
}
}