类图
1) 具有渐变色的Label控件MyLabel:具有渐变色的标签
2) 实现坐标系控件 MyCoordinate
代码
1) MyLabel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;
namespace WindowsFormsControlLibrary
{
/// <summary>
/// 自定义Label
/// </summary>
/// <remarks>
/// 渐变背景的Label
/// </remarks>
public class MyLabel:Control
{
/// <summary>
/// 背景渐变颜色2
/// </summary>
private Color backColor2;
/// <summary>
/// 背景渐变颜色2
/// </summary>
public Color BackColor2
{
get
{
return backColor2;
}
set
{
//重绘控件
this.Invalidate();
backColor2 = value;
}
}
public override string Text
{
get
{
return base.Text;
}
set
{
//重绘控件
this.Invalidate();
base.Text = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
LinearGradientBrush currentBrush = new LinearGradientBrush
(
new Point(0, 0),
new Point(0, Height),
BackColor, backColor2
);
e.Graphics.FillRectangle(currentBrush, ClientRectangle);
//测量字体所占的矩形大小
SizeF textSize = e.Graphics.MeasureString(Text, Font);
Brush foreBrush = new SolidBrush(ForeColor);
e.Graphics.DrawString(Text, Font, foreBrush,
new PointF(
(Width - textSize.Width) / 2,
(Height - textSize.Height) / 2
)
);
currentBrush.Dispose();
foreBrush.Dispose();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
}
2) MyCoordinate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Drawing.Drawing2D;
namespace WindowsFormsControlLibrary
{
public class MyCoordinate:Control
{
//坐标轴X与控件边缘的距离
private const int xPad = 80;
//坐标轴Y与控件边缘的距离
private const int yPad = 60;
/// <summary>
/// X坐标标签间距
/// </summary>
private int coordinateLablePadX = 20;
public int CoordinateLablePadX
{
get
{
return coordinateLablePadX;
}
set
{
Invalidate();
coordinateLablePadX = value;
}
}
/// <summary>
/// Y坐标标签间距
/// </summary>
private int coordinateLablePadY = 30;
public int CoordinateLablePadY
{
get
{
return coordinateLablePadY;
}
set
{
Invalidate();
coordinateLablePadY = value;
}
}
/// <summary>
/// y坐标轴起点坐标
/// </summary>
public Point YCoordinateStartPoint
{
get
{
return new Point(xPad, this.Height - yPad);
}
}
/// <summary>
/// y坐标轴终点坐标
/// </summary>
public Point YCoordinateEndPoint
{
get
{
return new Point(xPad, yPad);
}
}
/// <summary>
/// x坐标轴起点坐标
/// </summary>
public Point XCoordinateStartPoint
{
get
{
return YCoordinateStartPoint;
}
}
/// <summary>
/// x坐标轴终点坐标
/// </summary>
public Point XCoordinateEndPoint
{
get
{
Point current = new Point();
current.X = Width-xPad ;
current.Y = Height - yPad ;
return current;
}
}
/// <summary>
/// 当前鼠标的位置坐标
/// </summary>
private Point currentMouseLocation = new Point();
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyCoordinate
//
this.ResumeLayout(false);
}
/// <summary>
/// 重载绘制事件
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen currentPen=new Pen(Color.Black ) ;
currentPen.EndCap = LineCap.ArrowAnchor ;
//绘制Y轴
e.Graphics.DrawLine(
currentPen,
YCoordinateStartPoint,
YCoordinateEndPoint
);
//绘制Y轴
e.Graphics.DrawLine(
currentPen,
XCoordinateStartPoint,
XCoordinateEndPoint
);
currentPen.Dispose();
//绘制X,Y 标签
DrawCoordinateLabel(e.Graphics, YCoordinateStartPoint, YCoordinateEndPoint,XCoordinateStartPoint, XCoordinateEndPoint);
}