官网

http://www.hzhcontrols.com

前提

入行已经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来控制圆角和背景色,如果还不了解请移步查看

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

开始

添加一个类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自定义控件-水波图表的更多相关文章

  1. (四十)c#Winform自定义控件-开关-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  2. (四十六)c#Winform自定义控件-水波进度条-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  3. NeHe OpenGL教程 第四十五课:顶点缓存

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  4. Gradle 1.12用户指南翻译——第四十五章. 应用程序插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  5. SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关

    0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ...

  6. “全栈2019”Java第四十五章:super关键字

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. 孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备

     孤荷凌寒自学python第四十五天Python初学基础基本结束的下阶段预安装准备 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天本来应当继续学习Python的数据库操作,但根据过去我自 ...

  8. (十四--十五)数据库查询优化Part I

    (十四--十五)数据库查询优化Part I 如果理解的有问题.欢迎大家指出.这也是我在看课记得笔记..可能会有很多问题 查询优化的重要性 请记住用户只会告诉DMBS他们想要什么样的结果,而不会告诉他们 ...

  9. 《手把手教你》系列技巧篇(四十五)-java+ selenium自动化测试-web页面定位toast-上篇(详解教程)

    1.简介 在使用appium写app自动化的时候介绍toast的相关元素的定位,在Web UI测试过程中,也经常遇到一些toast,那么这个toast我们这边如何进行测试呢?今天宏哥就分两篇介绍一下. ...

随机推荐

  1. Python 爬虫:豆瓣电影Top250,包括电影导演、类型、年份、主演

    结果输出到文本文件中. import codecs import requests from bs4 import BeautifulSoup headers={'User-Agent': 'Mozi ...

  2. 洛谷P2172 [国家集训队]部落战争 题解

    题目链接:https://www.luogu.org/problemnew/show/P2172 分析: 不要被[国家集训队]的标签吓到,其实这题不是很难. 本题可以对比P4304 [TJOI2013 ...

  3. [HAOI2006]聪明的猴子 题解

    题意: 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着,部分植物的树冠露在水面上.猴子不会游泳,但跳跃能力比较强,它们仍然可以在 ...

  4. struct模块(用于对象的压缩)

    6.27自我总结 struct模块 1.struct模块中的函数 函数 return explain pack(fmt,v1,v2-) string 按照给定的格式(fmt),把数据转换成字符串(字节 ...

  5. [leetcode] 11. Container With Most Water (medium)

    原题链接 以Y坐标长度作为木桶边界,以X坐标差为桶底,找出可装多少水. 思路: 前后遍历. Runtime: 5 ms, faster than 95.28% of Java class Soluti ...

  6. [leetcode]914. X of a Kind in a Deck of Cards (easy)

    原题 题目原意可转换为 两组有大于等于2的公因数 /** * @param {number[]} deck * @return {boolean} */ var hasGroupsSizeX = fu ...

  7. JAVA项目从运维部署到项目开发(五. Nginx)

    <Nginx与Nginx-rtmp-module搭建RTMP视频直播和点播服务器>一文简单介绍了关于直播数据流的nginx相关配置,下面简单介绍下各种项目如何配置nginx. web项目. ...

  8. MySQL常用工具、日志及读写分离

    MySQL常用工具.日志及读写分离 1.MySQL中常用工具 1.1 mysql 1.1.1连接选项 1.1.2 执行选项 1.2 mysqladmin 1.3 mysqlbinlog 1.4 mys ...

  9. shiro创建配置对象

    在执行 Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(&quo ...

  10. 【Python3爬虫】当爬虫碰到表单提交,有点意思

    一.写在前面 我写爬虫已经写了一段时间了,对于那些使用GET请求或者POST请求的网页,爬取的时候都还算得心应手.不过最近遇到了一个有趣的网站,虽然爬取的难度不大,不过因为表单提交的存在,所以一开始还 ...