/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Drawing; using System.Windows.Forms; namespace TBF { public class BarGraph { int originX; /// Left coordinate of the bar int originY; /// Top coordinate of the bar int barWidth; /// Bar width, the bar is actualy 1 pixel wider int barHeight; /// Bar height the bat is actualy 1 pixel higher Color barColor; /// Bar color Color backColor; /// Background color (above the bar) /// Bar border is always black, 1 pixel wide int barValue; /// The actual bar size (the bar is drawn from bottom) System.Windows.Forms.Control control; Rectangle rect; /// Use this setter to update the bar size public float FValue { get { return (float)barValue/(float)barHeight; } set { Value = (int)(value * (float)barHeight); control.Invalidate(); } } /// Use this setter to update the bar size public int Value { get { return barValue; } set { if (value < 0) barValue = 0; else if (value >= barHeight) barValue = barHeight; else barValue = value; control.Invalidate(); } } /// /// Constructor /// public BarGraph(System.Windows.Forms.Control control, int originX, int originY, int barWidth, int barHeight, Color barColor, Color backColor) { this.control = control; this.originX = originX; this.originY = originY; this.barWidth = barWidth; this.barHeight = barHeight; this.barColor = barColor; this.backColor = backColor; rect = new Rectangle(originX, originY, barWidth + 1, barHeight + 1); barValue = 0; } /// /// Call this function in Form1_Paint(..), pass the arguments without any modifications /// public void Paint(Graphics g) { g.FillRectangle(new SolidBrush(backColor), originX + 1, originY + 1, barWidth, barHeight - barValue); g.FillRectangle(new SolidBrush(barColor), originX + 1, originY + barHeight + 1 - barValue, barWidth, barValue); g.DrawRectangle(new Pen(Color.Black, 2.0F), originX, originY, barWidth + 1, barHeight + 1); } } }