(四十五)c#Winform自定义控件-水波图表

前提入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。GitHub:https://github.com/kwwwvagaa/NetWinformControl码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git如果觉得写的还行,请点个star支持一下吧欢迎前来交流探讨:企鹅群568015492麻烦博客下...

(四十五)c#Winform自定义控件-水波图表

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492企鹅群568015492

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

这个用到GDI 画的,请先了解一下GDI

还有用到了基类控件UCControlBase来控制圆角和背景色,如果还不了解请移步查看

(一)c#Winform自定义控件-基类控件

开始

添加一个类UCWaveWithSource ,继承UCControlBase

添加属性

  private int m_waveActualWidth = 50;  private int m_waveWidth = 50;  [Description("波形宽度"), Category("自定义")]  public int WaveWidth  {get { return m_waveWidth; }set{ if (value <= 0)  return; m_waveWidth = value; ResetWaveCount(); Refresh();}  }  private int m_sleepTime = 1000;  /// <summary>  /// 波运行速度(运行时间间隔,毫秒)  /// </summary>  [Description("运行速度(运行时间间隔,毫秒)"), Category("自定义")]  public int SleepTime  {get { return m_sleepTime; }set{ if (value <= 0)  return; m_sleepTime = value; if (timer != null) {  timer.Enabled = false;  timer.Interval = value;  timer.Enabled = true; }}  }  private float m_lineTension = 0.5f;  /// <summary>  /// 线弯曲程度  /// </summary>  [Description("线弯曲程度(0-1)"), Category("自定义")]  public float LineTension  {get { return m_lineTension; }set{ if (!(value >= 0 && value <= 1)) {  return; } m_lineTension = value; Refresh();}  }  private Color m_lineColor = Color.FromArgb(150, 73, 119, 232);  [Description("曲线颜色"), Category("自定义")]  public Color LineColor  {get { return m_lineColor; }set{ m_lineColor = value; Refresh();}  }  private Color m_gridLineColor = Color.FromArgb(50, 73, 119, 232);  [Description("网格线颜色"), Category("自定义")]  public Color GridLineColor  {get { return m_gridLineColor; }set{ m_gridLineColor = value; Refresh();}  }  private Color m_gridLineTextColor = Color.FromArgb(150, 73, 119, 232);  [Description("网格文本颜色"), Category("自定义")]  public Color GridLineTextColor  {get { return m_gridLineTextColor; }set{ m_gridLineTextColor = value; Refresh();}  }  public override Font Font  {get{ return base.Font;}set{ base.Font = value;}  }  /// <summary>  /// 数据源,用以缓存所有需要显示的数据  /// </summary>  List<KeyValuePair<string, double>> m_dataSource = new List<KeyValuePair<string, double>>();  /// <summary>  /// 当前需要显示的数据  /// </summary>  List<KeyValuePair<string, double>> m_currentSource = new List<KeyValuePair<string, double>>();  Timer timer = new Timer();  /// <summary>  /// 画图区域  /// </summary>  Rectangle m_drawRect;  int m_waveCount = 0;

构造函数中初始化一下样式

 1public UCWaveWithSource() 2   { 3 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 4 this.SetStyle(ControlStyles.DoubleBuffer, true); 5 this.SetStyle(ControlStyles.ResizeRedraw, true); 6 this.SetStyle(ControlStyles.Selectable, true); 7 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 8 this.SetStyle(ControlStyles.UserPaint, true); 9 10 this.SizeChan
源文地址:https://www.guoxiongfei.cn/cntech/25186.html