|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Drawing; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows.Forms; |
| 8 | + |
| 9 | +namespace ThemePacker |
| 10 | +{ |
| 11 | + public enum ProgressBarDisplayText |
| 12 | + { |
| 13 | + Percentage, |
| 14 | + CustomText |
| 15 | + } |
| 16 | + |
| 17 | + class CustomProgressBar : ProgressBar |
| 18 | + { |
| 19 | + //Property to set to decide whether to print a % or Text |
| 20 | + public ProgressBarDisplayText DisplayStyle { get; set; } |
| 21 | + |
| 22 | + //Property to hold the custom text |
| 23 | + public string CustomText { get; set; } |
| 24 | + |
| 25 | + public CustomProgressBar() |
| 26 | + { |
| 27 | + // Modify the ControlStyles flags |
| 28 | + //http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx |
| 29 | + SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); |
| 30 | + } |
| 31 | + |
| 32 | + protected override void OnPaint(PaintEventArgs e) |
| 33 | + { |
| 34 | + Rectangle rect = ClientRectangle; |
| 35 | + Graphics g = e.Graphics; |
| 36 | + |
| 37 | + ProgressBarRenderer.DrawHorizontalBar(g, rect); |
| 38 | + rect.Inflate(-3, -3); |
| 39 | + if (Value > 0) |
| 40 | + { |
| 41 | + // As we doing this ourselves we need to draw the chunks on the progress bar |
| 42 | + Rectangle clip = new Rectangle(rect.X, rect.Y, (int)Math.Round(((float)Value / Maximum) * rect.Width), rect.Height); |
| 43 | + ProgressBarRenderer.DrawHorizontalChunks(g, clip); |
| 44 | + } |
| 45 | + |
| 46 | + // Set the Display text (Either a % amount or our custom text |
| 47 | + string text = DisplayStyle == ProgressBarDisplayText.Percentage ? Value.ToString() + '%' : CustomText; |
| 48 | + |
| 49 | + |
| 50 | + using (Font f = SystemFonts.DefaultFont) |
| 51 | + { |
| 52 | + |
| 53 | + SizeF len = g.MeasureString(text, f); |
| 54 | + // Calculate the location of the text (the middle of progress bar) |
| 55 | + // Point location = new Point(Convert.ToInt32((rect.Width / 2) - (len.Width / 2)), Convert.ToInt32((rect.Height / 2) - (len.Height / 2))); |
| 56 | + Point location = new Point(Convert.ToInt32((Width / 2) - len.Width / 2), Convert.ToInt32((Height / 2) - len.Height / 2)); |
| 57 | + // The commented-out code will centre the text into the highlighted area only. This will centre the text regardless of the highlighted area. |
| 58 | + // Draw the custom text |
| 59 | + g.DrawString(text, f, Brushes.Black, location); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments