// ***********************************************************************
// Assembly : HZH_Controls
// Created : 08-17-2019
//
// ***********************************************************************
//
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
//
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
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
{
///
/// Class UCProcessEllipse.
/// Implements the
///
///
public partial class UCProcessEllipse : UserControl
{
///
/// Occurs when [value changed].
///
[Description("值改变事件"), Category("自定义")]
public event EventHandler ValueChanged;
///
/// The m back ellipse color
///
private Color m_backEllipseColor = Color.FromArgb(228, 231, 237);
///
/// 圆背景色
///
/// The color of the back ellipse.
[Description("圆背景色"), Category("自定义")]
public Color BackEllipseColor
{
get { return m_backEllipseColor; }
set
{
m_backEllipseColor = value;
Refresh();
}
}
///
/// The m core ellipse color
///
private Color m_coreEllipseColor = Color.FromArgb(228, 231, 237);
///
/// 内圆颜色,ShowType=Ring 有效
///
/// The color of the core ellipse.
[Description("内圆颜色,ShowType=Ring 有效"), Category("自定义")]
public Color CoreEllipseColor
{
get { return m_coreEllipseColor; }
set
{
m_coreEllipseColor = value;
Refresh();
}
}
///
/// The m value color
///
private Color m_valueColor = Color.FromArgb(255, 77, 59);
///
/// Gets or sets the color of the value.
///
/// The color of the value.
[Description("值圆颜色"), Category("自定义")]
public Color ValueColor
{
get { return m_valueColor; }
set
{
m_valueColor = value;
Refresh();
}
}
///
/// The m is show core ellipse border
///
private bool m_isShowCoreEllipseBorder = true;
///
/// 内圆是否显示边框,ShowType=Ring 有效
///
/// true if this instance is show core ellipse border; otherwise, false.
[Description("内圆是否显示边框,ShowType=Ring 有效"), Category("自定义")]
public bool IsShowCoreEllipseBorder
{
get { return m_isShowCoreEllipseBorder; }
set
{
m_isShowCoreEllipseBorder = value;
Refresh();
}
}
///
/// The m value type
///
private ValueType m_valueType = ValueType.Percent;
///
/// 值文字类型
///
/// The type of the value.
[Description("值文字类型"), Category("自定义")]
public ValueType ValueType
{
get { return m_valueType; }
set
{
m_valueType = value;
Refresh();
}
}
///
/// The m value width
///
private int m_valueWidth = 30;
///
/// 外圆值宽度
///
/// The width of the value.
[Description("外圆值宽度,ShowType=Ring 有效"), Category("自定义")]
public int ValueWidth
{
get { return m_valueWidth; }
set
{
if (value <= 0 || value > Math.Min(this.Width, this.Height))
return;
m_valueWidth = value;
Refresh();
}
}
///
/// The m value margin
///
private int m_valueMargin = 5;
///
/// 外圆值间距
///
/// The value margin.
[Description("外圆值间距"), Category("自定义")]
public int ValueMargin
{
get { return m_valueMargin; }
set
{
if (value < 0 || m_valueMargin >= m_valueWidth)
return;
m_valueMargin = value;
Refresh();
}
}
///
/// The m maximum value
///
private int m_maxValue = 100;
///
/// 最大值
///
/// The maximum value.
[Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value > m_value || value <= 0)
return;
m_maxValue = value;
Refresh();
}
}
///
/// The m value
///
private int m_value = 0;
///
/// 当前值
///
/// The value.
[Description("当前值"), Category("自定义")]
public int Value
{
get { return m_value; }
set
{
if (m_maxValue < value || value < 0)
return;
m_value = value;
if (ValueChanged != null)
{
ValueChanged(this, null);
}
Refresh();
}
}
///
/// The m font
///
///
/// 获取或设置控件显示的文字的字体。
///
/// The font.
///
///
///
///
///
///
[Description("文字字体"), Category("自定义")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
}
///
/// 获取或设置控件的前景色。
///
/// The color of the fore.
///
///
///
[Description("文字颜色"), Category("自定义"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),Localizable(true)]
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
Refresh();
}
}
///
/// The m show type
///
private ShowType m_showType = ShowType.Ring;
///
/// Gets or sets the type of the show.
///
/// The type of the show.
[Description("显示类型"), Category("自定义")]
public ShowType ShowType
{
get { return m_showType; }
set
{
m_showType = value;
Refresh();
}
}
///
/// Initializes a new instance of the class.
///
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);
ForeColor = Color.White;
Font = new Font("Arial Unicode MS", 20);
}
///
/// 引发 事件。
///
/// 包含事件数据的 。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
int intWidth = Math.Min(this.Size.Width, this.Size.Height) - 1;
//底圆
g.FillEllipse(new SolidBrush(m_backEllipseColor), new Rectangle(new Point(0, 0), new Size(intWidth, intWidth)));
if (m_showType == HZH_Controls.Controls.ShowType.Ring)
{
//中心圆
int intCore = intWidth - m_valueWidth * 2;
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, 2), new Rectangle(new Point(m_valueWidth + 1, m_valueWidth + 1), new Size(intCore - 1, intCore - 1)));
}
if (m_value > 0 && m_maxValue > 0)
{
float fltPercent = (float)m_value / (float)m_maxValue;
if (fltPercent > 1)
{
fltPercent = 1;
}
g.DrawArc(new Pen(m_valueColor, m_valueWidth - m_valueMargin * 2), new RectangleF(new Point(m_valueWidth / 2 + m_valueMargin / 4, m_valueWidth / 2 + m_valueMargin / 4), new SizeF(intWidth - m_valueWidth - m_valueMargin / 2 + (m_valueMargin == 0 ? 0 : 1), intWidth - m_valueWidth - m_valueMargin / 2 + (m_valueMargin == 0 ? 0 : 1))), -90, fltPercent * 360);
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) / 2 + 1, (intWidth - _txtSize.Height) / 2 + 1));
}
}
else
{
if (m_value > 0 && m_maxValue > 0)
{
float fltPercent = (float)m_value / (float)m_maxValue;
if (fltPercent > 1)
{
fltPercent = 1;
}
g.FillPie(new SolidBrush(m_valueColor), new Rectangle(m_valueMargin, m_valueMargin, intWidth - m_valueMargin * 2, intWidth - m_valueMargin * 2), -90, fltPercent * 360);
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) / 2 + 1, (intWidth - _txtSize.Height) / 2 + 1));
}
}
}
}
///
/// Enum ValueType
///
public enum ValueType
{
///
/// 百分比
///
Percent,
///
/// 数值
///
Absolute
}
///
/// Enum ShowType
///
public enum ShowType
{
///
/// 圆环
///
Ring,
///
/// 扇形
///
Sector
}
}