转帖 .Net(C#)纯GDI+绘制实时动态曲线图之一 (曲线控件全部源码)

代码编写:方志洪
E-mail:zhihongf#qq.com (请将 #   改为 @)
Blog:http://blog.intodream.com

 

 

 

============(由于我的空间对文章字数有限制,所以该代码分两篇公布2-1)============

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace curve
{
/// <summary>
/// 实时动态曲线图
///
/// 代码编写:方志洪
/// 编写时间:2007年2月7日
/// E-Mail:zhihongf#qq.com(请将 # 改为 @)
/// BLOG:http://blog.intodream.com
/// </summary>
public class CurveControl : System.Windows.Forms.UserControl
{
   private System.Windows.Forms.PictureBox picCurveShow;
   private System.Windows.Forms.Label labShowView;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;

   public CurveControl()
   {
    // 该调用是 Windows.Forms 窗体设计器所必需的。
    InitializeComponent();

    // TODO: 在 InitializeComponent 调用后添加任何初始化

   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if(components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }

   #region 组件设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器
   /// 修改此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.picCurveShow = new System.Windows.Forms.PictureBox();
    this.labShowView = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // picCurveShow
    //
    this.picCurveShow.BackColor = System.Drawing.Color.Black;
    this.picCurveShow.Dock = System.Windows.Forms.DockStyle.Fill;
    this.picCurveShow.Location = new System.Drawing.Point(0, 0);
    this.picCurveShow.Name = "picCurveShow";
    this.picCurveShow.Size = new System.Drawing.Size(384, 264);
    this.picCurveShow.TabIndex = 0;
    this.picCurveShow.TabStop = false;
    this.picCurveShow.Resize += new System.EventHandler(this.picCurveShow_Resize);
    this.picCurveShow.DoubleClick += new System.EventHandler(this.picCurveShow_DoubleClick);
    this.picCurveShow.MouseMove += new System.Windows.Forms.MouseEventHandler(this.picCurveShow_MouseMove);
    this.picCurveShow.MouseLeave += new System.EventHandler(this.picCurveShow_MouseLeave);
    //
    // labShowView
    //
    this.labShowView.AutoSize = true;
    this.labShowView.BackColor = System.Drawing.Color.Black;
    this.labShowView.ForeColor = System.Drawing.Color.Red;
    this.labShowView.Location = new System.Drawing.Point(192, 96);
    this.labShowView.Name = "labShowView";
    this.labShowView.Size = new System.Drawing.Size(42, 17);
    this.labShowView.TabIndex = 1;
    this.labShowView.Text = "label1";
    this.labShowView.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    this.labShowView.Visible = false;
    //
    // CurveControl
    //
    this.Controls.Add(this.labShowView);
    this.Controls.Add(this.picCurveShow);
    this.Name = "CurveControl";
    this.Size = new System.Drawing.Size(384, 264);
    this.Load += new System.EventHandler(this.CurveControl_Load);
    this.ResumeLayout(false);

   }
   #endregion

   #region 曲线数据定义

   /// <summary>
   /// 定义曲线窗体标题
   /// </summary>
   private string title = "方志洪曲线代码测试DEMO";

   /// <summary>
   /// 定义曲线标题颜色
   /// </summary>
   private Color titleColor = Color.Yellow;

   /// <summary>
   /// 定义是否显示系统时间
   /// </summary>
   private bool showTime = true;

   /// <summary>
   /// 定义显示系统时间颜色
   /// </summary>
   private Color showTimeColor = Color.Red;

   /// <summary>
   /// 定义坐标零点向右偏移量
   /// </summary>
   private float coordinate = 50F;

   /// <summary>
   /// 定义曲线Y轴最大值
   /// </summary>
   private float yMaxValue = 100F;

   /// <summary>
   /// 定义曲线Y轴最大值提示文字
   /// </summary>
   private string yMaxString = "最大值:";

   /// <summary>
   /// 定义曲线Y轴最小值
   /// </summary>
   private float yMinValue = 0F;

   /// <summary>
   /// 定义曲线Y轴最小值提示文字
   /// </summary>
   private string yMinString = "最小值:";

   /// <summary>
   /// 定义曲线Y轴正常值(上限)
   /// </summary>
   private float yUpper = 70F;

   /// <summary>
   /// 定义曲线Y轴正常值(上限)提示文本
   /// </summary>
   private string yUpperString = "上限:";

   /// <summary>
   /// 定义曲线Y轴正常值(下限)
   /// </summary>
   private float yLower = 20F;

   /// <summary>
   /// 定义曲线Y轴正常值(下限)提示文本
   /// </summary>
   private string yLowerString = "下限:";

   /// <summary>
   /// 定义曲线正常值上下限线段颜色
   /// </summary>
   private Color yUpperAndLowerColor = Color.Lime;

   /// <summary>
   /// 定义曲线正常值上下限线段宽度
   /// </summary>
   private float yUpperAndLowerPenWidth = 1F;

   /// <summary>
   /// 定义背景颜色
   /// </summary>
   private Color backGroundColor = Color.Black;

   /// <summary>
   /// 定义是否滚动网格线
   /// </summary>
   private bool removeGrid = true;

   /// <summary>
   /// 定义背景网格线颜色
   /// </summary>
   private Color gridColor = Color.DarkGreen;

   /// <summary>
   /// 定义背景网格文字颜色
   /// </summary>
   private Color gridForeColor = Color.Yellow;

   /// <summary>
   /// 定义背景网格(分隔线)宽度
   /// </summary>
   private float gridCompart = 1F;

   /// <summary>
   /// 定义背景网格文字大小
   /// </summary>
   private float gridFontSize = 9;

   /// <summary>
   /// 定义背景网格线画笔宽度
   /// </summary>
   private float gridPenWidth = 1F;

   /// <summary>
   /// 定义背景网格线单元格宽度
   /// </summary>
   private float gridWidth = 10F;

   /// <summary>
   /// 定义背景网格线单元格高度
   /// </summary>
   private float gridHeight = 30F;

   /// <summary>
   /// 定义曲线颜色
   /// </summary>
   private Color curveColor = Color.White;

   /// <summary>
   /// 定义曲线画笔宽度
   /// </summary>
   private float curvePenWidth = 1;

   /// <summary>
   /// 定义曲线移动距离
   /// </summary>
   private int curveRemove = 1;

   /// <summary>
   /// 定义数值节点正方形宽度
   /// </summary>
   private float rectangleWidth = 1F;

   /// <summary>
   /// 定义正方形颜色
   /// </summary>
   private Color rectangleColor = Color.White;
  
   /// <summary>
   /// 定义显示节点数值鼠标X,Y轴容差精度
   /// </summary>
   private float xYPrecision = 4F;

   /// <summary>
   /// 曲线节点数据最大存储量
   /// </summary>
   private int maxNote = 1000;
   #endregion

   #region 公共属性
   /// <summary>
   /// 定义曲线窗体标题
   /// </summary>
   public string CarveTitle
   {
    get
    {
     return this.title;
    }
    set
    {
     this.title = value;
    }
   }

   /// <summary>
   /// 定义标题颜色
   /// </summary>
   public Color CarveTitleColor
   {
    get
    {
     return this.titleColor;
    }
    set
    {
     this.titleColor = value;
    }
   }

   /// <summary>
   /// 定义是否显示系统时间
   /// </summary>
   public bool CarveShowTime
   {
    get
    {
     return this.showTime;
    }
    set
    {
     this.showTime = value;
    }
   }

   /// <summary>
   /// 定义显示系统时间颜色
   /// </summary>
   public Color CarveShowTimeColor
   {
    get
    {
     return this.showTimeColor;
    }
    set
    {
     this.showTimeColor = value;
    }
   }

   /// <summary>
   /// 定义坐标零点向右偏移量
   /// </summary>
   public float CarveCoordinate
   {
    get
    {
     return this.coordinate;
    }
    set
    {
     this.coordinate = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴最大值
   /// </summary>
   public float CarveYMaxValue
   {
    get
    {
     return this.yMaxValue;
    }
    set
    {
     this.yMaxValue = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴最大值提示文字
   /// </summary>
   public string CarveYMaxString
   {
    get
    {
     return this.yMaxString;
    }
    set
    {
     this.yMaxString = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴最小值
   /// </summary>
   public float CarveYMinValue
   {
    get
    {
     return this.yMinValue;
    }
    set
    {
     this.yMinValue = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴最小值提示文字
   /// </summary>
   public string CarveYMinString
   {
    get
    {
     return this.yMinString;
    }
    set
    {
     this.yMinString = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴正常值(上限)
   /// </summary>
   public float CarveYUpper
   {
    get
    {
     return this.yUpper;
    }
    set
    {
     this.yUpper = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴正常值(上限)提示文本
   /// </summary>
   public string CarveYUpperString
   {
    get
    {
     return this.yUpperString;
    }
    set
    {
     this.yUpperString = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴正常值(下限)
   /// </summary>
   public float CarveYLower
   {
    get
    {
     return this.yLower;
    }
    set
    {
     this.yLower = value;
    }
   }

   /// <summary>
   /// 定义曲线Y轴正常值(下限)提示文本
   /// </summary>
   public string CarveYLowerString
   {
    get
    {
     return this.yLowerString;
    }
    set
    {
     this.yLowerString = value;
    }
   }

   /// <summary>
   /// 定义曲线正常值上下限线段颜色
   /// </summary>
   public Color CarveYUpperAndLowerColor
   {
    get
    {
     return this.yUpperAndLowerColor;
    }
    set
    {
     this.yUpperAndLowerColor = value;
    }
   }

   /// <summary>
   /// 定义曲线正常值上下限线段宽度
   /// </summary>
   public float CarveYUpperAndLowerPenWidth
   {
    get
    {
     return this.yUpperAndLowerPenWidth;
    }
    set
    {
     this.yUpperAndLowerPenWidth = value;
    }
   }

   /// <summary>
   /// 定义背景颜色
   /// </summary>
   public Color CarveBackGroundColor
   {
    get
    {
     return this.backGroundColor;
    }
    set
    {
     this.backGroundColor = value;
    }
   }

   /// <summary>
   /// 定义是否滚动网格线
   /// </summary>
   public bool CarveRemoveGrid
   {
    get
    {
     return this.removeGrid;
    }
    set
    {
     this.removeGrid = value;
    }
   }

   /// <summary>
   /// 定义背景网格线颜色
   /// </summary>
   public Color CarveGridColor
   {
    get
    {
     return this.gridColor;
    }
    set
    {
     this.gridColor = value;
    }
   }

   /// <summary>
   /// 定义背景网格文字颜色
   /// </summary>
   public Color CarveGridForeColor
   {
    get
    {
     return this.gridForeColor;
    }
    set
    {
     this.gridForeColor = value;
    }
   }

   /// <summary>
   /// 定义背景网格(分隔线)宽度
   /// </summary>
   public float CarveGridCompart
   {
    get
    {
     return this.gridCompart;
    }
    set
    {
     this.gridCompart = value;
    }
   }

   /// <summary>
   /// 定义背景网格文字大小
   /// </summary>
   public float CarveGridFontSize
   {
    get
    {
     return this.gridFontSize;
    }
    set
    {
     this.gridFontSize = value;
    }
   }

   /// <summary>
   /// 定义背景网格线画笔宽度
   /// </summary>
   public float CarveGridPenWidth
   {
    get
    {
     return this.gridPenWidth;
    }
    set
    {
     this.gridPenWidth = value;
    }
   }

   /// <summary>
   /// 定义背景网格线单元格宽度
   /// </summary>
   public float CarveGridWidth
   {
    get
    {
     return this.gridWidth;
    }
    set
    {
     this.gridWidth = value;
    }
   }

   /// <summary>
   /// 定义背景网格线单元格高度
   /// </summary>
   public float CarveGridHeight
   {
    get
    {
     return this.gridHeight;
    }
    set
    {
     this.gridHeight = value;
    }
   }

   /// <summary>
   /// 定义曲线颜色
   /// </summary>
   public Color CarveCurveColor
   {
    get
    {
     return this.curveColor;
    }
    set
    {
     this.curveColor = value;
    }
   }

   /// <summary>
   /// 定义曲线画笔宽度
   /// </summary>
   public float CarveCurvePenWidth
   {
    get
    {
     return this.curvePenWidth;
    }
    set
    {
     this.curvePenWidth = value;
    }
   }

   /// <summary>
   /// 定义曲线移动距离
   /// </summary>
   public int CarveCurveRemove
   {
    get
    {
     return this.curveRemove;
    }
    set
    {
     this.curveRemove = value;
    }
   }

   /// <summary>
   /// 定义数值节点正方形宽度
   /// </summary>
   public float CarveRectangleWidth
   {
    get
    {
     return this.rectangleWidth;
    }
    set
    {
     this.rectangleWidth = value;
    }
   }

   /// <summary>
   /// 定义正方形颜色
   /// </summary>
   public Color CarveRectangleColor
   {
    get
    {
     return this.rectangleColor;
    }
    set
    {
     this.rectangleColor = value;
    }
   }
  
   /// <summary>
   /// 定义显示节点数值鼠标X,Y轴容差精度
   /// </summary>
   public float CarveXYPrecision
   {
    get
    {
     return this.xYPrecision;
    }
    set
    {
     this.xYPrecision = value;
    }
   }

   /// <summary>
   /// 曲线节点数据最大存储量
   /// </summary>
   public int CarveMaxNote
   {
    get
    {
     return this.maxNote;
    }
    set
    {
     this.maxNote = value;
    }
   }
   #endregion

   #region 全局变量

   /// <summary>
   /// 背景方格移动量
   /// </summary>
   private float gridRemoveX = 1;

   /// <summary>
   /// 鼠标X,Y 坐标值,及该点坐标记录值、记录时间(数组)
   /// </summary>
   private CoordinatesValue[] noteMessages;

   /// <summary>
   /// 当前鼠标 X,Y坐标记录数组下标值
   /// </summary>
   private int noteNow = 0;

   /// <summary>
   /// 系统上次读取的值
   /// </summary>
   private float lastTimeValue =0F;

   /// <summary>
   /// 系统窗体高度临时值,用于窗体变形时刷新数组坐标。
   /// </summary>
   private int lastTimeSystemWindowHeight = 0;

   /// <summary>
   /// 系统窗体宽度临时值,用于窗体变形时刷新数组坐标。
   /// </summary>
   private int lastTimeSystemWindowWidth   = 0;

   #endregion

 

转载于:https://www.cnblogs.com/liuling2010/articles/1913416.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值