Files
1258/JinYuan.ControlLib/WaveProcess.cs
T

347 lines
12 KiB
C#
Raw Normal View History

2026-08-04 18:36:40 +08:00
using JinYuan.ControlLib.UCHelper;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace JinYuan.ControlLib
{
public partial class WaveProcess : ControlBase
{
/// <summary>
/// The m is rectangle
/// </summary>
private bool m_isRectangle = false;
/// <summary>
/// Gets or sets a value indicating whether this instance is rectangle.
/// </summary>
/// <value><c>true</c> if this instance is rectangle; otherwise, <c>false</c>.</value>
[Description("是否矩形"), Category("自定义")]
public bool IsRectangle
{
get { return m_isRectangle; }
set
{
m_isRectangle = value;
if (value)
{
base.ConerRadius = 10;
}
else
{
base.ConerRadius = Math.Min(this.Width, this.Height);
}
this.Invalidate();
}
}
#region 不再使用的父类属性 English:Parent class attributes that are no longer used
/// <summary>
/// 圆角角度
/// </summary>
/// <value>The coner radius.</value>
[Browsable(false)]
public new int ConerRadius
{
get;
set;
}
/// <summary>
/// 是否圆角
/// </summary>
/// <value><c>true</c> if this instance is radius; otherwise, <c>false</c>.</value>
[Browsable(false)]
public new bool IsRadius
{
get;
set;
}
/// <summary>
/// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
/// </summary>
/// <value>The color of the fill.</value>
[Browsable(false)]
public new Color FillColor
{
get;
set;
}
#endregion
/// <summary>
/// Occurs when [value changed].
/// </summary>
[Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged;
/// <summary>
/// The m value
/// </summary>
int m_value = 0;
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
[Description("当前属性"), Category("自定义")]
public int Value
{
set
{
if (value > m_maxValue)
m_value = m_maxValue;
else if (value < 0)
m_value = 0;
else
m_value = value;
if (ValueChanged != null)
ValueChanged(this, null);
wave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + wave1.WaveHeight;
this.Invalidate();
}
get
{
return m_value;
}
}
/// <summary>
/// The m maximum value
/// </summary>
private int m_maxValue = 100;
/// <summary>
/// Gets or sets the maximum value.
/// </summary>
/// <value>The maximum value.</value>
[Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value < m_value)
m_maxValue = m_value;
else
m_maxValue = value;
this.Invalidate();
}
}
/// <summary>
/// 获取或设置控件显示的文字的字体。
/// </summary>
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
this.Invalidate();
}
}
/// <summary>
/// 获取或设置控件的前景色。
/// </summary>
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
this.Invalidate();
}
}
/// <summary>
/// Gets or sets the color of the value.
/// </summary>
[Description("值颜色"), Category("自定义")]
public Color WaveColor
{
get { return this.wave1.WaveColor; }
set
{
this.wave1.WaveColor = value;
//this.Refresh();
}
}
/// <summary>
/// 边框宽度
/// </summary>
[Description("边框宽度"), Category("自定义")]
public override int RectWidth
{
get
{
return base.RectWidth;
}
set
{
if (value < 4)
base.RectWidth = 4;
else
base.RectWidth = value;
this.Invalidate();
}
}
/// <summary>
/// 气泡速度(0表示不启用)
/// </summary>
[Description("气泡速度(0表示不启用)"), Category("自定义")]
public int BubbleSpeed
{
get { return this.wave1.BubbleSpeed; }
set
{
this.wave1.BubbleSpeed = value;
//this.Invalidate();
}
}
/// <summary>
/// 气泡生成间隔时间(毫秒,不准确,根据波运行速度判断)
/// </summary>
[Description("气泡生成间隔时间(毫秒,不准确,根据波运行速度判断)"), Category("自定义")]
public int BubbleIntervalTime
{
get { return this.wave1.BubbleIntervalTime; }
set
{
this.wave1.BubbleIntervalTime = value;
this.Invalidate();
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ProcessWave" /> class.
/// </summary>
public WaveProcess()
{
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);
base.IsRadius = true;
base.IsShowRect = false;
RectWidth = 4;
RectColor = Color.White;
ForeColor = Color.White;
wave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + wave1.WaveHeight;
this.SizeChanged += ProcessWave_SizeChanged;
this.wave1.OnPainted += wave1_Painted;
base.ConerRadius = Math.Min(this.Width, this.Height);
}
#region 减少闪烁
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
#endregion
/// <summary>
/// Handles the Painted event of the wave1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
private void wave1_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(0, this.wave1.Height - this.Height, this.Width, this.Height);
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, 10, 10, 180f, 90f);
graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Y, 10, 10, 270f, 90f);
graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Bottom - 10 - 1, 10, 10, 0f, 90f);
graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - 10 - 1, 10, 10, 90f, 90f);
graphicsPath.CloseFigure();
e.Graphics.DrawPath(pen, graphicsPath);
}
else
{
SolidBrush solidBrush = new SolidBrush(RectColor);
e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(0, this.wave1.Height - this.Height, this.Width, this.Height));
}
}
if (!m_isRectangle)
{
//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
SolidBrush solidBrush1 = new SolidBrush(RectColor);
e.Graphics.DrawEllipse(new Pen(solidBrush1, 2), new Rectangle(-1, this.wave1.Height - this.Height - 1, this.Width + 2, this.Height + 2));
}
string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.wave1.Height - this.Height) + (this.Height - sizeF.Height) / 2));
}
/// <summary>
/// Handles the SizeChanged event of the UCProcessWave control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private void ProcessWave_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));
}
}
}
/// <summary>
/// Handles the <see cref="E:Paint" /> event.
/// </summary>
/// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SetGDIHigh();
if (!m_isRectangle)
{
//这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
SolidBrush solidBrush = new SolidBrush(RectColor);
e.Graphics.DrawEllipse(new Pen(solidBrush, 2), new Rectangle(-1, -1, this.Width + 2, this.Height + 2));
}
string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1));
}
}
}