(四十四)c#Winform自定义控件-水波-HZHControls
官网
前提
入行已经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+画的,如果不了解,可以百度一下。
开始
添加一个类 UCWave ,继承Control
定义属性
private Color m_waveColor = Color.FromArgb(, , );
[Description("波纹颜色"), Category("自定义")]
public Color WaveColor
{
get { return m_waveColor; }
set { m_waveColor = value; }
}
private int m_waveWidth = ;
/// <summary>
/// 为方便计算,强制使用10的倍数
/// </summary>
[Description("波纹宽度(为方便计算,强制使用10的倍数)"), Category("自定义")]
public int WaveWidth
{
get { return m_waveWidth; }
set
{
m_waveWidth = value;
m_waveWidth = m_waveWidth / * ;
intLeftX = value * -;
}
}
private int m_waveHeight = ;
/// <summary>
/// 波高
/// </summary>
[Description("波高"), Category("自定义")]
public int WaveHeight
{
get { return m_waveHeight; }
set { m_waveHeight = value; }
}
private int m_waveSleep = ;
/// <summary>
/// 波运行速度(运行时间间隔,毫秒)
/// </summary>
[Description("波运行速度(运行时间间隔,毫秒)"), Category("自定义")]
public int WaveSleep
{
get { return m_waveSleep; }
set
{
if (value <= )
return;
m_waveSleep = value;
if (timer != null)
{
timer.Enabled = false;
timer.Interval = value;
timer.Enabled = true;
}
}
}
Timer timer = new Timer();
int intLeftX = -;
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
List<Point> lst1 = new List<Point>();
List<Point> lst2 = new List<Point>();
int m_intX = intLeftX;
while (true)
{
lst1.Add(new Point(m_intX, ));
lst1.Add(new Point(m_intX + m_waveWidth / , m_waveHeight)); lst2.Add(new Point(m_intX + m_waveWidth / , ));
lst2.Add(new Point(m_intX + m_waveWidth / + m_waveWidth / , m_waveHeight));
m_intX += m_waveWidth;
if (m_intX > this.Width + m_waveWidth)
break;
} GraphicsPath path1 = new GraphicsPath();
path1.AddCurve(lst1.ToArray(), 0.5F);
path1.AddLine(this.Width + , -, this.Width + , this.Height);
path1.AddLine(this.Width + , this.Height, -, this.Height);
path1.AddLine(-, this.Height, -, -); GraphicsPath path2 = new GraphicsPath();
path2.AddCurve(lst2.ToArray(), 0.5F);
path2.AddLine(this.Width + , -, this.Width + , this.Height);
path2.AddLine(this.Width + , this.Height, -, this.Height);
path2.AddLine(-, this.Height, -, -); g.FillPath(new SolidBrush(Color.FromArgb(, m_waveColor.R, m_waveColor.G, m_waveColor.B)), path1);
g.FillPath(new SolidBrush(Color.FromArgb(, m_waveColor.R, m_waveColor.G, m_waveColor.B)), path2);
}
为了“波动”,添加定时器,定时器事件如下
void timer_Tick(object sender, EventArgs e)
{
intLeftX -= ;
if (intLeftX == m_waveWidth * -)
intLeftX = m_waveWidth * -;
this.Refresh();
}
完整代码
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 UCWave : Control
{
private Color m_waveColor = Color.FromArgb(, , ); [Description("波纹颜色"), Category("自定义")]
public Color WaveColor
{
get { return m_waveColor; }
set { m_waveColor = value; }
} private int m_waveWidth = ;
/// <summary>
/// 为方便计算,强制使用10的倍数
/// </summary>
[Description("波纹宽度(为方便计算,强制使用10的倍数)"), Category("自定义")]
public int WaveWidth
{
get { return m_waveWidth; }
set
{
m_waveWidth = value;
m_waveWidth = m_waveWidth / * ;
intLeftX = value * -;
}
} private int m_waveHeight = ;
/// <summary>
/// 波高
/// </summary>
[Description("波高"), Category("自定义")]
public int WaveHeight
{
get { return m_waveHeight; }
set { m_waveHeight = value; }
} private int m_waveSleep = ;
/// <summary>
/// 波运行速度(运行时间间隔,毫秒)
/// </summary>
[Description("波运行速度(运行时间间隔,毫秒)"), Category("自定义")]
public int WaveSleep
{
get { return m_waveSleep; }
set
{
if (value <= )
return;
m_waveSleep = value;
if (timer != null)
{
timer.Enabled = false;
timer.Interval = value;
timer.Enabled = true;
}
}
} Timer timer = new Timer();
int intLeftX = -;
public UCWave()
{
this.Size = new Size(, );
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);
timer.Interval = m_waveSleep;
timer.Tick += timer_Tick;
this.VisibleChanged += UCWave_VisibleChanged;
} void UCWave_VisibleChanged(object sender, EventArgs e)
{
timer.Enabled = this.Visible;
} void timer_Tick(object sender, EventArgs e)
{
intLeftX -= ;
if (intLeftX == m_waveWidth * -)
intLeftX = m_waveWidth * -;
this.Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
List<Point> lst1 = new List<Point>();
List<Point> lst2 = new List<Point>();
int m_intX = intLeftX;
while (true)
{
lst1.Add(new Point(m_intX, ));
lst1.Add(new Point(m_intX + m_waveWidth / , m_waveHeight)); lst2.Add(new Point(m_intX + m_waveWidth / , ));
lst2.Add(new Point(m_intX + m_waveWidth / + m_waveWidth / , m_waveHeight));
m_intX += m_waveWidth;
if (m_intX > this.Width + m_waveWidth)
break;
} GraphicsPath path1 = new GraphicsPath();
path1.AddCurve(lst1.ToArray(), 0.5F);
path1.AddLine(this.Width + , -, this.Width + , this.Height);
path1.AddLine(this.Width + , this.Height, -, this.Height);
path1.AddLine(-, this.Height, -, -); GraphicsPath path2 = new GraphicsPath();
path2.AddCurve(lst2.ToArray(), 0.5F);
path2.AddLine(this.Width + , -, this.Width + , this.Height);
path2.AddLine(this.Width + , this.Height, -, this.Height);
path2.AddLine(-, this.Height, -, -); g.FillPath(new SolidBrush(Color.FromArgb(, m_waveColor.R, m_waveColor.G, m_waveColor.B)), path1);
g.FillPath(new SolidBrush(Color.FromArgb(, m_waveColor.R, m_waveColor.G, m_waveColor.B)), path2);
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(四十四)c#Winform自定义控件-水波-HZHControls的更多相关文章
- (四十)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教程 第四十四课:3D光晕
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 网站开发进阶(四十四)input type="submit" 和"button"的区别
网站开发进阶(四十四)input type="submit" 和"button"的区别 在一个页面上画一个按钮,有四种办法: 这就是一个按钮.如果你不写ja ...
- 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第四十四章:继承
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 孤荷凌寒自学python第四十四天Python操作 数据库之准备工作
孤荷凌寒自学python第四十四天Python操作数据库之准备工作 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天非常激动地开始接触Python的数据库操作的学习了,数据库是系统化设计 ...
- Android项目实战(四十四):Zxing二维码切换横屏扫描
原文:Android项目实战(四十四):Zxing二维码切换横屏扫描 Demo链接 默认是竖屏扫描,但是当我们在清单文件中配置横屏显示的时候: <activity android:name=&q ...
随机推荐
- 《Java基础知识》Java标示符、保留字和数制
一.Java标识符程序员对程序中的各个元素加以命名时使用的命名记号称为标识符(identifier).Java语言中,标识符是以字母,下划线(_),美元符($)开始的一个字符序列,后面可以跟字母,下划 ...
- 《Java基础知识》Java接口和抽象类的区别
抽象类 抽象类必须用 abstract 修饰,子类必须实现抽象类中的抽象方法,如果有未实现的,那么子类也必须用 abstract 修饰.抽象类默认的权限修饰符为 public,可以定义为 public ...
- Golang中类面向对象特性
一.类型方法的实例成员复制与类型方法的实例成员引用 在Go中可以类似Java等面向对象语言一定为某个对象定义方法,但是Go中并没有类的存在,可以不严格的将Go中的struct类型理解为面向对象中的 ...
- 搭建Android开发环境 以及 ionic 编译安卓app步骤
1. 下载安装JDK 下载地址: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- MQ报错2009/2085解决方法
1.1. 响应2009错误 1.1.1. 涉及协议 MQ,调试回放阶段 1.1.2. 错误信息 完成码2原因为2009 1.1.3. 可能原因 远端MQ连接数不足,拒绝连接 1.1.4. ...
- 基于MbedTLS的AES加密实现,含STM32H7和STM32F4的实现例程
说明: 1.mbedTLS的前身是PolarSSL,开源免费. 主要提供了的SSL/TLS支持(在传输层对网络进行加密),各种加密算法,各种哈希算法,随机数生成以及X.509(密码学里公钥证书的格式标 ...
- 活久见: maven pom 竟然都会崩溃!
问题是: 我的应用的pom 并没有任何报错,但是代码报错,而且编译不通过. 如下,我本地项目,从 spring-cloud-alibaba-dependencies 0.2.1.RELEASE 升级到 ...
- Azure 上的高可用概念
更多内容,添加公众号关注: 场景一: 某智能家居厂家,用户喊出“小X同学,帮我扫地”后,服务器宕机了,扫地机器人不能立即启动,于是,用户可能再连续喊几次后,无奈又习惯的按下了扫地机器人的启动按钮. 场 ...
- WebShell代码分析溯源(六)
WebShell代码分析溯源 一.一句话变形马样本 <?php call_user_func('assert', $_REQUEST['assert']); ?> 二.代码分析 1.分析代 ...
- MyBatis操作Oracle批量插入 ORA-00933: SQL 命令未正确结束
最近在使用MyBatis操作Oracle数据库的时候,进行批量插入数据,思路是封装一个List集合通过Myabtis 的foreach标签进行循环插入,可是搬照Mysql的批量插入会产生 异常 ### ...