SunnyUI/SunnyUI/Controls/UIRoundProcess.cs

271 lines
7.7 KiB
C#
Raw Normal View History

2021-04-08 23:10:53 +08:00
/******************************************************************************
* SunnyUI
2025-01-07 22:19:21 +08:00
* CopyRight (C) 2012-2025 ShenYongHua().
2021-04-08 23:10:53 +08:00
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UIRoundProcess.cs
* :
2022-01-05 21:57:47 +08:00
* : V3.1
2021-04-08 23:10:53 +08:00
* : 2021-04-08
*
* 2021-04-08: V3.0.2
* 2021-10-18: V3.0.8
* 2022-03-19: V3.1.1
* 2023-07-12: V3.4.0
* 2023-07-15: V3.4.0
2021-04-08 23:10:53 +08:00
******************************************************************************/
2021-04-09 17:39:28 +08:00
using System;
2021-04-08 23:10:53 +08:00
using System.ComponentModel;
using System.Drawing;
2021-04-09 17:39:28 +08:00
using System.Drawing.Drawing2D;
using System.Windows.Forms;
2021-04-08 23:10:53 +08:00
namespace Sunny.UI
{
/// <summary>
/// 圆形滚动条
/// </summary>
2021-04-09 17:39:28 +08:00
[ToolboxItem(true)]
[DefaultEvent("ValueChanged")]
[DefaultProperty("Value")]
2025-03-18 22:12:54 +08:00
[Description("圆形进度条控件")]
2021-04-08 23:10:53 +08:00
public class UIRoundProcess : UIControl
{
public UIRoundProcess()
{
SetStyleFlags(true, false);
2021-04-08 23:10:53 +08:00
Size = new Size(120, 120);
Inner = 30;
Outer = 50;
fillColor = UIStyles.Blue.ProcessBarForeColor;
foreColor = UIStyles.Blue.ProcessBarForeColor;
rectColor = UIStyles.Blue.ProcessBackColor;
2021-04-08 23:10:53 +08:00
ShowText = false;
ShowRect = false;
}
private int startAngle = 0;
[Description("起始角度正北为0顺时针0到360°"), Category("SunnyUI")]
[DefaultValue(0)]
public int StartAngle
{
get => startAngle;
set
{
startAngle = value;
Invalidate();
}
}
private int sweepAngle = 360;
[Description("扫描角度范围0到360°"), Category("SunnyUI")]
[DefaultValue(360)]
public int SweepAngle
{
get => sweepAngle;
set
{
sweepAngle = Math.Max(1, value);
sweepAngle = Math.Min(360, value);
Invalidate();
}
}
[Description("显示文字小数位数"), Category("SunnyUI")]
[DefaultValue(1)]
public int DecimalPlaces
{
get => decimalCount;
set
{
decimalCount = Math.Max(value, 0);
Text = (posValue * 100.0 / maximum).ToString("F" + decimalCount) + "%";
}
}
private int decimalCount = 1;
2021-04-09 17:39:28 +08:00
private int maximum = 100;
2021-04-08 23:10:53 +08:00
2021-04-09 17:39:28 +08:00
[DefaultValue(100)]
[Description("最大值"), Category("SunnyUI")]
public int Maximum
{
get => maximum;
set
{
maximum = Math.Max(1, value);
Invalidate();
}
}
private int inner = 30;
private int outer = 50;
[Description("内径")]
[Category("SunnyUI")]
[DefaultValue(30)]
public int Inner
{
get => inner;
set
{
inner = Math.Max(value, 0);
Invalidate();
}
}
[Description("外径")]
[Category("SunnyUI")]
[DefaultValue(50)]
public int Outer
{
get => outer;
set
{
outer = Math.Max(value, 5);
Invalidate();
}
}
/// <summary>
/// 进度条前景色
/// </summary>
[Description("进度条前景色")]
[Category("SunnyUI")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color ProcessColor
{
get => fillColor;
set => SetFillColor(value);
}
/// <summary>
/// 进度条背景色
/// </summary>
[Description("进度条背景色")]
[Category("SunnyUI")]
[DefaultValue(typeof(Color), "185, 217, 255")]
2021-04-09 17:39:28 +08:00
public Color ProcessBackColor
{
get => rectColor;
set => SetRectColor(value);
}
/// <summary>
/// 字体颜色
/// </summary>
[Description("字体颜色")]
[Category("SunnyUI")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public override Color ForeColor
{
get => foreColor;
set => SetForeColor(value);
}
private int posValue;
[DefaultValue(0)]
[Description("当前位置"), Category("SunnyUI")]
public int Value
{
get => posValue;
set
{
2022-01-13 10:35:31 +08:00
value = Math.Max(value, 0);
value = Math.Min(value, maximum);
if (posValue != value)
{
posValue = value;
Text = (posValue * 100.0 / maximum).ToString("F" + decimalCount) + "%";
ValueChanged?.Invoke(this, posValue);
Invalidate();
}
2021-04-09 17:39:28 +08:00
}
}
protected override void OnPaintFore(Graphics g, GraphicsPath path)
{
if (ShowText)
{
Size size = TextRenderer.MeasureText(Text, Font);
if (Inner * 2 < size.Width - 4)
g.DrawString(Text, Font, ForeColor2, new Rectangle(0, 0, Width, Height), ContentAlignment.MiddleCenter);
else
g.DrawString(Text, Font, ForeColor, new Rectangle(0, 0, Width, Height), ContentAlignment.MiddleCenter);
}
}
private Color foreColor2 = Color.Black;
public Color ForeColor2
{
get => foreColor2;
set
{
foreColor2 = value;
2023-11-06 14:08:09 +08:00
Invalidate();
}
}
2021-04-09 17:39:28 +08:00
public delegate void OnValueChanged(object sender, int value);
2021-04-08 23:10:53 +08:00
2021-04-09 17:39:28 +08:00
public event OnValueChanged ValueChanged;
2021-04-08 23:10:53 +08:00
2022-06-23 23:27:55 +08:00
/// <summary>
/// 绘制填充颜色
/// </summary>
/// <param name="g">绘图图面</param>
/// <param name="path">绘图路径</param>
2021-04-09 17:39:28 +08:00
protected override void OnPaintFill(Graphics g, GraphicsPath path)
{
2021-06-24 22:15:47 +08:00
int iin = Math.Min(inner, outer);
int iou = Math.Max(inner, outer);
if (iin == iou)
{
iou = iin + 1;
}
inner = iin;
outer = iou;
g.FillFan(ProcessBackColor, ClientRectangle.Center(), Inner, Outer, StartAngle - 90, SweepAngle);
g.FillFan(ProcessColor, ClientRectangle.Center(), Inner, Outer, StartAngle - 90, Value * 1.0f / Maximum * SweepAngle);
2021-04-09 17:39:28 +08:00
}
2021-04-08 23:10:53 +08:00
2022-06-23 23:27:55 +08:00
/// <summary>
/// 设置主题样式
/// </summary>
/// <param name="uiColor">主题样式</param>
2021-04-09 17:39:28 +08:00
public override void SetStyleColor(UIBaseStyle uiColor)
{
base.SetStyleColor(uiColor);
fillColor = uiColor.ProcessBarForeColor;
foreColor = uiColor.ProcessBarForeColor;
rectColor = uiColor.ProcessBackColor;
foreColor2 = uiColor.RoundProcessForeColor2;
2021-04-09 17:39:28 +08:00
}
2021-04-08 23:10:53 +08:00
[DefaultValue(false)]
2021-04-09 17:39:28 +08:00
public bool ShowProcess
2021-04-08 23:10:53 +08:00
{
2021-04-09 17:39:28 +08:00
get => ShowText;
set => ShowText = value;
2021-04-08 23:10:53 +08:00
}
}
}