Advertisement
Guest User

Untitled

a guest
Jun 26th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.43 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Collections;
  5. using System.Windows.Forms;
  6. using System.ComponentModel;
  7. using System.Drawing.Drawing2D;
  8.  
  9. namespace Launcher {    
  10.     [DefaultEvent("ValueChanged")]
  11.     public class ProgressBar : System.Windows.Forms.UserControl {
  12.         private System.ComponentModel.Container components = null;
  13.         public ProgressBar() {
  14.             InitializeComponent();
  15.             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  16.             this.SetStyle(ControlStyles.DoubleBuffer, true);
  17.             this.SetStyle(ControlStyles.ResizeRedraw, true);
  18.             this.SetStyle(ControlStyles.Selectable, true);
  19.             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  20.             this.SetStyle(ControlStyles.UserPaint, true);
  21.             this.BackColor = Color.Transparent;
  22.             if (!InDesignMode()) {
  23.                 mGlowAnimation.Tick += new EventHandler(mGlowAnimation_Tick);
  24.                 mGlowAnimation.Interval = 15;
  25.                 if (Value < Maximum) { mGlowAnimation.Start(); }
  26.             }
  27.         }
  28.  
  29.         protected override void Dispose(bool disposing) {
  30.             if (disposing) {
  31.                 if (components != null) {
  32.                     components.Dispose();
  33.                 }
  34.             }
  35.             base.Dispose(disposing);
  36.         }
  37.  
  38.         private void InitializeComponent() {
  39.             this.SuspendLayout();
  40.             this.Name = "ProgressBar";
  41.             this.Size = new System.Drawing.Size(264, 32);
  42.             this.Load += new System.EventHandler(this.ProgressBar_Load);
  43.             this.Paint += new System.Windows.Forms.PaintEventHandler(this.ProgressBar_Paint);
  44.             this.ResumeLayout(false);
  45.         }
  46.  
  47.         private int mGlowPosition = -325;
  48.         private Timer mGlowAnimation = new Timer();
  49.         private int mValue = 0;
  50.  
  51.         [Category("Value"),
  52.         DefaultValue(0),
  53.         Description("The value that is displayed on the progress bar.")]
  54.         public int Value {
  55.             get { return mValue; }
  56.             set {
  57.                 if (value > Maximum || value < MinValue) { return; }
  58.                 mValue = value;
  59.                 if (value < Maximum) { mGlowAnimation.Start(); }
  60.                 if (value == Maximum) { mGlowAnimation.Stop(); }
  61.                 ValueChangedHandler vc = ValueChanged;
  62.                 if (vc != null) { vc(this, new System.EventArgs()); }
  63.                 this.Invalidate();
  64.             }
  65.         }
  66.  
  67.         private int mMaxValue = 100;
  68.         [Category("Value"),
  69.         DefaultValue(100),
  70.         Description("The maximum value for the Value property.")]
  71.         public int Maximum {
  72.             get { return mMaxValue; }
  73.             set {
  74.                 mMaxValue = value;
  75.                 if (value > Maximum) { Value = Maximum; }
  76.                 if (Value < Maximum) { mGlowAnimation.Start(); }
  77.                 MaxChangedHandler mc = MaxChanged;
  78.                 if (mc != null) { mc(this, new System.EventArgs()); }
  79.                 this.Invalidate();
  80.             }
  81.         }
  82.  
  83.         private int mMinValue = 0;
  84.         [Category("Value"),
  85.         DefaultValue(0),
  86.         Description("The minimum value for the Value property.")]
  87.         public int MinValue {
  88.             get { return mMinValue; }
  89.             set {
  90.                 mMinValue = value;
  91.                 if (value < MinValue) { Value = MinValue; }
  92.                 MinChangedHandler mc = MinChanged;
  93.                 if (mc != null) { mc(this, new System.EventArgs()); }
  94.                 this.Invalidate();
  95.             }
  96.         }
  97.  
  98.         private Color mStartColor = Color.FromArgb(210, 0, 0);
  99.         [Category("Bar"),
  100.         DefaultValue(typeof(Color), "210, 0, 0"),
  101.         Description("The start color for the progress bar." +
  102.                     "210, 000, 000 = Red\n" +
  103.                     "210, 202, 000 = Yellow\n" +
  104.                     "000, 163, 211 = Blue\n" +
  105.                     "000, 211, 040 = Green\n")]
  106.         public Color StartColor {
  107.             get { return mStartColor; }
  108.             set { mStartColor = value; this.Invalidate(); }
  109.         }
  110.  
  111.         private Color mEndColor = Color.FromArgb(0, 211, 40);
  112.         [Category("Bar"),
  113.         DefaultValue(typeof(Color), "0, 211, 40"),
  114.         Description("The end color for the progress bar." +
  115.             "210, 000, 000 = Red\n" +
  116.             "210, 202, 000 = Yellow\n" +
  117.             "000, 163, 211 = Blue\n" +
  118.             "000, 211, 040 = Green\n")]
  119.         public Color EndColor {
  120.             get { return mEndColor; }
  121.             set { mEndColor = value; this.Invalidate(); }
  122.         }
  123.  
  124.         private Color mHighlightColor = Color.White;
  125.         [Category("Highlights and Glows"),
  126.         DefaultValue(typeof(Color), "White"),
  127.         Description("The color of the highlights.")]
  128.         public Color HighlightColor {
  129.             get { return mHighlightColor; }
  130.             set { mHighlightColor = value; this.Invalidate(); }
  131.         }
  132.  
  133.         private Color mBackgroundColor = Color.FromArgb(201, 201, 201);
  134.         /// <summary>
  135.         /// The color of the background.
  136.         /// </summary>
  137.         [Category("Highlights and Glows"),
  138.         DefaultValue(typeof(Color), "201,201,201"),
  139.         Description("The color of the background.")]
  140.         public Color BackgroundColor {
  141.             get { return mBackgroundColor; }
  142.             set { mBackgroundColor = value; this.Invalidate(); }
  143.         }
  144.  
  145.         private bool mAnimate = true;
  146.         /// <summary>
  147.         /// Whether the glow is animated.
  148.         /// </summary>
  149.         [Category("Highlights and Glows"),
  150.         DefaultValue(typeof(bool), "true"),
  151.         Description("Whether the glow is animated or not.")]
  152.         public bool Animate {
  153.             get { return mAnimate; }
  154.             set {
  155.                 mAnimate = value;
  156.                 if (value) { mGlowAnimation.Start(); } else { mGlowAnimation.Stop(); }
  157.                 this.Invalidate();
  158.             }
  159.         }
  160.  
  161.         private Color mGlowColor = Color.FromArgb(150, 255, 255, 255);
  162.         /// <summary>
  163.         /// The color of the glow.
  164.         /// </summary>
  165.         [Category("Highlights and Glows"),
  166.         DefaultValue(typeof(Color), "150, 255, 255, 255"),
  167.         Description("The color of the glow.")]
  168.         public Color GlowColor {
  169.             get { return mGlowColor; }
  170.             set { mGlowColor = value; this.Invalidate(); }
  171.         }
  172.  
  173.         #region -  Drawing  -
  174.  
  175.         private void DrawBackground(Graphics g) {
  176.             Rectangle r = this.ClientRectangle; r.Width--; r.Height--;
  177.             GraphicsPath rr = RoundRect(r, 2, 2, 2, 2);
  178.             g.FillPath(new SolidBrush(this.BackgroundColor), rr);
  179.         }
  180.  
  181.         private void DrawBackgroundShadows(Graphics g) {
  182.             Rectangle lr = new Rectangle(2, 2, 10, this.Height - 5);
  183.             LinearGradientBrush lg = new LinearGradientBrush(lr, Color.FromArgb(30, 0, 0, 0),
  184.                                                              Color.Transparent,
  185.                                                              LinearGradientMode.Horizontal);
  186.             lr.X--;
  187.             g.FillRectangle(lg, lr);
  188.  
  189.             Rectangle rr = new Rectangle(this.Width - 12, 2, 10, this.Height - 5);
  190.             LinearGradientBrush rg = new LinearGradientBrush(rr, Color.Transparent,
  191.                                                              Color.FromArgb(20, 0, 0, 0),
  192.                                                              LinearGradientMode.Horizontal);
  193.             g.FillRectangle(rg, rr);
  194.         }
  195.  
  196.         private void DrawBar(Graphics g) {
  197.             Rectangle r = new Rectangle(1, 2, this.Width - 3, this.Height - 3);
  198.             r.Width = (int)(Value * 1.0F / (Maximum - MinValue) * this.Width);
  199.             g.FillRectangle(new SolidBrush(GetIntermediateColor()), r);
  200.         }
  201.  
  202.         private void DrawBarShadows(Graphics g) {
  203.             Rectangle lr = new Rectangle(1, 2, 15, this.Height - 3);
  204.             LinearGradientBrush lg = new LinearGradientBrush(lr, Color.White, Color.White,
  205.                                                              LinearGradientMode.Horizontal);
  206.  
  207.             ColorBlend lc = new ColorBlend(3);
  208.             lc.Colors = new Color[] { Color.Transparent, Color.FromArgb(40, 0, 0, 0), Color.Transparent };
  209.             lc.Positions = new float[] { 0.0F, 0.2F, 1.0F };
  210.             lg.InterpolationColors = lc;
  211.  
  212.             lr.X--;
  213.             g.FillRectangle(lg, lr);
  214.  
  215.             Rectangle rr = new Rectangle(this.Width - 3, 2, 15, this.Height - 3);
  216.             rr.X = (int)(Value * 1.0F / (Maximum - MinValue) * this.Width) - 14;
  217.             LinearGradientBrush rg = new LinearGradientBrush(rr, Color.Black, Color.Black,
  218.                                                              LinearGradientMode.Horizontal);
  219.  
  220.             ColorBlend rc = new ColorBlend(3);
  221.             rc.Colors = new Color[] { Color.Transparent, Color.FromArgb(40, 0, 0, 0), Color.Transparent };
  222.             rc.Positions = new float[] { 0.0F, 0.8F, 1.0F };
  223.             rg.InterpolationColors = rc;
  224.  
  225.             g.FillRectangle(rg, rr);
  226.         }
  227.  
  228.         private void DrawHighlight(Graphics g) {
  229.             Rectangle tr = new Rectangle(1, 1, this.Width - 1, 6);
  230.             GraphicsPath tp = RoundRect(tr, 2, 2, 0, 0);
  231.  
  232.             g.SetClip(tp);
  233.             LinearGradientBrush tg = new LinearGradientBrush(tr, Color.White,
  234.                                                              Color.FromArgb(128, Color.White),
  235.                                                              LinearGradientMode.Vertical);
  236.             g.FillPath(tg, tp);
  237.             g.ResetClip();
  238.  
  239.             Rectangle br = new Rectangle(1, this.Height - 8, this.Width - 1, 6);
  240.             GraphicsPath bp = RoundRect(br, 0, 0, 2, 2);
  241.  
  242.             g.SetClip(bp);
  243.             LinearGradientBrush bg = new LinearGradientBrush(br, Color.Transparent,
  244.                                                              Color.FromArgb(100, this.HighlightColor),
  245.                                                              LinearGradientMode.Vertical);
  246.             g.FillPath(bg, bp);
  247.             g.ResetClip();
  248.         }
  249.  
  250.         private void DrawInnerStroke(Graphics g) {
  251.             Rectangle r = this.ClientRectangle;
  252.             r.X++; r.Y++; r.Width -= 3; r.Height -= 3;
  253.             GraphicsPath rr = RoundRect(r, 2, 2, 2, 2);
  254.             g.DrawPath(new Pen(Color.FromArgb(100, Color.White)), rr);
  255.         }
  256.  
  257.         private void DrawGlow(Graphics g) {
  258.             /*Rectangle r = new Rectangle(mGlowPosition, 0, 60, this.Height);
  259.             LinearGradientBrush lgb = new LinearGradientBrush(r, Color.White, Color.White,
  260.                                                               LinearGradientMode.Horizontal);
  261.  
  262.             ColorBlend cb = new ColorBlend(4);
  263.             cb.Colors = new Color[] {Color.Transparent, this.GlowColor, this.GlowColor, Color.Transparent};
  264.             cb.Positions = new float[] {0.0F, 0.5F, 0.6F, 1.0F};
  265.             lgb.InterpolationColors = cb;
  266.  
  267.             Rectangle clip = new Rectangle(1, 2, this.Width - 3, this.Height - 3);
  268.             clip.Width = (int)(Value * 1.0F / (MaxValue - MinValue) * this.Width);
  269.             g.SetClip(clip);
  270.             g.FillRectangle(lgb,r);
  271.             g.ResetClip();*/
  272.         }
  273.  
  274.         private void DrawOuterStroke(Graphics g) {
  275.             Rectangle r = this.ClientRectangle; r.Width--; r.Height--;
  276.             GraphicsPath rr = RoundRect(r, 2, 2, 2, 2);
  277.             g.DrawPath(new Pen(Color.FromArgb(178, 178, 178)), rr);
  278.         }
  279.  
  280.         #endregion
  281.  
  282.         #region -  Functions  -
  283.  
  284.         private GraphicsPath RoundRect(RectangleF r, float r1, float r2, float r3, float r4) {
  285.             float x = r.X, y = r.Y, w = r.Width, h = r.Height;
  286.             GraphicsPath rr = new GraphicsPath();
  287.             rr.AddBezier(x, y + r1, x, y, x + r1, y, x + r1, y);
  288.             rr.AddLine(x + r1, y, x + w - r2, y);
  289.             rr.AddBezier(x + w - r2, y, x + w, y, x + w, y + r2, x + w, y + r2);
  290.             rr.AddLine(x + w, y + r2, x + w, y + h - r3);
  291.             rr.AddBezier(x + w, y + h - r3, x + w, y + h, x + w - r3, y + h, x + w - r3, y + h);
  292.             rr.AddLine(x + w - r3, y + h, x + r4, y + h);
  293.             rr.AddBezier(x + r4, y + h, x, y + h, x, y + h - r4, x, y + h - r4);
  294.             rr.AddLine(x, y + h - r4, x, y + r1);
  295.             return rr;
  296.         }
  297.  
  298.         private bool InDesignMode() {
  299.             return (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
  300.         }
  301.  
  302.         private Color GetIntermediateColor() {
  303.             Color c = this.StartColor;
  304.             Color c2 = this.EndColor;
  305.  
  306.             float pc = this.Value * 1.0F / (this.Maximum - this.MinValue);
  307.  
  308.             int ca = c.A, cr = c.R, cg = c.G, cb = c.B;
  309.             int c2a = c2.A, c2r = c2.R, c2g = c2.G, c2b = c2.B;
  310.  
  311.             int a = (int)Math.Abs(ca + (ca - c2a) * pc);
  312.             int r = (int)Math.Abs(cr - ((cr - c2r) * pc));
  313.             int g = (int)Math.Abs(cg - ((cg - c2g) * pc));
  314.             int b = (int)Math.Abs(cb - ((cb - c2b) * pc));
  315.  
  316.             if (a > 255) { a = 255; }
  317.             if (r > 255) { r = 255; }
  318.             if (g > 255) { g = 255; }
  319.             if (b > 255) { b = 255; }
  320.  
  321.             return (Color.FromArgb(a, r, g, b));
  322.         }
  323.  
  324.         #endregion
  325.  
  326.         #region -  Other  -
  327.  
  328.         private void ProgressBar_Paint(object sender, PaintEventArgs e) {
  329.             e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  330.             e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  331.             DrawBackground(e.Graphics);
  332.             DrawBackgroundShadows(e.Graphics);
  333.             DrawBar(e.Graphics);
  334.             DrawBarShadows(e.Graphics);
  335.             DrawHighlight(e.Graphics);
  336.             DrawInnerStroke(e.Graphics);
  337.             DrawGlow(e.Graphics);
  338.             DrawOuterStroke(e.Graphics);
  339.         }
  340.  
  341.         private void mGlowAnimation_Tick(object sender, EventArgs e) {
  342.             if (this.Animate) {
  343.                 mGlowPosition += 4;
  344.                 if (mGlowPosition > this.Width) {
  345.                     mGlowPosition = -300;
  346.                 }
  347.                 this.Invalidate();
  348.             } else {
  349.                 mGlowAnimation.Stop();
  350.                 mGlowPosition = -320;
  351.             }
  352.         }
  353.  
  354.         #endregion
  355.  
  356.         #region -  Events  -
  357.  
  358.         /// <summary>
  359.         /// When the Value property is changed.
  360.         /// </summary>
  361.         public delegate void ValueChangedHandler(object sender, EventArgs e);
  362.         /// <summary>
  363.         /// When the Value property is changed.
  364.         /// </summary>
  365.         public event ValueChangedHandler ValueChanged;
  366.  
  367.         /// <summary>
  368.         /// When the MinValue property is changed.
  369.         /// </summary>
  370.         public delegate void MinChangedHandler(object sender, EventArgs e);
  371.         /// <summary>
  372.         /// When the MinValue property is changed.
  373.         /// </summary>
  374.         public event MinChangedHandler MinChanged;
  375.  
  376.         /// <summary>
  377.         /// When the MaxValue property is changed.
  378.         /// </summary>
  379.         public delegate void MaxChangedHandler(object sender, EventArgs e);
  380.         /// <summary>
  381.         /// When the MaxValue property is changed.
  382.         /// </summary>
  383.         public event MaxChangedHandler MaxChanged;
  384.  
  385.         #endregion
  386.  
  387.         private void ProgressBar_Load(object sender, EventArgs e) {
  388.  
  389.         }
  390.  
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement