(四十四)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 ...
随机推荐
- 在 .NET Core 中使用 Diagnostics (Diagnostic Source) 记录跟踪信息
前言 最新一直在忙着项目上的事情,很久没有写博客了,在这里对关注我的粉丝们说声抱歉,后面我可能更多的分享我们在微服务落地的过程中的一些经验.那么今天给大家讲一下在 .NET Core 2 中引入的全新 ...
- 软帝学院:一万字的Java基础知识总结大全(实用)
Java基础总结大全(实用) 一.基础知识: 1.JVM.JRE和JDK的区别: JVM(Java Virtual Machine):java虚拟机,用于保证java的跨平台的特性. java语言是跨 ...
- 深入了解angularjs中的$digest与$apply方法,从区别聊到使用优化
壹 ❀ 引 如果有人问,在angularjs中修改模型数据为何视图会同步更新呢,我想大多数人一定会回答脏检查(Dirty Checking)相关概念.没错,在angularjs中作用域(scope) ...
- https类型接口,关闭认证(verify=False)情况下消除告警的方法
一.问题描述 当我们测试接口的协议为https时,当关闭了认证(verify=False)的时候,我们测试通过了,但是使用有一条警告显示,最终生成的测试报告也不美观,如下图 二.解决办法 我们可以在封 ...
- java8-Optional的引入
背景 NPE问题,100%的Java程序员都碰到,并且曾经是心中的痛. 1965年英国TonyHoare引入了Null引用,后续的设计语言包括Java都保持了这种设计. 一个例子 业务模型 Perso ...
- Add the Scheduler Module 添加计划程序模块
Important 重要 Scheduler requires the Event business class to be in your XAF application model. Follow ...
- GO基础之接口
一.概念1. 面向对象语言中,接口用于定义对象的行为.接口只指定对象应该做什么,实现这种行为的方法(实现细节)是由对象来决定.2. 在Go语言中,接口是一组方法签名. •接口只指定了类型应该具有的方法 ...
- 下拉框移动 jquery
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html& ...
- GBT32960-2016电动汽车远程服务与管理系统技术规范 第3部分:通信协议及数据格式
电动汽车远程服务与管理系统技术规范 PDF下载地址:https://files.cnblogs.com/files/88223100/GTB32960.zip
- 「SAP技术」A项目关联公司间退货STO流程
[SAP技术]A项目关联公司间退货STO流程 1)创建公司间退货STO单据. 如下图示的公司间退货STO 4500000572, 2),VL10B, 创建交货单. 如下图交货单号:80044918, ...