Advertisement
Yassine_Abbani

Advanced [Progress bar], [Costume Control]

May 14th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. using System.Drawing.Drawing2D;
  9.  
  10.  
  11.  
  12.     #region ProgressBar
  13.  
  14.     /// <summary>
  15.     /// A basic vertical/horizontal ProgressBar
  16.     /// </summary>
  17.     public partial class Ce_Advance_ProgressBar : Control
  18.     {
  19.         /// <summary>
  20.         /// Value changed event handler delegate
  21.         /// </summary>
  22.         /// <param name="sender">the ProgressBar</param>
  23.         /// <param name="e">ValueChangedEventArgs</param>
  24.         public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);
  25.  
  26.         /// <summary>
  27.         /// Determines how the text on the progressbar is shown.
  28.         /// <para>None - No text is drawn</para>
  29.         /// <para>Percentage - The percentage done is shown</para>
  30.         /// <para>Text - Draws the text assosciated with the control</para>
  31.         /// <para>Value - The current Value is shown</para>
  32.         /// <para>ValueOverMaximum - The current Value shown with the Maximum value</para>
  33.         /// </summary>
  34.         public enum TextStyleType
  35.         {
  36.             None,
  37.             Percentage,
  38.             Text,
  39.             Value,
  40.             ValueOverMaximum
  41.         };
  42.  
  43.         private int minimum = 0;
  44.         private int maximum = 100;
  45.         private int currentValue = 25;
  46.  
  47.         private Orientation orientation = Orientation.Vertical;
  48.  
  49.         private Color barColor = Color.DodgerBlue;
  50.         private Color borderColor = Color.Black;
  51.         private int borderThickness = 2;
  52.  
  53.         private TextStyleType textStyle = TextStyleType.Value;
  54.         private TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordEllipsis;
  55.         private Color textColor = Color.Black;
  56.  
  57.         private CompositingMode compositingMode = CompositingMode.SourceOver;
  58.         private CompositingQuality compositingQuality = CompositingQuality.HighQuality;
  59.         private InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic;
  60.         private PixelOffsetMode pixelOffsetMode = PixelOffsetMode.HighQuality;
  61.         private SmoothingMode smoothingMode = SmoothingMode.HighQuality;
  62.  
  63.         private bool hasErrors = false;
  64.         private string errorLog = null;
  65.  
  66.         /// <summary>
  67.         /// BasicProgressBar initialization
  68.         /// </summary>
  69.         public Ce_Advance_ProgressBar()
  70.         {
  71.  
  72.             this.SetStyle
  73.                 (
  74.                 ControlStyles.UserPaint |
  75.                 ControlStyles.AllPaintingInWmPaint |
  76.                 ControlStyles.ResizeRedraw |
  77.                 ControlStyles.OptimizedDoubleBuffer |
  78.                 ControlStyles.SupportsTransparentBackColor,
  79.                 true
  80.                 );
  81.  
  82.             this.ForeColor = barColor;
  83.             this.BackColor = Color.DarkGray;
  84.             this.Font = new Font("Consolas", 10.25f);
  85.             OnForeColorChanged(EventArgs.Empty);
  86.  
  87.         }
  88.  
  89.  
  90.         /// <summary>
  91.         /// Occurs when the ProgressBar's Value changed.
  92.         /// </summary>
  93.         [Category("Action"),
  94.         Description("Occurs when the ProgressBar's Value changed")]
  95.         public event ValueChangedEventHandler ValueChanged;
  96.  
  97.         /// <summary>
  98.         /// Occurs when the ProgressBar's value has changed.
  99.         /// </summary>
  100.         /// <param name="e">ValueChangedEventArgs</param>
  101.         protected virtual void OnValueChanged(ValueChangedEventArgs e)
  102.         {
  103.             // Raise the event
  104.             if (ValueChanged != null)
  105.             {
  106.                 ValueChanged(this, e);
  107.             }
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Overrides the OnForeColorChanged event to set the current bar color to the ForeColor.
  112.         /// </summary>
  113.         /// <param name="e">EventArgs</param>
  114.         protected override void OnForeColorChanged(EventArgs e)
  115.         {
  116.             barColor = ForeColor;
  117.             base.OnForeColorChanged(e);
  118.         }
  119.  
  120.         /// <summary>
  121.         /// The maximum value.
  122.         /// </summary>
  123.         [Description("The maximum value."),
  124.         DefaultValue(100)]
  125.         public int Maximum
  126.         {
  127.             get { return maximum; }
  128.             set
  129.             {
  130.                 maximum = value;
  131.                 this.Invalidate();
  132.             }
  133.         }
  134.  
  135.         /// <summary>
  136.         /// The minimum value.
  137.         /// </summary>
  138.         [Description("The minimum value."),
  139.         DefaultValue(0)]
  140.         public int Minimum
  141.         {
  142.             get { return minimum; }
  143.             set
  144.             {
  145.                 minimum = value;
  146.                 this.Invalidate();
  147.             }
  148.         }
  149.  
  150.         /// <summary>
  151.         /// The current value.
  152.         /// <para>Note:</para>
  153.         /// <para>If the value is less than the Minimum, the value is set to the Minimum</para>
  154.         /// <para>If the value is greater than the Maximum, the value is set to the Maximum</para>
  155.         /// </summary>
  156.         [Description("The current value."),
  157.         DefaultValue(25)]
  158.         public int Value
  159.         {
  160.             get { return currentValue; }
  161.             set
  162.             {
  163.                 if (value >= minimum && value <= maximum)
  164.                 {
  165.                     currentValue = value;
  166.                 }
  167.                 else if (value > maximum)
  168.                 {
  169.                     currentValue = maximum;
  170.                 }
  171.                 else if (value < minimum)
  172.                 {
  173.                     currentValue = minimum;
  174.                 }
  175.  
  176.                 this.Invalidate();
  177.  
  178.                 OnValueChanged(new ValueChangedEventArgs(currentValue));
  179.             }
  180.         }
  181.  
  182.         /// <summary>
  183.         /// The border color.
  184.         /// <para>If set to Transparent, no border is drawn.</para>
  185.         /// </summary>
  186.         [Description("The border color."),
  187.         DefaultValue(typeof(Color), "Black")]
  188.         public Color BorderColor
  189.         {
  190.             get { return borderColor; }
  191.             set
  192.             {
  193.                 borderColor = value;
  194.                 this.Invalidate();
  195.             }
  196.         }
  197.  
  198.         /// <summary>
  199.         /// The border thickness.
  200.         /// <para>If set to 0, no border is drawn.</para>
  201.         /// </summary>
  202.         [Description("The border thickness"),
  203.         DefaultValue(2)]
  204.         public int BorderThickness
  205.         {
  206.             get { return borderThickness; }
  207.             set
  208.             {
  209.                 borderThickness = value;
  210.                 this.Invalidate();
  211.             }
  212.         }
  213.  
  214.         /// <summary>
  215.         /// The ProgressBar oritentation.
  216.         /// </summary>
  217.         [Description("The ProgressBar oritentation."),
  218.         DefaultValue(typeof(Orientation), "Vertical")]
  219.         public Orientation Orientation
  220.         {
  221.             get { return orientation; }
  222.             set
  223.             {
  224.                 orientation = value;
  225.                 this.Invalidate();
  226.             }
  227.         }
  228.  
  229.         /// <summary>
  230.         /// The color of the text drawn on the ProgressBar.
  231.         /// <para>If set to transparent, no text is drawn.</para>
  232.         /// </summary>
  233.         [Description("The color of the text drawn on the ProgressBar."),
  234.         DefaultValue(typeof(Color), "Black")]
  235.         public Color TextColor
  236.         {
  237.             get { return textColor; }
  238.             set
  239.             {
  240.                 textColor = value;
  241.                 this.Invalidate();
  242.             }
  243.         }
  244.  
  245.         /// <summary>
  246.         /// The way the text on the ProgressBar is drawn.
  247.         /// </summary>
  248.         [Description("The way the text on the ProgressBar is drawn."),
  249.         DefaultValue(typeof(TextStyleType), "Value")]
  250.         public TextStyleType TextStyle
  251.         {
  252.             get { return textStyle; }
  253.             set
  254.             {
  255.                 textStyle = value;
  256.                 this.Invalidate();
  257.             }
  258.         }
  259.  
  260.  
  261.  
  262.  
  263.         #region Smoothing properties
  264.  
  265.         /// <summary>
  266.         /// The ProgressBar's Graphic's CompositingMode.
  267.         /// </summary>
  268.         [Description("The ProgressBar's Graphic's CompositingMode."),
  269.         DefaultValue(typeof(CompositingMode), "SourceOver")]
  270.         public CompositingMode CompositingMode
  271.         {
  272.             get { return compositingMode; }
  273.             set
  274.             {
  275.                 compositingMode = value;
  276.                 this.Invalidate();
  277.             }
  278.         }
  279.         /// <summary>
  280.         /// The ProgressBar's Graphic's CompositingQuality.
  281.         /// </summary>
  282.         [Description("The ProgressBar's Graphic's CompositingQuality."),
  283.         DefaultValue(typeof(CompositingQuality), "HighQuality")]
  284.         public CompositingQuality CompositingQuality
  285.         {
  286.             get { return compositingQuality; }
  287.             set
  288.             {
  289.                 if (value != CompositingQuality.Invalid)
  290.                 {
  291.                     compositingQuality = value;
  292.                     this.Invalidate();
  293.                 }
  294.             }
  295.         }
  296.  
  297.         /// <summary>
  298.         /// The ProgressBar's Graphic's InterpolationMode.
  299.         /// </summary>
  300.         [Description("The ProgressBar's Graphic's InterpolationMode."),
  301.         DefaultValue(typeof(InterpolationMode), "HighQualityBicubic")]
  302.         public InterpolationMode InterpolationMode
  303.         {
  304.             get { return interpolationMode; }
  305.             set
  306.             {
  307.                 if (value != InterpolationMode.Invalid)
  308.                 {
  309.                     interpolationMode = value;
  310.                     this.Invalidate();
  311.                 }
  312.             }
  313.         }
  314.  
  315.         /// <summary>
  316.         /// The ProgressBar's Graphic's PixelOffsetMode.
  317.         /// </summary>
  318.         [Description("The ProgressBar's Graphic's PixelOffsetMode."),
  319.         DefaultValue(typeof(PixelOffsetMode), "HighQuality")]
  320.         public PixelOffsetMode PixelOffsetMode
  321.         {
  322.             get { return pixelOffsetMode; }
  323.             set
  324.             {
  325.                 if (value != PixelOffsetMode.Invalid)
  326.                 {
  327.                     pixelOffsetMode = value;
  328.                     this.Invalidate();
  329.                 }
  330.             }
  331.         }
  332.  
  333.         /// <summary>
  334.         /// The ProgressBar's Graphic's SmoothingMode.
  335.         /// </summary>
  336.         [Description("The ProgressBar's Graphic's SmoothingMode."),
  337.         DefaultValue(typeof(SmoothingMode), "HighQuality")]
  338.         public SmoothingMode SmoothingMode
  339.         {
  340.             get { return smoothingMode; }
  341.             set
  342.             {
  343.                 if (value != SmoothingMode.Invalid)
  344.                 {
  345.                     smoothingMode = value;
  346.                     this.Invalidate();
  347.                 }
  348.             }
  349.         }
  350.  
  351.         #endregion
  352.  
  353.  
  354.         /// <summary>
  355.         /// If any errors occur, this will contain the errors information.
  356.         /// HasErrors will be set to true if any errors have occured.
  357.         /// </summary>
  358.         [Description("If any errors occur, this will contain the errors information. " +
  359.             "HasErrors will be set to true if any errors have occured")]
  360.         public string ErrorLog
  361.         {
  362.             get { return errorLog; }
  363.         }
  364.  
  365.         /// <summary>
  366.         /// If any errors have occured, this will be set to true.
  367.         /// </summary>
  368.         [Description("If any errors have occured, this will be set to true")]
  369.         public bool HasErrors
  370.         {
  371.             get { return hasErrors; }
  372.         }
  373.  
  374.         /// <summary>
  375.         /// Clears any errors and sets HasErrors to false.
  376.         /// </summary>
  377.         public void ClearErrors()
  378.         {
  379.             errorLog = "";
  380.             hasErrors = false;
  381.         }
  382.  
  383.         /// <summary>
  384.         /// Draws the progress bar.
  385.         /// </summary>
  386.         /// <param name="pe">PaintEventArgs</param>
  387.         protected override void OnPaint(PaintEventArgs pe)
  388.         {
  389.             //base.OnPaint(e);
  390.  
  391.             try
  392.             {
  393.                 //Don't bother drawing if there's no surface area to work with
  394.                 if (this.Width <= 0 || this.Height <= 0)
  395.                 {
  396.                     return;
  397.                 }
  398.  
  399.                 pe.Graphics.CompositingMode = compositingMode;
  400.                 pe.Graphics.CompositingQuality = compositingQuality;
  401.                 pe.Graphics.InterpolationMode = interpolationMode;
  402.                 pe.Graphics.PixelOffsetMode = pixelOffsetMode;
  403.                 pe.Graphics.SmoothingMode = smoothingMode;
  404.  
  405.                 //Draw bar
  406.                 if (currentValue != 0)
  407.                 {
  408.                     using (SolidBrush brush = new SolidBrush(barColor))
  409.                     {
  410.                         if (orientation == Orientation.Vertical)
  411.                         {
  412.                             int scaledHeight = this.Height - Convert.ToInt32(((double)this.Height / maximum) * currentValue);
  413.                             pe.Graphics.FillRectangle(brush, 0, scaledHeight, this.Width, this.Height);
  414.                         }
  415.                         else
  416.                         {
  417.                             int scaledWidth = Convert.ToInt32(((double)this.Width / maximum) * currentValue);
  418.                             pe.Graphics.FillRectangle(brush, 0, 0, scaledWidth, this.Height);
  419.                         }
  420.                     }
  421.                 }
  422.  
  423.                 //Draw text
  424.                 if (textStyle != TextStyleType.None)
  425.                 {
  426.                     if (textColor != Color.Transparent)
  427.                     {
  428.                         using (Font font = new Font(this.Font.Name, this.Font.SizeInPoints, this.Font.Style, GraphicsUnit.Pixel))
  429.                         {
  430.                             string txt = null;
  431.  
  432.                             if (textStyle == TextStyleType.Value)
  433.                             {
  434.                                 txt = currentValue.ToString();
  435.                             }
  436.                             else if (textStyle == TextStyleType.ValueOverMaximum)
  437.                             {
  438.                                 txt = String.Format("{0}/{1}", currentValue, maximum);
  439.                             }
  440.                             else if (textStyle == TextStyleType.Percentage && maximum != 0)
  441.                             {
  442.                                 double p = Convert.ToDouble((100d / maximum) * Value);
  443.                                 txt = String.Format("{0}%", p);
  444.                             }
  445.                             else if (textStyle == TextStyleType.Text && !String.IsNullOrWhiteSpace(this.Text))
  446.                             {
  447.                                 txt = this.Text;
  448.                             }
  449.  
  450.                             if (txt != null)
  451.                             {
  452.                                 TextRenderer.DrawText(pe.Graphics, txt, font, new Rectangle(0, 0, this.Width, this.Height), textColor, flags);
  453.                             }
  454.  
  455.                         }
  456.                     }
  457.                 }
  458.  
  459.                 //Draw border
  460.                 if (borderThickness > 0)
  461.                 {
  462.                     if (borderColor != Color.Transparent)
  463.                     {
  464.                         using (Pen pen = new Pen(borderColor, borderThickness))
  465.                         {
  466.                             pe.Graphics.DrawRectangle(pen, 0, 0, this.Width, this.Height);
  467.                         }
  468.                     }
  469.                 }
  470.  
  471.             }
  472.             catch (Exception ex)
  473.             {
  474.                 errorLog += "Error in OnPaint event\n " +
  475.                     "Message: " + ex.Message + "\n" +
  476.                     "Type: " + ex.GetType().ToString() + "\n";
  477.  
  478.                 hasErrors = true;
  479.             }
  480.  
  481.         }
  482.  
  483.  
  484.     }
  485.  
  486.     #endregion
  487.  
  488.     #region ValueChangedEventArgs : EventArgs
  489.  
  490.     /// <summary>
  491.     /// Event arguments for the ValueChanged ProgressBar event
  492.     /// </summary>
  493.     public class ValueChangedEventArgs : EventArgs
  494.     {
  495.         /// <summary>
  496.         /// ValueChangedEventArgs
  497.         /// </summary>
  498.         /// <param name="currentValue">The current value of the ProgressBar</param>
  499.         public ValueChangedEventArgs(int currentValue)
  500.         {
  501.             this.Value = currentValue;
  502.         }
  503.  
  504.         /// <summary>
  505.         /// The current value of ProgressBar
  506.         /// </summary>
  507.         public int Value { get; set; }
  508.     }
  509.  
  510.     #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement