(四十五)c#Winform自定义控件-水波图表
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果

准备工作
这个用到GDI+画的,请先了解一下GDI+
还有用到了基类控件UCControlBase来控制圆角和背景色,如果还不了解请移步查看
开始
添加一个类UCWaveWithSource ,继承UCControlBase
添加属性
private int m_waveActualWidth = ;
private int m_waveWidth = ;
[Description("波形宽度"), Category("自定义")]
public int WaveWidth
{
get { return m_waveWidth; }
set
{
if (value <= )
return;
m_waveWidth = value;
ResetWaveCount();
Refresh();
}
}
private int m_sleepTime = ;
/// <summary>
/// 波运行速度(运行时间间隔,毫秒)
/// </summary>
[Description("运行速度(运行时间间隔,毫秒)"), Category("自定义")]
public int SleepTime
{
get { return m_sleepTime; }
set
{
if (value <= )
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 >= && value <= ))
{
return;
}
m_lineTension = value;
Refresh();
}
}
private Color m_lineColor = Color.FromArgb(, , , );
[Description("曲线颜色"), Category("自定义")]
public Color LineColor
{
get { return m_lineColor; }
set
{
m_lineColor = value;
Refresh();
}
}
private Color m_gridLineColor = Color.FromArgb(, , , );
[Description("网格线颜色"), Category("自定义")]
public Color GridLineColor
{
get { return m_gridLineColor; }
set
{
m_gridLineColor = value;
Refresh();
}
}
private Color m_gridLineTextColor = Color.FromArgb(, , , );
[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 = ;
构造函数中初始化一下样式
public UCWaveWithSource()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true); this.SizeChanged += UCWaveWithSource_SizeChanged;
this.IsShowRect = true;
this.RectColor = Color.FromArgb(, , );
this.FillColor = Color.FromArgb(, , );
this.RectWidth = ;
this.ConerRadius = ;
this.IsRadius = true;
this.Size = new Size(, ); timer.Interval = m_sleepTime;
timer.Tick += timer_Tick;
this.VisibleChanged += UCWave_VisibleChanged;
}
一个数据添加的函数
/// <summary>
/// 添加需要显示的数据
/// </summary>
/// <param name="key">名称</param>
/// <param name="value">值</param>
public void AddSource(string key, double value)
{
m_dataSource.Add(new KeyValuePair<string, double>(key, value));
}
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh(); int intLineSplit = m_drawRect.Height / ;
for (int i = ; i <= ; i++)
{
var pen = new Pen(new SolidBrush(m_gridLineColor), );
// pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
g.DrawLine(pen, m_drawRect.Left, m_drawRect.Bottom - - i * intLineSplit, m_drawRect.Right, m_drawRect.Bottom - - i * intLineSplit);
} if (m_currentSource == null || m_currentSource.Count <= )
{
for (int i = ; i <= ; i++)
{
string strText = ( / * i).ToString();
System.Drawing.SizeF _numSize = g.MeasureString(strText, this.Font);
g.DrawString(strText, Font, new SolidBrush(m_gridLineTextColor), m_drawRect.Left - _numSize.Width - , m_drawRect.Bottom - - i * intLineSplit - (_numSize.Height / ));
}
return;
}
List<Point> lst1 = new List<Point>();
double dblValue = m_currentSource.Max(p => p.Value);
int intValue = (int)dblValue;
int intDivisor = ("".PadRight(intValue.ToString().Length - , '')).ToInt();
if (intDivisor < )
intDivisor = ;
int intTop = intValue;
if (intValue % intDivisor != )
{
intTop = (intValue / intDivisor + ) * intDivisor;
}
if (intTop == )
intTop = ; for (int i = ; i <= ; i++)
{
string strText = (intTop / * i).ToString();
System.Drawing.SizeF _numSize = g.MeasureString(strText, this.Font);
g.DrawString(strText, Font, new SolidBrush(m_gridLineTextColor), m_drawRect.Left - _numSize.Width - , m_drawRect.Bottom - - i * intLineSplit - (_numSize.Height / ));
} int intEndX = ;
int intEndY = ;
for (int i = ; i < m_currentSource.Count; i++)
{
intEndX = i * m_waveActualWidth + m_drawRect.X;
intEndY = m_drawRect.Bottom - - (int)(m_currentSource[i].Value / intTop * m_drawRect.Height);
lst1.Add(new Point(intEndX, intEndY));
if (!string.IsNullOrEmpty(m_currentSource[i].Key))
{
System.Drawing.SizeF _numSize = g.MeasureString(m_currentSource[i].Key, this.Font);
int txtX = intEndX - (int)(_numSize.Width / ) + ;
g.DrawString(m_currentSource[i].Key, Font, new SolidBrush(m_gridLineTextColor), new PointF(txtX, m_drawRect.Bottom + ));
}
} int intFirstY = m_drawRect.Bottom - - (int)(m_currentSource[].Value / intTop * m_drawRect.Height); GraphicsPath path1 = new GraphicsPath();
path1.AddCurve(lst1.ToArray(), m_lineTension);
g.DrawPath(new Pen(new SolidBrush(m_lineColor), ), path1); }
辅助函数
/// <summary>
/// 得到当前需要画图的数据
/// </summary>
/// <returns></returns>
private List<KeyValuePair<string, double>> GetCurrentList()
{
if (m_dataSource.Count < m_waveCount)
{
int intCount = m_waveCount - m_dataSource.Count;
for (int i = ; i < intCount; i++)
{
m_dataSource.Add(new KeyValuePair<string, double>("", ));
}
} var lst = m_dataSource.GetRange(, m_waveCount);
if (lst.Count == )
lst.Insert(, new KeyValuePair<string, double>("", ));
return lst;
} /// <summary>
/// 计算需要显示的个数
/// </summary>
private void ResetWaveCount()
{
m_waveCount = m_drawRect.Width / m_waveWidth;
m_waveActualWidth = m_waveWidth + (m_drawRect.Width % m_waveWidth) / m_waveCount;
m_waveCount++;
if (m_dataSource.Count < m_waveCount)
{
int intCount = m_waveCount - m_dataSource.Count;
for (int i = ; i < intCount; i++)
{
m_dataSource.Insert(, new KeyValuePair<string, double>("", ));
}
}
}
完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
public class UCWaveWithSource : UCControlBase
{
private int m_waveActualWidth = ; private int m_waveWidth = ; [Description("波形宽度"), Category("自定义")]
public int WaveWidth
{
get { return m_waveWidth; }
set
{
if (value <= )
return;
m_waveWidth = value;
ResetWaveCount();
Refresh();
}
} private int m_sleepTime = ;
/// <summary>
/// 波运行速度(运行时间间隔,毫秒)
/// </summary>
[Description("运行速度(运行时间间隔,毫秒)"), Category("自定义")]
public int SleepTime
{
get { return m_sleepTime; }
set
{
if (value <= )
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 >= && value <= ))
{
return;
}
m_lineTension = value;
Refresh();
}
} private Color m_lineColor = Color.FromArgb(, , , ); [Description("曲线颜色"), Category("自定义")]
public Color LineColor
{
get { return m_lineColor; }
set
{
m_lineColor = value;
Refresh(); }
} private Color m_gridLineColor = Color.FromArgb(, , , ); [Description("网格线颜色"), Category("自定义")]
public Color GridLineColor
{
get { return m_gridLineColor; }
set
{
m_gridLineColor = value;
Refresh();
}
} private Color m_gridLineTextColor = Color.FromArgb(, , , ); [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 = ;
public UCWaveWithSource()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true); this.SizeChanged += UCWaveWithSource_SizeChanged;
this.IsShowRect = true;
this.RectColor = Color.FromArgb(, , );
this.FillColor = Color.FromArgb(, , );
this.RectWidth = ;
this.ConerRadius = ;
this.IsRadius = true;
this.Size = new Size(, ); timer.Interval = m_sleepTime;
timer.Tick += timer_Tick;
this.VisibleChanged += UCWave_VisibleChanged;
} /// <summary>
/// 添加需要显示的数据
/// </summary>
/// <param name="key">名称</param>
/// <param name="value">值</param>
public void AddSource(string key, double value)
{
m_dataSource.Add(new KeyValuePair<string, double>(key, value));
} void UCWave_VisibleChanged(object sender, EventArgs e)
{
if (!DesignMode)
{
timer.Enabled = this.Visible;
}
} void timer_Tick(object sender, EventArgs e)
{
m_currentSource = GetCurrentList();
m_dataSource.RemoveAt();
this.Refresh();
}
void UCWaveWithSource_SizeChanged(object sender, EventArgs e)
{
m_drawRect = new Rectangle(, , this.Width - , this.Height - );
ResetWaveCount();
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh(); int intLineSplit = m_drawRect.Height / ;
for (int i = ; i <= ; i++)
{
var pen = new Pen(new SolidBrush(m_gridLineColor), );
// pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
g.DrawLine(pen, m_drawRect.Left, m_drawRect.Bottom - - i * intLineSplit, m_drawRect.Right, m_drawRect.Bottom - - i * intLineSplit);
} if (m_currentSource == null || m_currentSource.Count <= )
{
for (int i = ; i <= ; i++)
{
string strText = ( / * i).ToString();
System.Drawing.SizeF _numSize = g.MeasureString(strText, this.Font);
g.DrawString(strText, Font, new SolidBrush(m_gridLineTextColor), m_drawRect.Left - _numSize.Width - , m_drawRect.Bottom - - i * intLineSplit - (_numSize.Height / ));
}
return;
}
List<Point> lst1 = new List<Point>();
double dblValue = m_currentSource.Max(p => p.Value);
int intValue = (int)dblValue;
int intDivisor = ("".PadRight(intValue.ToString().Length - , '')).ToInt();
if (intDivisor < )
intDivisor = ;
int intTop = intValue;
if (intValue % intDivisor != )
{
intTop = (intValue / intDivisor + ) * intDivisor;
}
if (intTop == )
intTop = ; for (int i = ; i <= ; i++)
{
string strText = (intTop / * i).ToString();
System.Drawing.SizeF _numSize = g.MeasureString(strText, this.Font);
g.DrawString(strText, Font, new SolidBrush(m_gridLineTextColor), m_drawRect.Left - _numSize.Width - , m_drawRect.Bottom - - i * intLineSplit - (_numSize.Height / ));
} int intEndX = ;
int intEndY = ;
for (int i = ; i < m_currentSource.Count; i++)
{
intEndX = i * m_waveActualWidth + m_drawRect.X;
intEndY = m_drawRect.Bottom - - (int)(m_currentSource[i].Value / intTop * m_drawRect.Height);
lst1.Add(new Point(intEndX, intEndY));
if (!string.IsNullOrEmpty(m_currentSource[i].Key))
{
System.Drawing.SizeF _numSize = g.MeasureString(m_currentSource[i].Key, this.Font);
int txtX = intEndX - (int)(_numSize.Width / ) + ;
g.DrawString(m_currentSource[i].Key, Font, new SolidBrush(m_gridLineTextColor), new PointF(txtX, m_drawRect.Bottom + ));
}
} int intFirstY = m_drawRect.Bottom - - (int)(m_currentSource[].Value / intTop * m_drawRect.Height); GraphicsPath path1 = new GraphicsPath();
path1.AddCurve(lst1.ToArray(), m_lineTension);
g.DrawPath(new Pen(new SolidBrush(m_lineColor), ), path1); }
/// <summary>
/// 得到当前需要画图的数据
/// </summary>
/// <returns></returns>
private List<KeyValuePair<string, double>> GetCurrentList()
{
if (m_dataSource.Count < m_waveCount)
{
int intCount = m_waveCount - m_dataSource.Count;
for (int i = ; i < intCount; i++)
{
m_dataSource.Add(new KeyValuePair<string, double>("", ));
}
} var lst = m_dataSource.GetRange(, m_waveCount);
if (lst.Count == )
lst.Insert(, new KeyValuePair<string, double>("", ));
return lst;
} /// <summary>
/// 计算需要显示的个数
/// </summary>
private void ResetWaveCount()
{
m_waveCount = m_drawRect.Width / m_waveWidth;
m_waveActualWidth = m_waveWidth + (m_drawRect.Width % m_waveWidth) / m_waveCount;
m_waveCount++;
if (m_dataSource.Count < m_waveCount)
{
int intCount = m_waveCount - m_dataSource.Count;
for (int i = ; i < intCount; i++)
{
m_dataSource.Insert(, new KeyValuePair<string, double>("", ));
}
}
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(四十五)c#Winform自定义控件-水波图表的更多相关文章
- (四十)c#Winform自定义控件-开关-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (四十六)c#Winform自定义控件-水波进度条-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- NeHe OpenGL教程 第四十五课:顶点缓存
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Gradle 1.12用户指南翻译——第四十五章. 应用程序插件
本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关
0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ...
- “全栈2019”Java第四十五章:super关键字
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备
孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天本来应当继续学习Python的数据库操作,但根据过去我自 ...
- (十四--十五)数据库查询优化Part I
(十四--十五)数据库查询优化Part I 如果理解的有问题.欢迎大家指出.这也是我在看课记得笔记..可能会有很多问题 查询优化的重要性 请记住用户只会告诉DMBS他们想要什么样的结果,而不会告诉他们 ...
- 《手把手教你》系列技巧篇(四十五)-java+ selenium自动化测试-web页面定位toast-上篇(详解教程)
1.简介 在使用appium写app自动化的时候介绍toast的相关元素的定位,在Web UI测试过程中,也经常遇到一些toast,那么这个toast我们这边如何进行测试呢?今天宏哥就分两篇介绍一下. ...
随机推荐
- R035---偷个懒,用UiPath录制电脑操作过程,迅速实现流程自动化
一.缘起 UiPath可以录制你操作电脑的过程,从而实现自动化. 目前有点鸡肋,因为有些操作过程无法录制,例如: 键盘快捷键 修改键 右键点击 鼠标悬停 即便如此,录制功能有时候还是可以用一下,特别 ...
- 自定义new和delete
#include "stdafx.h" #include <stdlib.h> #include <malloc.h> #include <iostr ...
- 比赛:大奔的方案solution
分析: 此题是小奔的方案的改进.小奔的方案思路:倒推,每次都从小到大排序并且保证小号在前,然后使每一个人分到的金币都是上一次加一,直到金币分完或者自己可以存活(投票率大于等于所需概率),如果不行就-1 ...
- window平台下 cmd 命令窗口的编码设置
在WINDOWS上打开控制台界面,发现默认的编码是GBK,这样有时候写完的代码运行的时候就会出码,基于此,查阅各方资料,终于得出两种方案. 一.临时解决方案(只针对本次会话有效) 1. 打开控制台,输 ...
- django第一次(转自刘江大佬)
下面的模型定义了一个“人”,它具有first_name和last_name字段: from django.db import models class Person(models.Model): fi ...
- web设计_2_灵活的文字
最佳设计:可以让用户自由控制任何页面的文字大小. 浏览器中用户都是可以自定义默认的文字大小的,如果使用 px,用户自行在浏览器设置中改变了文字大小后,网页上是不会变化的.我们不能排除视障用户(如近视) ...
- 关于写自定义的SQL接口出现的问题
1.<if test=" as != ' ' "></if> 与 <if test=' as != " " ' ...
- T-SQL 恢复数据库
USE master GO ALTER DATABASE DEMO SET SINGLE_USER GO ALTER DATABASE DEMO SET EMERGENCY GO DBCC CHECK ...
- angularjs的input防抖
在开发中,遇到一个这样的需求,使用$scope.$watch()方法监听input值的改变,然后去$resource请求,但是请求过于频繁,需要做逻辑调整.代码如下: var timeout; $sc ...
- Pow共识算法
谈到哈希算法,每个程序员都不陌生,但是谈到比特币共识算法PoW,如果没有接触过的技术人员可能觉得应该会很复杂,毕竟全球的比特币节点数量如此庞大,达成共识的算法应该不会很简单.但其实如果你已掌握哈希算法 ...