(四十六)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+画的,请先了解一下GDI+
还有用到了基类控件UCControlBase来控制圆角和背景色,如果还不了解请移步查看
另外用到了水波控件UCWave,如果不了解请移步查看
开始
添加一个用户控件UCProcessWave,继承UCControlBase
一些属性
private bool m_isRectangle = false;
[Description("是否矩形"), Category("自定义")]
public bool IsRectangle
{
get { return m_isRectangle; }
set
{
m_isRectangle = value;
if (value)
{
base.ConerRadius = ;
}
else
{
base.ConerRadius = Math.Min(this.Width, this.Height);
}
}
}
#region 不再使用的父类属性 English:Parent class attributes that are no longer used
[Browsable(false)]
public new int ConerRadius
{
get;
set;
}
[Browsable(false)]
public new bool IsRadius
{
get;
set;
} [Browsable(false)]
public new Color FillColor
{
get;
set;
}
#endregion [Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged;
int m_value = ;
[Description("当前属性"), Category("自定义")]
public int Value
{
set
{
if (value > m_maxValue)
m_value = m_maxValue;
else if (value < )
m_value = ;
else
m_value = value;
if (ValueChanged != null)
ValueChanged(this, null);
ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
Refresh();
}
get
{
return m_value;
}
} private int m_maxValue = ; [Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value < m_value)
m_maxValue = m_value;
else
m_maxValue = value;
Refresh();
}
} public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
} public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
} [Description("值颜色"), Category("自定义")]
public Color ValueColor
{
get { return this.ucWave1.WaveColor; }
set
{
this.ucWave1.WaveColor = value;
}
} [Description("边框宽度"), Category("自定义")]
public override int RectWidth
{
get
{
return base.RectWidth;
}
set
{
if (value < )
base.RectWidth = ;
else
base.RectWidth = value;
}
}
构造函数一些设置
public UCProcessWave()
{
InitializeComponent();
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);
base.IsRadius = true;
base.IsShowRect = false;
RectWidth = ;
RectColor = Color.White;
ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
this.SizeChanged += UCProcessWave_SizeChanged;
this.ucWave1.OnPainted += ucWave1_Painted;
base.ConerRadius = Math.Min(this.Width, this.Height);
}
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
if (!m_isRectangle)
{
//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
SolidBrush solidBrush = new SolidBrush(Color.White);
e.Graphics.DrawEllipse(new Pen(solidBrush, ), new Rectangle(-, -, this.Width + , this.Height + ));
}
string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / , (this.Height - sizeF.Height) / + ));
}
波形控件重绘时处理
void ucWave1_Painted(object sender, PaintEventArgs e)
{
e.Graphics.SetGDIHigh();
if (IsShowRect)
{
if (m_isRectangle)
{
Color rectColor = RectColor;
Pen pen = new Pen(rectColor, (float)RectWidth);
Rectangle clientRectangle = new Rectangle(, this.ucWave1.Height - this.Height, this.Width, this.Height);
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, , , 180f, 90f);
graphicsPath.AddArc(clientRectangle.Width - - , clientRectangle.Y, , , 270f, 90f);
graphicsPath.AddArc(clientRectangle.Width - - , clientRectangle.Bottom - - , , , 0f, 90f);
graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - - , , , 90f, 90f);
graphicsPath.CloseFigure();
e.Graphics.DrawPath(pen, graphicsPath);
}
else
{
SolidBrush solidBrush = new SolidBrush(RectColor);
e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(, this.ucWave1.Height - this.Height, this.Width, this.Height));
}
} if (!m_isRectangle)
{
//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
SolidBrush solidBrush1 = new SolidBrush(Color.White);
e.Graphics.DrawEllipse(new Pen(solidBrush1, ), new Rectangle(-, this.ucWave1.Height - this.Height - , this.Width + , this.Height + ));
}
string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / , (this.ucWave1.Height - this.Height) + (this.Height - sizeF.Height) / ));
}
不知道你们有没有注意这句话
//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
因为设置原价导致了区域毛边,所有画个没有毛边的边框覆盖之
完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D; namespace HZH_Controls.Controls
{
public partial class UCProcessWave : UCControlBase
{
private bool m_isRectangle = false;
[Description("是否矩形"), Category("自定义")]
public bool IsRectangle
{
get { return m_isRectangle; }
set
{
m_isRectangle = value;
if (value)
{
base.ConerRadius = ;
}
else
{
base.ConerRadius = Math.Min(this.Width, this.Height);
}
}
}
#region 不再使用的父类属性 English:Parent class attributes that are no longer used
[Browsable(false)]
public new int ConerRadius
{
get;
set;
}
[Browsable(false)]
public new bool IsRadius
{
get;
set;
} [Browsable(false)]
public new Color FillColor
{
get;
set;
}
#endregion [Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged;
int m_value = ;
[Description("当前属性"), Category("自定义")]
public int Value
{
set
{
if (value > m_maxValue)
m_value = m_maxValue;
else if (value < )
m_value = ;
else
m_value = value;
if (ValueChanged != null)
ValueChanged(this, null);
ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
Refresh();
}
get
{
return m_value;
}
} private int m_maxValue = ; [Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value < m_value)
m_maxValue = m_value;
else
m_maxValue = value;
Refresh();
}
} public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
} public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
} [Description("值颜色"), Category("自定义")]
public Color ValueColor
{
get { return this.ucWave1.WaveColor; }
set
{
this.ucWave1.WaveColor = value;
}
} [Description("边框宽度"), Category("自定义")]
public override int RectWidth
{
get
{
return base.RectWidth;
}
set
{
if (value < )
base.RectWidth = ;
else
base.RectWidth = value;
}
} public UCProcessWave()
{
InitializeComponent();
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);
base.IsRadius = true;
base.IsShowRect = false;
RectWidth = ;
RectColor = Color.White;
ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
this.SizeChanged += UCProcessWave_SizeChanged;
this.ucWave1.OnPainted += ucWave1_Painted;
base.ConerRadius = Math.Min(this.Width, this.Height);
} void ucWave1_Painted(object sender, PaintEventArgs e)
{
e.Graphics.SetGDIHigh();
if (IsShowRect)
{
if (m_isRectangle)
{
Color rectColor = RectColor;
Pen pen = new Pen(rectColor, (float)RectWidth);
Rectangle clientRectangle = new Rectangle(, this.ucWave1.Height - this.Height, this.Width, this.Height);
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, , , 180f, 90f);
graphicsPath.AddArc(clientRectangle.Width - - , clientRectangle.Y, , , 270f, 90f);
graphicsPath.AddArc(clientRectangle.Width - - , clientRectangle.Bottom - - , , , 0f, 90f);
graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - - , , , 90f, 90f);
graphicsPath.CloseFigure();
e.Graphics.DrawPath(pen, graphicsPath);
}
else
{
SolidBrush solidBrush = new SolidBrush(RectColor);
e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(, this.ucWave1.Height - this.Height, this.Width, this.Height));
}
} if (!m_isRectangle)
{
//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
SolidBrush solidBrush1 = new SolidBrush(Color.White);
e.Graphics.DrawEllipse(new Pen(solidBrush1, ), new Rectangle(-, this.ucWave1.Height - this.Height - , this.Width + , this.Height + ));
}
string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / , (this.ucWave1.Height - this.Height) + (this.Height - sizeF.Height) / ));
} void UCProcessWave_SizeChanged(object sender, EventArgs e)
{
if (!m_isRectangle)
{
base.ConerRadius = Math.Min(this.Width, this.Height);
if (this.Width != this.Height)
{
this.Size = new Size(Math.Min(this.Width, this.Height), Math.Min(this.Width, this.Height));
}
}
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
if (!m_isRectangle)
{
//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
SolidBrush solidBrush = new SolidBrush(Color.White);
e.Graphics.DrawEllipse(new Pen(solidBrush, ), new Rectangle(-, -, this.Width + , this.Height + ));
}
string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / , (this.Height - sizeF.Height) / + ));
}
}
}
namespace HZH_Controls.Controls
{
partial class UCProcessWave
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ucWave1 = new HZH_Controls.Controls.UCWave();
this.SuspendLayout();
//
// ucWave1
//
this.ucWave1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ucWave1.Location = new System.Drawing.Point(, );
this.ucWave1.Name = "ucWave1";
this.ucWave1.Size = new System.Drawing.Size(, );
this.ucWave1.TabIndex = ;
this.ucWave1.Text = "ucWave1";
this.ucWave1.WaveColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucWave1.WaveHeight = ;
this.ucWave1.WaveSleep = ;
this.ucWave1.WaveWidth = ;
//
// UCProcessWave
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.Controls.Add(this.ucWave1);
this.Name = "UCProcessWave";
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false); } #endregion private UCWave ucWave1;
}
}
最后的话
如果你喜欢的话,请到 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教程 第四十六课:全屏反走样
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 四十六、android中的Bitmap
四十六.android中的Bitmap: http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html 四十七.实现调用Android ...
- Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》
Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...
- “全栈2019”Java第四十六章:继承与字段
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 第四十六个知识点 在Sigma协议中,正确性,公正性和零知识性意味着什么
第四十六个知识点 在Sigma协议中,正确性,公正性和零知识性意味着什么 Sigma协议 Sigma协议是Alice想要向Bob证明一些东西的协议(Alice知道一些秘密).他们有下面的一般范式:Al ...
- (四十五)c#Winform自定义控件-水波图表
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- (四十一)c#Winform自定义控件-进度条
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- 牛客练习赛31A 地、颜色、魔法(搜索+二维数组一维表示)
红色来源于山脉,象征着狂躁.愤怒.混乱,血雨腥风,电光火石. 蓝色来源于海岛,象征着控制.幻觉.诡计,运筹帷幄,谋定后动. 绿色来源于树林,象征着生命.蛮力.成长,横冲直撞,生生不息. 黑色来源于沼泽 ...
- django生命周期请求l流程图
django思维导图链接:https://www.processon.com/view/link/5dddb0f8e4b074c442e5c68c
- d3.js 教程 模仿echarts折线图
今天我们来仿echarts折线图,这个图在echarts是折线图堆叠,但是我用d3改造成了普通的折线图,只为了大家学习(其实在简单的写一个布局就可以).废话不多说商行代码. 1 制作 Line 类 c ...
- JS 获取元素、修改元素/css样式/标签属性、简单事件、数据类型
基本使用 写在Script 标签里 引入外部js文件:<script src=" "></script> console.log(" " ...
- Android4.4 RIL短信接收流程分析
最近有客户反馈Android接收不到短信,于是一头扎进RIL里面找原因.最后发现不是RIL的问题,而是BC72上报短信的格式不对,AT+CNMA=1无作用等几个小问题导致的.尽管问题不在RIL,但总算 ...
- 浅谈Mysql重置密码
新手刚开始用MySQL的时候可能会很容易忘记登录密码,下面说一下如何重置和修改密码 第一种方法:直接在命令行窗口操作停止服务器mysql(这是重点:直接停止)打开CMD 在命令行窗口输入MySQL安装 ...
- js的动态表格的增删改查思路
1. 首先我们要知道,动态添加,肯定不是 在页面上写死得,而是通过js调用循环放入到页面上的,我们在写动态表格的时候不要先着急写,我们第一步要做的就是构思,要把自己的逻辑先弄清楚,不然的话,前面是好写 ...
- Linux selinux 规则导致audit拒绝
Linux selinux 规则导致audit拒绝 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-09-26. 查看 audit2why -d audit2allow 这两个命令. ...
- java自学-常用api
API(Application Programming Interface),应用程序编程接口.Java API是JDK中提供给我们使用的类的说明文档.即jdk包里边写好的类,这些类将底层的代码实现封 ...
- RHEL/CentOS 安装最新版Nginx
本篇简单介绍如何在RHEL/CentOS安装最新版本的Nginx. Step 1:安装yum-utils [root@localhost ~]# cat /etc/redhat-release Cen ...