UCWaterTank水池
水池控件一一派生 于UserControl,用户控件
用作储水池或供水池效果呈现
创建过程:
1.设置控件样式:
2.属性扩展: Value 当前水位值
ValueColor水位深度颜色
BorderColor 边框颜色
RectWidth边框粗细
MaxValue 最大水位值
3.绘制外观:边框水位深度
public partial class UCWaterTank : UserControl
{
public UCWaterTank()
{
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);
RectWidth = 2;
this.Size = new Size(100, 200);
}
float f=1.0f;
float fHeight = 0.0f;
private RectangleF valueRect;
int m_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;
UpdateHeight();
Refresh();
}
get
{
return m_value;
}
}
private int m_maxValue = 100;
[Description("最大值"), Category("自定义")]
public int MaxValue
{
get { return m_maxValue; }
set
{
if (value < m_value)
m_maxValue = m_value;
else
m_maxValue = value;
UpdateHeight();
Refresh();
}
}
[Description("值颜色"), Category("自定义")]
private Color valueColor = Color.Blue;
public Color ValueColor
{
get { return valueColor; }
set
{
valueColor = value;
Refresh();
}
}
[Description("边框宽度"), Category("自定义")]
private int rectWidth = 1;
public int RectWidth
{
get
{
return rectWidth;
}
set
{
rectWidth = value;
UpdateHeight();
Refresh();
}
}
[Description("边框颜色"), Category("自定义")]
private Color borderColor = Color.Blue;
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Refresh();
}
}
/// <summary>
/// 更新比例系数和实际水位矩形高度
/// </summary>
private void UpdateHeight()
{
f = (float)(this.Height- 2 * RectWidth) / (float)MaxValue;
//当前水位高度
fHeight = (float)this.Value * f;
//水位实际值的矩形
valueRect = new RectangleF(this.RectWidth, (this.Height - fHeight-rectWidth), this.Width - 2 * RectWidth-1, fHeight );
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
UpdateHeight();
}
/// <summary>
/// 重绘
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
//呈现质量
g.SmoothingMode = SmoothingMode.AntiAlias;
//Pen对象创建
Pen pen = new Pen(new SolidBrush(BorderColor), (float)RectWidth);
//水池边框矩形
Rectangle rect = new Rectangle(0, 0, this.Width-1, this.Height-1);
//圆角矩形路径
GraphicsPath rectpath = PaintClass.GetRoundRectangle(rect, 5);
//画边线
//填充水位
g.FillRectangle(new SolidBrush(ValueColor), valueRect);
g.DrawPath(pen, rectpath);
}
}