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;
using JinYuan.ControlLib.UCHelper;
namespace JinYuan.ControlLib
{
public partial class WaveProcess : ControlBase
{
///
/// The m is rectangle
///
private bool m_isRectangle = false;
///
/// Gets or sets a value indicating whether this instance is rectangle.
///
/// true if this instance is rectangle; otherwise, false.
[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
///
/// 圆角角度
///
/// The coner radius.
[Browsable(false)]
public new int ConerRadius
{
get;
set;
}
///
/// 是否圆角
///
/// true if this instance is radius; otherwise, false.
[Browsable(false)]
public new bool IsRadius
{
get;
set;
}
///
/// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
///
/// The color of the fill.
[Browsable(false)]
public new Color FillColor
{
get;
set;
}
#endregion
///
/// Occurs when [value changed].
///
[Description("值变更事件"), Category("自定义")]
public event EventHandler ValueChanged;
///
/// The m value
///
int m_value = 0;
///
/// Gets or sets the value.
///
/// The 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;
}
}
///
/// The m maximum value
///
private int m_maxValue = 100;
///
/// Gets or sets the maximum value.
///
/// The maximum 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();
}
}
///
/// 获取或设置控件显示的文字的字体。
///
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
this.Invalidate();
}
}
///
/// 获取或设置控件的前景色。
///
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
this.Invalidate();
}
}
///
/// Gets or sets the color of the value.
///
[Description("值颜色"), Category("自定义")]
public Color WaveColor
{
get { return this.wave1.WaveColor; }
set
{
this.wave1.WaveColor = value;
//this.Refresh();
}
}
///
/// 边框宽度
///
[Description("边框宽度"), Category("自定义")]
public override int RectWidth
{
get
{
return base.RectWidth;
}
set
{
if (value < 4)
base.RectWidth = 4;
else
base.RectWidth = value;
this.Invalidate();
}
}
///
/// 气泡速度(0表示不启用)
///
[Description("气泡速度(0表示不启用)"), Category("自定义")]
public int BubbleSpeed
{
get { return this.wave1.BubbleSpeed; }
set
{
this.wave1.BubbleSpeed = value;
//this.Invalidate();
}
}
///
/// 气泡生成间隔时间(毫秒,不准确,根据波运行速度判断)
///
[Description("气泡生成间隔时间(毫秒,不准确,根据波运行速度判断)"), Category("自定义")]
public int BubbleIntervalTime
{
get { return this.wave1.BubbleIntervalTime; }
set
{
this.wave1.BubbleIntervalTime = value;
this.Invalidate();
}
}
///
/// Initializes a new instance of the class.
///
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
///
/// Handles the Painted event of the wave1 control.
///
/// The source of the event.
/// The instance containing the event data.
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));
}
///
/// Handles the SizeChanged event of the UCProcessWave control.
///
/// The source of the event.
/// The instance containing the event data.
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));
}
}
}
///
/// Handles the event.
///
/// The instance containing the event data.
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));
}
}
}