namespace BMX.UI { using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; [ToolboxBitmap(typeof(BatteryStatusBar), "Icons.BatteryStatusBar.ico")] public partial class BatteryStatusBar : Control { #region Variables private Color borderColor = Color.Black; private Color shadowColor = Color.LightGray; private Color innerColor = Color.White; private int borderWidth; private int cornerRadius; private int value; private Color bar1Color = Color.Red; private Color bar2Color = Color.Orange; private Color bar3Color = Color.Yellow; private Color bar4Color = Color.YellowGreen; private Color bar5Color = Color.LightGreen; #endregion #region Events protected EventHandler OnValueChanged; public event EventHandler ValueChanged { add { if (OnValueChanged != null) { foreach (Delegate d in OnValueChanged.GetInvocationList()) { if (ReferenceEquals(d, value)) { return; } } } OnValueChanged = (EventHandler)Delegate.Combine(OnValueChanged, value); } remove { OnValueChanged = (EventHandler)Delegate.Remove(OnValueChanged, value); } } #endregion public BatteryStatusBar() { SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true); InitializeComponent(); Init(); } private void Init() { borderWidth = 10; cornerRadius = 10; value = 0; } [Category("Progress"), Description("Gets or sets the border width"), Browsable(true)] public int BorderWidth { get { return borderWidth; } set { borderWidth = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the border color"), Browsable(true)] public Color BorderColor { get { return borderColor; } set { borderColor = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the border width"), Browsable(true)] public int CornerRadius { get { return cornerRadius; } set { cornerRadius = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the bar1 color."), Browsable(true)] public Color Bar1Color { get { return bar1Color; } set { bar2Color = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the bar2 color."), Browsable(true)] public Color Bar2Color { get { return bar2Color; } set { bar2Color = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the bar3 color."), Browsable(true)] public Color Bar3Color { get { return bar3Color; } set { bar3Color = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the bar4 color."), Browsable(true)] public Color Bar4Color { get { return bar4Color; } set { bar4Color = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the bar5 color."), Browsable(true)] public Color Bar5Color { get { return bar5Color; } set { bar5Color = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the shadow color."), Browsable(true)] public Color ShadowColor { get { return shadowColor; } set { shadowColor = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the inner color."), Browsable(true)] public Color InnerColor { get { return innerColor; } set { innerColor = value; Invalidate(); } } [Category("Progress"), Description("Gets or sets the value."), Browsable(true)] public int Value { get { return value; } set { this.value = value; Invalidate(); } } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); PaintBar(e.Graphics); } private void PaintBar(Graphics g) { g.SmoothingMode = SmoothingMode.AntiAlias; try { Rectangle rect = ClientRectangle; int width = rect.Width; int height = rect.Height; int topWidth = 24; int topHeight = 12; Rectangle rectTop = new Rectangle(width / 2 - topWidth / 2, 0, topWidth - 1, topHeight); using (GraphicsPath pathTop = GetPath(rectTop, 2)) { g.FillPath(new SolidBrush(borderColor), pathTop); } Rectangle rectBody = new Rectangle(0, topHeight - 4, width -1, height - topHeight + 4 - 1); using (GraphicsPath pathBody = GetPath(rectBody, cornerRadius)) { g.FillPath(new SolidBrush(borderColor), pathBody); g.DrawPath(new Pen(borderColor), pathBody); } Rectangle rectShadow = new Rectangle(borderWidth, topHeight - 4 + borderWidth + 10, width - borderWidth * 2, height - topHeight + 4 - borderWidth * 2 - 20); g.FillRectangle(new SolidBrush(shadowColor), rectShadow); Rectangle rectInner = new Rectangle(borderWidth + borderWidth / 2 + 1, topHeight - 4 + borderWidth + 10 + borderWidth - 1, width - borderWidth * 2 - borderWidth - 1, height - topHeight + 4 - borderWidth * 2 - borderWidth - 20 - 3); g.FillRectangle(new SolidBrush(innerColor), rectInner); //g.DrawLine(new Pen(Color.White, borderWidth), rectBody.X + borderWidth / 2 + 1, rectBody.Y + borderWidth / 2 + 1, rectBody.X + borderWidth / 2 + 1, height - borderWidth); Color barColor = innerColor; if (value <= 20) barColor = bar1Color; else if (value <= 40) barColor = bar2Color; else if (value <= 60) barColor = bar3Color; else if (value <= 80) barColor = bar4Color; else if (value <= 100) barColor = bar5Color; Rectangle rectBar = rectInner; rectBar.Inflate(-borderWidth / 2, -borderWidth); rectBar.Height = (rectInner.Height - borderWidth) * value / 100; rectBar.Y = rectInner.Y + (rectInner.Height - borderWidth / 2 - rectBar.Height); g.FillRectangle(new SolidBrush(barColor), rectBar); //Line int lineHeight = rectInner.Height / 5; int y = rectInner.Y + rectInner.Height - lineHeight; for (int i = 0; i < 5; i++) { g.DrawLine(new Pen(Color.White, borderWidth), rectBar.X - 1, y, rectBar.X + rectBar.Width + 2, y); y = y - lineHeight; } } catch { } } private GraphicsPath GetPath(Rectangle rect, int radius) { GraphicsPath gp = new GraphicsPath(); gp.AddArc(rect.X, rect.Y, radius, radius, 180, 90); gp.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90); gp.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90); gp.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90); gp.AddLine(rect.X, rect.Y + rect.Height - radius, rect.X, rect.Y + radius / 2); return gp; } } }