Advertisement
SplittyDev

Numeric ProgressBar

Feb 3rd, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9.  
  10. namespace SplittyDev.Progress
  11. {
  12.     public class ProgressLabel : Control
  13.     {
  14.         internal int value;
  15.         public int Value { get { return value; } set { this.value = value; this.Refresh (); } }
  16.  
  17.         internal Color foreColor;
  18.         public Color ForegroundColor { get { return foreColor; } set { foreColor = value; this.Refresh (); } }
  19.  
  20.         internal Color fillColor;
  21.         public Color FillColor { get { return fillColor; } set { fillColor = value; this.Refresh (); } }
  22.  
  23.         internal bool optimize;
  24.         public bool Optimize { get { return optimize; } set { optimize = value; this.Refresh (); } }
  25.  
  26.         internal bool displayPercentage;
  27.         public bool DisplayPercentage { get { return displayPercentage; } set { displayPercentage = value; this.Refresh (); } }
  28.  
  29.         public ProgressLabel ()
  30.         {
  31.             SetStyle (ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
  32.                 ControlStyles.UserPaint, true);
  33.             SetStyle (ControlStyles.FixedWidth | ControlStyles.FixedHeight, true);
  34.             UpdateStyles ();
  35.  
  36.             this.value = 0;
  37.             this.foreColor = Color.Gray;
  38.             this.fillColor = Color.DeepSkyBlue;
  39.             this.Font = new Font (SystemFonts.DefaultFont.FontFamily, 74f, FontStyle.Regular, GraphicsUnit.Pixel);
  40.             this.displayPercentage = true;
  41.             this.optimize = true;
  42.         }
  43.  
  44.         protected override void OnInvalidated (InvalidateEventArgs e)
  45.         {
  46.             Size size = TextRenderer.MeasureText (this.value.ToString () + ((displayPercentage) ? "%" : ""), this.Font);
  47.             this.Size = size;
  48.  
  49.             base.OnInvalidated (e);
  50.         }
  51.  
  52.         protected override void OnPaint (PaintEventArgs e)
  53.         {
  54.             e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
  55.             e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  56.             e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  57.             e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
  58.  
  59.             Rectangle rect = e.ClipRectangle;
  60.             if (Optimize) rect.Inflate (- (this.Size.Width / 7), - (this.Size.Height / 7));
  61.  
  62.             using (LinearGradientBrush overlay = new LinearGradientBrush (rect, foreColor, fillColor, LinearGradientMode.Vertical))
  63.             {
  64.  
  65.                 float val = 1.0f - (float)(((float)value) / 100.0f);
  66.  
  67.                 Blend blend = new Blend ();
  68.  
  69.                 blend.Factors = new float[] { 0f, 0f, 0.5f, 1f, 1f };
  70.                 blend.Positions = new float[] { 0f, (val >= 0.001f) ? val - 0.001f : 0.001f, val, (val <= 0.999f) ? val + 0.001f : 0.999f, 1.0f };
  71.  
  72.                 overlay.Blend = blend;
  73.  
  74.                 e.Graphics.DrawString (this.value.ToString () + ((displayPercentage) ? "%" : ""), this.Font, overlay, new Point (0, 0));
  75.  
  76.             }
  77.  
  78.             base.OnPaint (e);
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement