Advertisement
Alex-Trader

C# PercentProgressBar

Jul 29th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. public class PercentProgressBar : ProgressBar
  2. {
  3.     public PercentProgressBar()
  4.     {
  5.        
  6.         base.SetStyle(ControlStyles.UserPaint |
  7.             ControlStyles.AllPaintingInWmPaint |
  8.             ControlStyles.OptimizedDoubleBuffer, true);
  9.     }
  10.  
  11.     protected override void OnPaint(PaintEventArgs e)
  12.     {
  13.        
  14.         ProgressBarRenderer.DrawHorizontalBar(e.Graphics, this.ClientRectangle);
  15.  
  16.        
  17.         double percent = (double)(this.Value - this.Minimum)
  18.             / (double)(this.Maximum - this.Minimum);
  19.         int chunksWidth = (int)((double)(this.ClientSize.Width - 2) * percent);
  20.         Rectangle chunksRect = new Rectangle(1, 1,
  21.             chunksWidth, this.ClientSize.Height - 2);
  22.        
  23.         ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, chunksRect);
  24.  
  25.        
  26.         string displayText = string.Format("{0}%", (int)(percent * 100.0));
  27.        
  28.         TextFormatFlags tff = TextFormatFlags.HorizontalCenter |
  29.             TextFormatFlags.VerticalCenter |
  30.             TextFormatFlags.SingleLine;
  31.         TextRenderer.DrawText(e.Graphics, displayText, this.Font,
  32.             this.ClientRectangle, SystemColors.ControlText, tff);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement