Advertisement
Yassine_Abbani

White [Track Bar], [Costume Control]

May 22nd, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.40 KB | None | 0 0
  1.  
  2. #region Import
  3. using System;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8. #endregion
  9. #region RoundRectangle
  10.  
  11. static class RoundRectangle
  12. {
  13.     public static GraphicsPath RoundRect(Rectangle Rectangle, int Curve)
  14.     {
  15.         GraphicsPath P = new GraphicsPath();
  16.         int ArcRectangleWidth = Curve * 2;
  17.         P.AddArc(new Rectangle(Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -180, 90);
  18.         P.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -90, 90);
  19.         P.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 0, 90);
  20.         P.AddArc(new Rectangle(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 90, 90);
  21.         P.AddLine(new Point(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y), new Point(Rectangle.X, Curve + Rectangle.Y));
  22.         return P;
  23.     }
  24.     public static GraphicsPath RoundRect(int X, int Y, int Width, int Height, int Curve)
  25.     {
  26.         Rectangle Rectangle = new Rectangle(X, Y, Width, Height);
  27.         GraphicsPath P = new GraphicsPath();
  28.         int ArcRectangleWidth = Curve * 2;
  29.         P.AddArc(new Rectangle(Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -180, 90);
  30.         P.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -90, 90);
  31.         P.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 0, 90);
  32.         P.AddArc(new Rectangle(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), 90, 90);
  33.         P.AddLine(new Point(Rectangle.X, Rectangle.Height - ArcRectangleWidth + Rectangle.Y), new Point(Rectangle.X, Curve + Rectangle.Y));
  34.         return P;
  35.     }
  36.     public static GraphicsPath RoundedTopRect(Rectangle Rectangle, int Curve)
  37.     {
  38.         GraphicsPath P = new GraphicsPath();
  39.         int ArcRectangleWidth = Curve * 2;
  40.         P.AddArc(new Rectangle(Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -180, 90);
  41.         P.AddArc(new Rectangle(Rectangle.Width - ArcRectangleWidth + Rectangle.X, Rectangle.Y, ArcRectangleWidth, ArcRectangleWidth), -90, 90);
  42.         P.AddLine(new Point(Rectangle.X + Rectangle.Width, Rectangle.Y + ArcRectangleWidth), new Point(Rectangle.X + Rectangle.Width, Rectangle.Y + Rectangle.Height - 1));
  43.         P.AddLine(new Point(Rectangle.X, Rectangle.Height - 1 + Rectangle.Y), new Point(Rectangle.X, Rectangle.Y + Curve));
  44.         return P;
  45.     }
  46. }
  47.  
  48. #endregion
  49. #region TrackBar_Old
  50.  
  51. [DefaultEvent("ValueChanged")]
  52. class Ce_TrackBar_Old : Control
  53. {
  54.  
  55.     #region Enums
  56.  
  57.     public enum ValueDivisor
  58.     {
  59.         By1 = 1,
  60.         By10 = 10,
  61.         By100 = 100,
  62.         By1000 = 1000
  63.     }
  64.  
  65.     #endregion
  66.     #region Variables
  67.  
  68.     private GraphicsPath PipeBorder;
  69.     private GraphicsPath TrackBarHandle;
  70.     private Rectangle TrackBarHandleRect;
  71.     private Rectangle ValueRect;
  72.     private LinearGradientBrush VlaueLGB;
  73.     private LinearGradientBrush TrackBarHandleLGB;
  74.     private bool Cap;
  75.  
  76.     private int ValueDrawer;
  77.     private int _Minimum = 0;
  78.     private int _Maximum = 10;
  79.     private int _Value = 0;
  80.     private Color _ValueColour = Color.FromArgb(224, 224, 224);
  81.     private bool _DrawHatch = true;
  82.     private bool _DrawValueString = false;
  83.     private bool _JumpToMouse = false;
  84.     private ValueDivisor DividedValue = ValueDivisor.By1;
  85.  
  86.     #endregion
  87.     #region Custom Properties
  88.  
  89.     public int Minimum
  90.     {
  91.         get { return _Minimum; }
  92.  
  93.         set
  94.         {
  95.             if (value >= _Maximum)
  96.                 value = _Maximum - 10;
  97.             if (_Value < value)
  98.                 _Value = value;
  99.  
  100.             _Minimum = value;
  101.             Invalidate();
  102.         }
  103.     }
  104.  
  105.     public int Maximum
  106.     {
  107.         get { return _Maximum; }
  108.  
  109.         set
  110.         {
  111.             if (value <= _Minimum)
  112.                 value = _Minimum + 10;
  113.             if (_Value > value)
  114.                 _Value = value;
  115.  
  116.             _Maximum = value;
  117.             Invalidate();
  118.         }
  119.     }
  120.  
  121.     public event ValueChangedEventHandler ValueChanged;
  122.     public delegate void ValueChangedEventHandler();
  123.     public int Value
  124.     {
  125.         get { return _Value; }
  126.         set
  127.         {
  128.             if (_Value != value)
  129.             {
  130.                 if (value < _Minimum)
  131.                 {
  132.                     _Value = _Minimum;
  133.                 }
  134.                 else
  135.                 {
  136.                     if (value > _Maximum)
  137.                     {
  138.                         _Value = _Maximum;
  139.                     }
  140.                     else
  141.                     {
  142.                         _Value = value;
  143.                     }
  144.                 }
  145.                 Invalidate();
  146.                 if (ValueChanged != null)
  147.                 {
  148.                     ValueChanged();
  149.                 }
  150.             }
  151.         }
  152.     }
  153.  
  154.     public ValueDivisor ValueDivison
  155.     {
  156.         get
  157.         {
  158.             return this.DividedValue;
  159.         }
  160.         set
  161.         {
  162.             this.DividedValue = value;
  163.             this.Invalidate();
  164.         }
  165.     }
  166.  
  167.     [Browsable(false)]
  168.     public float ValueToSet
  169.     {
  170.         get
  171.         {
  172.             return (float)(((double)this._Value) / ((double)this.DividedValue));
  173.         }
  174.         set
  175.         {
  176.             this.Value = (int)Math.Round((double)(value * ((float)this.DividedValue)));
  177.         }
  178.     }
  179.  
  180.     public Color ValueColour
  181.     {
  182.         get { return _ValueColour; }
  183.         set
  184.         {
  185.             _ValueColour = value;
  186.             Invalidate();
  187.         }
  188.     }
  189.  
  190.     public bool DrawHatch
  191.     {
  192.         get { return _DrawHatch; }
  193.         set
  194.         {
  195.             _DrawHatch = value;
  196.             Invalidate();
  197.         }
  198.     }
  199.  
  200.     public bool DrawValueString
  201.     {
  202.         get { return _DrawValueString; }
  203.         set
  204.         {
  205.             _DrawValueString = value;
  206.             if (_DrawValueString == true)
  207.             {
  208.                 Height = 40;
  209.             }
  210.             else
  211.             {
  212.                 Height = 22;
  213.             }
  214.             Invalidate();
  215.         }
  216.     }
  217.  
  218.     public bool JumpToMouse
  219.     {
  220.         get
  221.         {
  222.             return this._JumpToMouse;
  223.         }
  224.         set
  225.         {
  226.             this._JumpToMouse = value;
  227.         }
  228.     }
  229.  
  230.     #endregion
  231.     #region EventArgs
  232.  
  233.     protected override void OnMouseMove(MouseEventArgs e)
  234.     {
  235.         base.OnMouseMove(e);
  236.         if ((this.Cap && (e.X > -1)) && (e.X < (this.Width + 1)))
  237.         {
  238.             this.Value = this._Minimum + ((int)Math.Round((double)((this._Maximum - this._Minimum) * (((double)e.X) / ((double)this.Width)))));
  239.         }
  240.     }
  241.  
  242.     protected override void OnMouseDown(MouseEventArgs e)
  243.     {
  244.         base.OnMouseDown(e);
  245.         if (e.Button == MouseButtons.Left)
  246.         {
  247.             this.ValueDrawer = (int)Math.Round((double)((((double)(this._Value - this._Minimum)) / ((double)(this._Maximum - this._Minimum))) * (this.Width - 11)));
  248.             this.TrackBarHandleRect = new Rectangle(this.ValueDrawer, 0, 10, 20);
  249.             this.Cap = this.TrackBarHandleRect.Contains(e.Location);
  250.             if (this._JumpToMouse)
  251.             {
  252.                 this.Value = this._Minimum + ((int)Math.Round((double)((this._Maximum - this._Minimum) * (((double)e.X) / ((double)this.Width)))));
  253.             }
  254.         }
  255.     }
  256.  
  257.     protected override void OnMouseUp(MouseEventArgs e)
  258.     {
  259.         base.OnMouseUp(e);
  260.         this.Cap = false;
  261.     }
  262.  
  263.  
  264.     #endregion
  265.  
  266.     public Ce_TrackBar_Old()
  267.     {
  268.         SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer, true);
  269.  
  270.         _DrawHatch = true;
  271.         Size = new Size(80, 22);
  272.         MinimumSize = new Size(37, 22);
  273.     }
  274.  
  275.     protected override void OnResize(EventArgs e)
  276.     {
  277.         base.OnResize(e);
  278.         if (_DrawValueString == true)
  279.         {
  280.             Height = 40;
  281.         }
  282.         else
  283.         {
  284.             Height = 22;
  285.         }
  286.     }
  287.  
  288.     protected override void OnPaint(PaintEventArgs e)
  289.     {
  290.         base.OnPaint(e);
  291.         Graphics G = e.Graphics;
  292.         HatchBrush Hatch = new HatchBrush(HatchStyle.WideDownwardDiagonal, Color.FromArgb(20, Color.Black), Color.Transparent);
  293.         G.Clear(Parent.BackColor);
  294.         G.SmoothingMode = SmoothingMode.AntiAlias;
  295.         checked
  296.         {
  297.             this.PipeBorder = RoundRectangle.RoundRect(1, 6, this.Width - 3, 8, 3);
  298.             try
  299.             {
  300.                 this.ValueDrawer = (int)Math.Round(unchecked(checked((double)(this._Value - this._Minimum) / (double)(this._Maximum - this._Minimum)) * (double)checked(this.Width - 11)));
  301.             }
  302.             catch (Exception)
  303.             {
  304.             }
  305.             this.TrackBarHandleRect = new Rectangle(this.ValueDrawer, 0, 10, 20);
  306.             G.SetClip(this.PipeBorder);
  307.             this.ValueRect = new Rectangle(1, 7, this.TrackBarHandleRect.X + this.TrackBarHandleRect.Width - 2, 7);
  308.             this.VlaueLGB = new LinearGradientBrush(this.ValueRect, this._ValueColour, this._ValueColour, 90f);
  309.             G.FillRectangle(this.VlaueLGB, this.ValueRect);
  310.  
  311.             if (_DrawHatch == true)
  312.             {
  313.                 G.FillRectangle(Hatch, this.ValueRect);
  314.             }
  315.  
  316.             G.ResetClip();
  317.             G.SmoothingMode = SmoothingMode.AntiAlias;
  318.             G.DrawPath(new Pen(Color.FromArgb(180, 180, 180)), this.PipeBorder);
  319.             this.TrackBarHandle = RoundRectangle.RoundRect(this.TrackBarHandleRect, 3);
  320.             this.TrackBarHandleLGB = new LinearGradientBrush(this.ClientRectangle, SystemColors.Control, SystemColors.Control, 90f);
  321.             G.FillPath(this.TrackBarHandleLGB, this.TrackBarHandle);
  322.             G.DrawPath(new Pen(Color.FromArgb(180, 180, 180)), this.TrackBarHandle);
  323.  
  324.             if (_DrawValueString == true)
  325.             {
  326.                 G.DrawString(System.Convert.ToString(ValueToSet), Font, Brushes.Gray, 0, 25);
  327.             }
  328.         }
  329.     }
  330. }
  331.  
  332. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement