(三十八)c#Winform自定义控件-圆形进度条-HZHControls
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">
目录
https://www.cnblogs.com/bfyx/p/11364884.html
准备工作
我们理一下思路,进度条支持圆环或扇形显示,支持百分比和数值显示
开始
添加一个用户控件,命名UCProcessEllipse
定义2个枚举
public enum ValueType
{
/// <summary>
/// 百分比
/// </summary>
Percent,
/// <summary>
/// 数值
/// </summary>
Absolute
} public enum ShowType
{
/// <summary>
/// 圆环
/// </summary>
Ring,
/// <summary>
/// 扇形
/// </summary>
Sector
}
添加属性
[Description("值改变事件"), Category("自定义")]
public event EventHandler ValueChanged; private Color m_backEllipseColor = Color.FromArgb(, , );
/// <summary>
/// 圆背景色
/// </summary>
[Description("圆背景色"), Category("自定义")]
public Color BackEllipseColor
{
get { return m_backEllipseColor; }
set
{
m_backEllipseColor = value;
Refresh();
}
} private Color m_coreEllipseColor = Color.FromArgb(, , );
/// <summary>
/// 内圆颜色,ShowType=Ring 有效
/// </summary>
[Description("内圆颜色,ShowType=Ring 有效"), Category("自定义")]
public Color CoreEllipseColor
{
get { return m_coreEllipseColor; }
set
{
m_coreEllipseColor = value;
Refresh();
}
} private Color m_valueColor = Color.FromArgb(, , ); [Description("值圆颜色"), Category("自定义")]
public Color ValueColor
{
get { return m_valueColor; }
set
{
m_valueColor = value;
Refresh();
}
} private bool m_isShowCoreEllipseBorder = true;
/// <summary>
/// 内圆是否显示边框,ShowType=Ring 有效
/// </summary>
[Description("内圆是否显示边框,ShowType=Ring 有效"), Category("自定义")]
public bool IsShowCoreEllipseBorder
{
get { return m_isShowCoreEllipseBorder; }
set
{
m_isShowCoreEllipseBorder = value;
Refresh();
}
} private ValueType m_valueType = ValueType.Percent;
/// <summary>
/// 值文字类型
/// </summary>
[Description("值文字类型"), Category("自定义")]
public ValueType ValueType
{
get { return m_valueType; }
set
{
m_valueType = value;
Refresh();
}
} private int m_valueWidth = ;
/// <summary>
/// 外圆值宽度
/// </summary>
[Description("外圆值宽度,ShowType=Ring 有效"), Category("自定义")]
public int ValueWidth
{
get { return m_valueWidth; }
set
{
if (value <= || value > Math.Min(this.Width, this.Height))
return;
m_valueWidth = value;
Refresh();
}
} private int m_valueMargin = ;
/// <summary>
/// 外圆值间距
/// </summary>
[Description("外圆值间距"), Category("自定义")]
public int ValueMargin
{
get { return m_valueMargin; }
set
{
if (value < || m_valueMargin >= m_valueWidth)
return;
m_valueMargin = value;
Refresh();
}
} private int m_maxValue = ;
/// <summary>
/// 最大值
/// </summary>
[Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value > m_value || value <= )
return;
m_maxValue = value;
Refresh();
}
} private int m_value = ;
/// <summary>
/// 当前值
/// </summary>
[Description("当前值"), Category("自定义")]
public int Value
{
get { return m_value; }
set
{
if (m_maxValue < value || value <= )
return;
m_value = value;
if (ValueChanged != null)
{
ValueChanged(this, null);
}
Refresh();
}
}
private Font m_font = new Font("Arial Unicode MS", );
[Description("文字字体"), Category("自定义")]
public override Font Font
{
get
{
return m_font;
}
set
{
m_font = value;
Refresh();
}
}
Color m_foreColor = Color.White;
[Description("文字颜色"), Category("自定义")]
public override Color ForeColor
{
get
{
return m_foreColor;
}
set
{
m_foreColor = value;
Refresh();
}
} private ShowType m_showType = ShowType.Ring; [Description("显示类型"), Category("自定义")]
public ShowType ShowType
{
get { return m_showType; }
set
{
m_showType = value;
Refresh();
}
}
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality; int intWidth = Math.Min(this.Size.Width, this.Size.Height);
//底圆
g.FillEllipse(new SolidBrush(m_backEllipseColor), new Rectangle(new Point(, ), new Size(intWidth, intWidth)));
if (m_showType == HZH_Controls.Controls.ShowType.Ring)
{
//中心圆
int intCore = intWidth - m_valueWidth * ;
g.FillEllipse(new SolidBrush(m_coreEllipseColor), new Rectangle(new Point(m_valueWidth, m_valueWidth), new Size(intCore, intCore)));
//中心圆边框
if (m_isShowCoreEllipseBorder)
{
g.DrawEllipse(new Pen(m_valueColor, ), new Rectangle(new Point(m_valueWidth + , m_valueWidth + ), new Size(intCore - , intCore - )));
}
if (m_value > && m_maxValue > )
{
float fltPercent = (float)m_value / (float)m_maxValue;
if (fltPercent > )
{
fltPercent = ;
} g.DrawArc(new Pen(m_valueColor, m_valueWidth - m_valueMargin * ), new RectangleF(new Point(m_valueWidth / + m_valueMargin / , m_valueWidth / + m_valueMargin / ), new SizeF(intWidth - m_valueWidth - m_valueMargin / + (m_valueMargin == ? : ), intWidth - m_valueWidth - m_valueMargin / + (m_valueMargin == ? : ))), -, fltPercent * ); string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / + , (intWidth - _txtSize.Height) / + ));
}
}
else
{
if (m_value > && m_maxValue > )
{
float fltPercent = (float)m_value / (float)m_maxValue;
if (fltPercent > )
{
fltPercent = ;
} g.FillPie(new SolidBrush(m_valueColor), new Rectangle(m_valueMargin, m_valueMargin, intWidth - m_valueMargin * , intWidth - m_valueMargin * ), -, fltPercent * ); string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / + , (intWidth - _txtSize.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 UCProcessEllipse : UserControl
{
[Description("值改变事件"), Category("自定义")]
public event EventHandler ValueChanged; private Color m_backEllipseColor = Color.FromArgb(, , );
/// <summary>
/// 圆背景色
/// </summary>
[Description("圆背景色"), Category("自定义")]
public Color BackEllipseColor
{
get { return m_backEllipseColor; }
set
{
m_backEllipseColor = value;
Refresh();
}
} private Color m_coreEllipseColor = Color.FromArgb(, , );
/// <summary>
/// 内圆颜色,ShowType=Ring 有效
/// </summary>
[Description("内圆颜色,ShowType=Ring 有效"), Category("自定义")]
public Color CoreEllipseColor
{
get { return m_coreEllipseColor; }
set
{
m_coreEllipseColor = value;
Refresh();
}
} private Color m_valueColor = Color.FromArgb(, , ); [Description("值圆颜色"), Category("自定义")]
public Color ValueColor
{
get { return m_valueColor; }
set
{
m_valueColor = value;
Refresh();
}
} private bool m_isShowCoreEllipseBorder = true;
/// <summary>
/// 内圆是否显示边框,ShowType=Ring 有效
/// </summary>
[Description("内圆是否显示边框,ShowType=Ring 有效"), Category("自定义")]
public bool IsShowCoreEllipseBorder
{
get { return m_isShowCoreEllipseBorder; }
set
{
m_isShowCoreEllipseBorder = value;
Refresh();
}
} private ValueType m_valueType = ValueType.Percent;
/// <summary>
/// 值文字类型
/// </summary>
[Description("值文字类型"), Category("自定义")]
public ValueType ValueType
{
get { return m_valueType; }
set
{
m_valueType = value;
Refresh();
}
} private int m_valueWidth = ;
/// <summary>
/// 外圆值宽度
/// </summary>
[Description("外圆值宽度,ShowType=Ring 有效"), Category("自定义")]
public int ValueWidth
{
get { return m_valueWidth; }
set
{
if (value <= || value > Math.Min(this.Width, this.Height))
return;
m_valueWidth = value;
Refresh();
}
} private int m_valueMargin = ;
/// <summary>
/// 外圆值间距
/// </summary>
[Description("外圆值间距"), Category("自定义")]
public int ValueMargin
{
get { return m_valueMargin; }
set
{
if (value < || m_valueMargin >= m_valueWidth)
return;
m_valueMargin = value;
Refresh();
}
} private int m_maxValue = ;
/// <summary>
/// 最大值
/// </summary>
[Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value > m_value || value <= )
return;
m_maxValue = value;
Refresh();
}
} private int m_value = ;
/// <summary>
/// 当前值
/// </summary>
[Description("当前值"), Category("自定义")]
public int Value
{
get { return m_value; }
set
{
if (m_maxValue < value || value <= )
return;
m_value = value;
if (ValueChanged != null)
{
ValueChanged(this, null);
}
Refresh();
}
}
private Font m_font = new Font("Arial Unicode MS", );
[Description("文字字体"), Category("自定义")]
public override Font Font
{
get
{
return m_font;
}
set
{
m_font = value;
Refresh();
}
}
Color m_foreColor = Color.White;
[Description("文字颜色"), Category("自定义")]
public override Color ForeColor
{
get
{
return m_foreColor;
}
set
{
m_foreColor = value;
Refresh();
}
} private ShowType m_showType = ShowType.Ring; [Description("显示类型"), Category("自定义")]
public ShowType ShowType
{
get { return m_showType; }
set
{
m_showType = value;
Refresh();
}
} public UCProcessEllipse()
{
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);
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality; int intWidth = Math.Min(this.Size.Width, this.Size.Height);
//底圆
g.FillEllipse(new SolidBrush(m_backEllipseColor), new Rectangle(new Point(, ), new Size(intWidth, intWidth)));
if (m_showType == HZH_Controls.Controls.ShowType.Ring)
{
//中心圆
int intCore = intWidth - m_valueWidth * ;
g.FillEllipse(new SolidBrush(m_coreEllipseColor), new Rectangle(new Point(m_valueWidth, m_valueWidth), new Size(intCore, intCore)));
//中心圆边框
if (m_isShowCoreEllipseBorder)
{
g.DrawEllipse(new Pen(m_valueColor, ), new Rectangle(new Point(m_valueWidth + , m_valueWidth + ), new Size(intCore - , intCore - )));
}
if (m_value > && m_maxValue > )
{
float fltPercent = (float)m_value / (float)m_maxValue;
if (fltPercent > )
{
fltPercent = ;
} g.DrawArc(new Pen(m_valueColor, m_valueWidth - m_valueMargin * ), new RectangleF(new Point(m_valueWidth / + m_valueMargin / , m_valueWidth / + m_valueMargin / ), new SizeF(intWidth - m_valueWidth - m_valueMargin / + (m_valueMargin == ? : ), intWidth - m_valueWidth - m_valueMargin / + (m_valueMargin == ? : ))), -, fltPercent * ); string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / + , (intWidth - _txtSize.Height) / + ));
}
}
else
{
if (m_value > && m_maxValue > )
{
float fltPercent = (float)m_value / (float)m_maxValue;
if (fltPercent > )
{
fltPercent = ;
} g.FillPie(new SolidBrush(m_valueColor), new Rectangle(m_valueMargin, m_valueMargin, intWidth - m_valueMargin * , intWidth - m_valueMargin * ), -, fltPercent * ); string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / + , (intWidth - _txtSize.Height) / + ));
}
} }
} public enum ValueType
{
/// <summary>
/// 百分比
/// </summary>
Percent,
/// <summary>
/// 数值
/// </summary>
Absolute
} public enum ShowType
{
/// <summary>
/// 圆环
/// </summary>
Ring,
/// <summary>
/// 扇形
/// </summary>
Sector
}
}
namespace HZH_Controls.Controls
{
partial class UCProcessEllipse
{
/// <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()
{
components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
} #endregion
}
}
用处及效果
最后的话
如果你喜欢的话,请到 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自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- android 自定义控件——(四)圆形进度条
----------------------------------↓↓圆形进度条(源代码下有属性解释)↓↓---------------------------------------------- ...
- NeHe OpenGL教程 第三十八课:资源文件
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- Android自定义控件系列之应用篇——圆形进度条
一.概述 在上一篇博文中,我们给大家介绍了Android自定义控件系列的基础篇.链接:http://www.cnblogs.com/jerehedu/p/4360066.html 这一篇博文中,我们将 ...
- Java进阶(三十八)快速排序
Java进阶(三十八)快速排序 前言 有没有既不浪费空间又可以快一点的排序算法呢?那就是"快速排序"啦!光听这个名字是不是就觉得很高端呢. 假设我们现在对"6 1 2 7 ...
- Qt自定义控件系列(一) --- 圆形进度条
本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...
- SQL注入之Sqli-labs系列第三十八关、第三十九关,第四十关(堆叠注入)
0x1 堆叠注入讲解 (1)前言 国内有的称为堆查询注入,也有称之为堆叠注入.个人认为称之为堆叠注入更为准确.堆叠注入为攻击者提供了很多的攻击手段,通过添加一个新 的查询或者终止查询,可以达到修改数据 ...
- 微信小程序把玩(三十八)获取设备信息 API
原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx. ...
随机推荐
- SpringCloud服务降级案列
一.什么是服务降级 所有的RPC技术里面服务降级是一个最为重要的话题,所谓的降级指的是当服务的提供方不可使用的时候,程序不会出现异常,而会出现本地的操作 二.服务降级案例 1.目录展示 2.导入依赖 ...
- 用故事说透 HTTPS
本文来自素燕公众号,原文地址:用故事说透 HTTPS 故事中的主演:小华今年上大一,这是她第一次离开父母,独自一人到北京上学.今天妈妈的生日,想了想要给妈妈一个祝福,便给妈妈发了条消息:妈妈收到这条消 ...
- [20191218]降序索引疑问4.txt
[20191218]降序索引疑问4.txt --//前几天优化一个项目,我发现许多表里面有有隐含字段,一般开发很少建立函数索引.我自己检查发现里面存在大量的降序索引.--//我感觉有点奇怪,为什么开发 ...
- 剑指offer-36:数组中的逆序对
参考:1. https://www.geeksforgeeks.org/merge-sort/ 2.<剑指Offer:名企面试官精讲典型编程题> 题目描述 在数组中的两个数字,如果前面一个 ...
- Linux ipv6 无状态 设置为 eui64
Linux ipv6 无状态 设置为 eui64 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-08-22. 无状态的ipv6有eui64和stable-privacy模式, 在家 ...
- Jenkins之自动部署、代码安全扫描、自动化接口测试
搭建Jenkins wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.reporpm --i ...
- 【ftp服务】ftp登陆报错530 Login incorrect. Login failed.
ftp登陆时报错,530 Login incorrect.Login failed.1.cat /etc/passwd,查看你登陆的账户主目录和登陆shell对应的是什么,我的是/sbin/nolog ...
- 东芝MCU实现位带操作
位带操作简介 位带操作的概念其实30年前就有了,那还是 8051单片机开创的先河,如今ARM CM3 将此能力进化,可以说,这里的位带操作是8051 位寻址区的威力大幅加强版.即如果要改写某个寄存器的 ...
- Centos7部署mysql
安装mysql yum install mysql mysql-server 会出现以下错误: [root@yl-web yl]# yum install mysql-server Loaded pl ...
- C# List与Dictionary相互转换与高效查找
TestModel类定义: public class TestModel{ public int Id { get; set; } public string Name { get; se ...