C# 自定义控件Chart(可实现多限象显示)
视图展示
十字坐标轴方式
一象限坐标轴方式
二象限坐标轴方式
创建步骤
##创建自定义控件(名字可随意)
给自定义控件添加事件
以上个事件一个就Load(初始化)、背景颜色发生改变(不多说)、控件大小发生改变(支持大小拖动)
添加一个图片控件(pictureBox1)
直接从工具箱中拖到自定义控件的绘制板就OK啦!
记得将pictureBox1.Dock=Fill;
代码部分
下面有点儿长我直接拷贝整个代码的(有些同学控件名字不一样要修改哦)
注意长篇出现 ↓↓↓↓↓↓↓↓↓↓
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Char控件使用
{
public partial class New_Chart : UserControl
{
//private int 上边距 = 20, 下边距 = 35;
//private int 左边距 = 50, 右边距 = 20;
//private int X轴格数 = 6, Y轴格数 = 6;
//private double X_Min=-50, X_Max=50,Y_Min=-50,Y_Max=50;
private int 线数量 = 1;
private float X_0点位置, Y_0点位置, X_轴长, Y_轴长;
private Graphics g;
private Bitmap bmp;
public DrLine[] dr_line;
#region
[Description("当前颜色值"), DefaultValue(0), Category("Data")]
public int 上边距 { get; set; } = 20;
public int 下边距 { get; set; } = 35;
public int 左边距 { get; set; } = 50;
public int 右边距 { get; set; } = 20;
public int X轴格数 { get; set; } = 6;
public int Y轴格数 { get; set; } = 6;
public double X_Min { get; set; } = -50;
public double X_Max { get; set; } = 50;
public double Y_Min { get; set; } = -50;
public double Y_Max { get; set; } = 50;
#endregion
#region 绘制线结构体
/// <summary>
/// 绘制线的结构体
/// </summary>
public struct DrLine
{
public Pen pen ;
public float X_上次;
public float Y_上次;
public float X_Value;
public float Y_Value;
};
#endregion
#region 线条对象数量
/// <summary>
/// 线条对象数量
/// </summary>
public int Dr_Line_Num
{
get { return 线数量; }
set
{
线数量 = value;
dr_line = new DrLine[线数量];
SetLineZero();
}
}
#endregion
#region 设置线的0点位置
/// <summary>
/// 对创建的线设置0点位置
/// </summary>
void SetLineZero()
{
for (int i = 0; i < dr_line.Length; i++)
{
dr_line[i].X_上次 = X_0点位置;
dr_line[i].Y_上次 = Y_0点位置;
if(dr_line[i].pen==null)
dr_line[i].pen = new Pen(Brushes.Yellow, 1F);
}
}
#endregion
#region 控件触发事件
public New_Chart()
{
InitializeComponent();
dr_line = new DrLine[1];
}
private void New_Chart_SizeChanged(object sender, EventArgs e)
{
Clear_Dr();
}
private void New_Chart_Load(object sender, EventArgs e)
{
Clear_Dr();
}
private void New_Chart_BackColorChanged(object sender, EventArgs e)
{
pictureBox1.BackColor = this.BackColor;
Clear_Dr();
}
#endregion
#region 绘制坐标函数
private void Draw_Border()
{
X_轴长 = pictureBox1.Width - (左边距 + 右边距);
Y_轴长 = pictureBox1.Height - (上边距 + 下边距);
X_0点位置 = 左边距 + (float)((float)(X_轴长/(X_Max-X_Min))*(0-X_Min));
Y_0点位置 = pictureBox1.Height - 下边距 + (float)((float)(Y_轴长 / (Y_Max - Y_Min)) * Y_Min);
float X格长 = (float)(X_轴长 / X轴格数);
float Y格长 = (float)(Y_轴长 / Y轴格数);
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
g = Graphics.FromImage(bmp);
Pen pen = new Pen(Brushes.White, 2F);
//绘制边框
g.DrawLine(pen, 左边距, 上边距, 左边距, pictureBox1.Height - 下边距); // 左边|
g.DrawLine(pen, 左边距, 上边距, pictureBox1.Width - 右边距, 上边距);
g.DrawLine(pen, 左边距, pictureBox1.Height - 下边距, pictureBox1.Width - 右边距, pictureBox1.Height - 下边距);
g.DrawLine(pen, pictureBox1.Width - 右边距, 上边距, pictureBox1.Width - 右边距, pictureBox1.Height - 下边距); // 右边|
//绘制十字中心
g.DrawLine(pen, 左边距, Y_0点位置, pictureBox1.Width - 右边距, Y_0点位置);
g.DrawLine(pen, X_0点位置, 上边距, X_0点位置, pictureBox1.Height - 下边距);
pen = new Pen(Brushes.Green, 1F);
pen.DashStyle = DashStyle.Dash; //虚线
/* 绘制X轴刻度 */
// g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //字体为白色是要使用这个
for (int i = 0; i <= X轴格数; i++)
{
if(i!=0 && i != (X轴格数))
g.DrawLine(pen,(float) (左边距 + X格长*i), 上边距, (float)(左边距 + X格长 * i), pictureBox1.Height - 下边距);
g.DrawString(((float)((X_Max - X_Min) / X轴格数) * i + X_Min).ToString("0.0"), new Font("宋体", 8), Brushes.White, (float)(左边距 + X格长 * i)-15, pictureBox1.Height - 下边距 +5);
}
//绘制Y轴刻度
for (int i = 0; i <= Y轴格数; i++)
{
if (i!=0 && i != (Y轴格数))
g.DrawLine(pen,左边距, (float)(上边距 + Y格长 * i), pictureBox1.Width - 右边距, (float)(上边距 + Y格长 * i));
g.DrawString(((float)((Y_Max - Y_Min) / Y轴格数) * i + Y_Min).ToString("0.0"), new Font("宋体", 8), Brushes.White, 10, pictureBox1.Height-下边距 - (float)(Y格长 * i)-10);
}
SetLineZero();
pictureBox1.Image = bmp;
}
#endregion
#region 绘制数据线函数
/// <summary>
/// 绘制线函数
/// </summary>
/// <param name="this_drline">绘制线结构体</param>
public void Write_Dr_Data(ref DrLine this_drline)
{
float X = X_0点位置 + ((X_轴长 / (float)(X_Max - Y_Min)) * this_drline.X_Value);
float Y = Y_0点位置 - ((Y_轴长 / (float)(Y_Max - Y_Min)) * this_drline.Y_Value);
// g.SmoothingMode = SmoothingMode.HighQuality; //抗锯齿华设置
g.DrawLine(this_drline.pen, this_drline.X_上次, this_drline.Y_上次, X, Y);
this_drline.X_上次 = X;
this_drline.Y_上次 = Y;
pictureBox1.Image = bmp;
}
#endregion
#region 清除绘制的图形
/// <summary>
/// 清除绘制的图形
/// </summary>
public void Clear_Dr()
{
if (g != null)
{
g.Clear(Color.Black);
g.Dispose();
}
pictureBox1.Refresh();
Draw_Border();
}
#endregion
}
}
以上就是控件全部咯,小菜鸟一只如果有什么不对了,可以评论区指正,我会及时修改哦!!