Yassine_Abbani

Subscribe [Button], [Social Media], [Custom Control]

May 24th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 31.38 KB | None | 0 0
  1. #region Directives
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Imaging;
  8. using System.Drawing.Text;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. #endregion
  15. #region Browse
  16. /* Copyright & Contact
  17. * --------------------------------------------------------------------------------<
  18. * Tool Name    : Button                                                           *
  19. * From Project : Creator Eye                                                      *
  20. * Project Lang : C#                                                               *
  21. * Creator      : Yassine Abbani                                                   *
  22. * Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  23. * Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  24. * Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  25. * Version      : 1.0 Beta                                                         *
  26. * Color        : Red, Blue                                                        *
  27. * Style        : youtube Subscriber                                               *
  28. *>--------------------------------------------------------------------------------<
  29. */
  30. /* Features
  31. * ------------------------
  32.  * We Was Converting rbg color to Html color
  33.  * Creators Eye Cards is a custom control that arranges your info or controls inside a beautiful panel. It makes your dashboard presentation more organized and aligned.
  34. */
  35. /*  history
  36. * ------------------------
  37. * 1.0 (24 Feb 2018):
  38. *
  39. * Custom Properties
  40.  * Ability to add image to the left side of the card using property
  41.  * Ability to change the color of the card image using Color property
  42. */
  43. #endregion
  44.     public sealed class HelperMethods
  45.     {
  46.  
  47.         #region MouseStates
  48.  
  49.         /// <summary>
  50.         /// The helper enumerator to get mouse states.
  51.         /// </summary>
  52.         public enum MouseMode : byte
  53.         {
  54.             Normal,
  55.             Hovered,
  56.             Pushed,
  57.             Disabled
  58.         }
  59.  
  60.         #endregion
  61.  
  62.         #region Draw Methods
  63.  
  64.         /// <summary>
  65.         /// The Method to draw the image from encoded base64 string.
  66.         /// </summary>
  67.         /// <param name="g">The Graphics to draw the image.</param>
  68.         /// <param name="base64Image">The Encoded base64 image.</param>
  69.         /// <param name="rect">The Rectangle area for the image.</param>
  70.         public void DrawImageFromBase64(Graphics g, string base64Image, Rectangle rect)
  71.         {
  72.             Image im = null;
  73.             using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(base64Image)))
  74.             {
  75.                 im = Image.FromStream(ms);
  76.                 ms.Close();
  77.             }
  78.             g.DrawImage(im, rect);
  79.         }
  80.  
  81.         /// <summary>
  82.         /// The Method to fill rounded rectangle.
  83.         /// </summary>
  84.         /// <param name="g">The Graphics to draw the image.</param>
  85.         /// <param name="c">The Color to the rectangle area.</param>
  86.         /// <param name="rect">The Rectangle area to be filled.</param>
  87.         /// <param name="curve">The Rounding border radius.</param>
  88.         /// <param name="topLeft">Wether the top left of rectangle be round or not.</param>
  89.         /// <param name="topRight">Wether the top right of rectangle be round or not.</param>
  90.         /// <param name="bottomLeft">Wether the bottom left of rectangle be round or not.</param>
  91.         /// <param name="bottomRight">Wether the bottom right of rectangle be round or not.</param>
  92.         public void FillRoundedPath(Graphics g, Color c, Rectangle rect, int curve, bool topLeft = true,
  93.             bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
  94.         {
  95.             g.FillPath(new SolidBrush(c), RoundRec(rect, curve, topLeft, topRight, bottomLeft, bottomRight));
  96.         }
  97.  
  98.         /// <summary>
  99.         /// The Method to fill the rounded rectangle.
  100.         /// </summary>
  101.         /// <param name="g">The Graphics to fill the rectangle.</param>
  102.         /// <param name="b">The brush to the rectangle area.</param>
  103.         /// <param name="rect">The Rectangle area to be filled.</param>
  104.         /// <param name="curve">The Rounding border radius.</param>
  105.         /// <param name="topLeft">Wether the top left of rectangle be round or not.</param>
  106.         /// <param name="topRight">Wether the top right of rectangle be round or not.</param>
  107.         /// <param name="bottomLeft">Wether the bottom left of rectangle be round or not.</param>
  108.         /// <param name="bottomRight">Wether the bottom right of rectangle be round or not.</param>
  109.         public void FillRoundedPath(Graphics g, Brush b, Rectangle rect, int curve, bool topLeft = true,
  110.             bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
  111.         {
  112.             g.FillPath(b, RoundRec(rect, curve, topLeft, topRight, bottomLeft, bottomRight));
  113.         }
  114.  
  115.         /// <summary>
  116.         /// The Method to fill the rectangle the base color and surrounding with another color(Rectangle with shadow).
  117.         /// </summary>
  118.         /// <param name="g">The Graphics to fill the rectangle.</param>
  119.         /// <param name="centerColor">The Center color of the rectangle area.</param>
  120.         /// <param name="surroundColor">The Inner Surround color of the rectangle area.</param>
  121.         /// <param name="p">The Point of the surrounding color.</param>
  122.         /// <param name="rect">The Rectangle area to be filled.</param>
  123.         /// <param name="curve">The Rounding border radius.</param>
  124.         /// <param name="topLeft">Wether the top left of rectangle be round or not.</param>
  125.         /// <param name="topRight">Wether the top right of rectangle be round or not.</param>
  126.         /// <param name="bottomLeft">Wether the bottom left of rectangle be round or not.</param>
  127.         /// <param name="bottomRight">Wether the bottom right of rectangle be round or not.</param>
  128.         public void FillWithInnerRectangle(Graphics g, Color centerColor, Color surroundColor, Point p, Rectangle rect,
  129.             int curve, bool topLeft = true, bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
  130.         {
  131.             using (
  132.                 PathGradientBrush pgb =
  133.                     new PathGradientBrush(RoundRec(rect, curve, topLeft, topRight, bottomLeft, bottomRight)))
  134.             {
  135.  
  136.                 pgb.CenterColor = centerColor;
  137.                 pgb.SurroundColors = new Color[] { surroundColor };
  138.                 pgb.FocusScales = p;
  139.                 GraphicsPath gp = new GraphicsPath { FillMode = FillMode.Winding };
  140.                 gp.AddRectangle(rect);
  141.                 g.FillPath(pgb, gp);
  142.                 gp.Dispose();
  143.             }
  144.         }
  145.  
  146.         /// <summary>
  147.         /// The Method to fill the circle the base color and surrounding with another color(Rectangle with shadow).
  148.         /// </summary>
  149.         /// <param name="g">The Graphics to fill the circle.</param>
  150.         /// <param name="centerColor">The Center color of the rectangle area.</param>
  151.         /// <param name="surroundColor">The Inner Surround color of the rectangle area.</param>
  152.         /// <param name="p">The Point of the surrounding color.</param>
  153.         /// <param name="rect">The circle area to be filled.</param>
  154.         public void FillWithInnerEllipse(Graphics g, Color centerColor, Color surroundColor, Point p, Rectangle rect)
  155.         {
  156.             GraphicsPath gp = new GraphicsPath { FillMode = FillMode.Winding };
  157.             gp.AddEllipse(rect);
  158.             using (PathGradientBrush pgb = new PathGradientBrush(gp))
  159.             {
  160.                 pgb.CenterColor = centerColor;
  161.                 pgb.SurroundColors = new Color[] { surroundColor };
  162.                 pgb.FocusScales = p;
  163.                 g.FillPath(pgb, gp);
  164.                 gp.Dispose();
  165.             }
  166.         }
  167.  
  168.         /// <summary>
  169.         /// The Method to fill the rounded rectangle the base color and surrounding with another color(Rectangle with shadow).
  170.         /// </summary>
  171.         /// <param name="g">The Graphics to fill rounded the rectangle.</param>
  172.         /// <param name="centerColor">The Center color of the rectangle area.</param>
  173.         /// <param name="surroundColor">The Inner Surround color of the rectangle area.</param>
  174.         /// <param name="p">The Point of the surrounding color.</param>
  175.         /// <param name="rect">The Rectangle area to be filled.</param>
  176.         /// <param name="curve">The Rounding border radius.</param>
  177.         /// <param name="topLeft">Wether the top left of rectangle be round or not.</param>
  178.         /// <param name="topRight">Wether the top right of rectangle be round or not.</param>
  179.         /// <param name="bottomLeft">Wether the bottom left of rectangle be round or not.</param>
  180.         /// <param name="bottomRight">Wether the bottom right of rectangle be round or not.</param>
  181.         public void FillWithInnerRoundedPath(Graphics g, Color centerColor, Color surroundColor, Point p, Rectangle rect,
  182.             int curve, bool topLeft = true, bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
  183.         {
  184.             using (
  185.                 PathGradientBrush pgb =
  186.                     new PathGradientBrush(RoundRec(rect, curve, topLeft, topRight, bottomLeft, bottomRight)))
  187.             {
  188.                 pgb.CenterColor = centerColor;
  189.                 pgb.SurroundColors = new Color[] { surroundColor };
  190.                 pgb.FocusScales = p;
  191.                 g.FillPath(pgb, RoundRec(rect, curve, topLeft, topRight, bottomLeft, bottomRight));
  192.             }
  193.         }
  194.  
  195.         /// <summary>
  196.         /// The Method to draw the rounded rectangle area.
  197.         /// </summary>
  198.         /// <param name="g">The Graphics to draw rounded the rectangle.</param>
  199.         /// <param name="c">Border Color</param>
  200.         /// <param name="size">Border thickness</param>
  201.         /// <param name="rect">The Rectangle area to be drawn.</param>
  202.         /// <param name="curve">The Rounding border radius.</param>
  203.         /// <param name="topLeft">Wether the top left of rectangle be round or not.</param>
  204.         /// <param name="topRight">Wether the top right of rectangle be round or not.</param>
  205.         /// <param name="bottomLeft">Wether the bottom left of rectangle be round or not.</param>
  206.         /// <param name="bottomRight">Wether the bottom right of rectangle be round or not.</param>
  207.         public void DrawRoundedPath(Graphics g, Color c, float size, Rectangle rect, int curve, bool topLeft = true,
  208.             bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
  209.         {
  210.             g.DrawPath(new Pen(c, size), RoundRec(rect, curve, topLeft, topRight, bottomLeft, bottomRight));
  211.         }
  212.  
  213.         /// <summary>
  214.         /// The method to draw the triangle.
  215.         /// </summary>
  216.         /// <param name="g">The Graphics to draw triangle.</param>
  217.         /// <param name="c">The Triangle Color.</param>
  218.         /// <param name="size">The Triangle thickness</param>
  219.         /// <param name="P1">Point 1</param>
  220.         /// <param name="P2">Point 2</param>
  221.         /// <param name="P3">Point 3</param>
  222.         /// <param name="P4">Point 4</param>
  223.         /// <param name="P5">Point 5</param>
  224.         /// <param name="P6">Point 6</param>
  225.         public void DrawTriangle(Graphics g, Color c, int size, Point p10, Point p11, Point p20, Point p21, Point p30,
  226.             Point p31)
  227.         {
  228.             g.DrawLine(new Pen(c, size), p10, p11);
  229.             g.DrawLine(new Pen(c, size), p20, p21);
  230.             g.DrawLine(new Pen(c, size), p30, p31);
  231.         }
  232.  
  233.         /// <summary>
  234.         /// The Method to fill the rectangle with border.
  235.         /// </summary>
  236.         /// <param name="g">The Graphics to fill the the rectangle.</param>
  237.         /// <param name="rect">The Rectangle to fill.</param>
  238.         /// <param name="rectColor">The Rectangle color.</param>
  239.         /// <param name="StrokeColor">The Stroke(Border) color.</param>
  240.         /// <param name="strokeSize">The Stroke thickness.</param>
  241.         public void FillStrokedRectangle(Graphics g, Rectangle rect, Color rectColor, Color stroke, int strokeSize = 1)
  242.         {
  243.             using (SolidBrush b = new SolidBrush(rectColor))
  244.             using (Pen s = new Pen(stroke, strokeSize))
  245.             {
  246.                 g.FillRectangle(b, rect);
  247.                 g.DrawRectangle(s, rect);
  248.             }
  249.  
  250.         }
  251.  
  252.         /// <summary>
  253.         /// The Method to fill rounded rectangle with border.
  254.         /// </summary>
  255.         /// <param name="g">The Graphics to fill rounded the rectangle.</param>
  256.         /// <param name="rect">The Rectangle to fill.</param>
  257.         /// <param name="rectColor">The Rectangle color.</param>
  258.         /// <param name="StrokeColor">The Stroke(Border) color.</param>
  259.         /// <param name="strokeSize">The Stroke thickness.</param>
  260.         /// <param name="Curve">The Rounding border radius.</param>
  261.         /// <param name="topLeft">Wether the top left of rectangle be round or not.</param>
  262.         /// <param name="topRight">Wether the top right of rectangle be round or not.</param>
  263.         /// <param name="bottomLeft">Wether the bottom left of rectangle be round or not.</param>
  264.         /// <param name="bottomRight">Wether the bottom right of rectangle be round or not.</param>
  265.         public void FillRoundedStrokedRectangle(Graphics g, Rectangle rect, Color rectColor, Color stroke,
  266.             int strokeSize = 1, int curve = 1, bool topLeft = true, bool topRight = true, bool bottomLeft = true,
  267.             bool bottomRight = true)
  268.         {
  269.             using (SolidBrush b = new SolidBrush(rectColor))
  270.             {
  271.                 using (Pen s = new Pen(stroke, strokeSize))
  272.                 {
  273.                     FillRoundedPath(g, b, rect, curve, topLeft, topRight, bottomLeft, bottomRight);
  274.                     DrawRoundedPath(g, stroke, strokeSize, rect, curve, topLeft, topRight, bottomLeft, bottomRight);
  275.                 }
  276.             }
  277.         }
  278.  
  279.         /// <summary>
  280.         /// The Method to draw the image with custom color.
  281.         /// </summary>
  282.         /// <param name="g"> The Graphic to draw the image.</param>
  283.         /// <param name="r"> The Rectangle area of image.</param>
  284.         /// <param name="image"> The image that the custom color applies on it.</param>
  285.         /// <param name="c">The Color that be applied to the image.</param>
  286.         /// <remarks></remarks>
  287.         public void DrawImageWithColor(Graphics g, Rectangle r, Image image, Color c)
  288.         {
  289.             float[][] ptsArray = new float[][]
  290.             {
  291.                 new float[] {Convert.ToSingle(c.R/255.0), 0f, 0f, 0f, 0f},
  292.                 new float[] {0f, Convert.ToSingle(c.G/255.0), 0f, 0f, 0f},
  293.                 new float[] {0f, 0f, Convert.ToSingle(c.B/255.0), 0f, 0f},
  294.                 new float[] {0f, 0f, 0f, Convert.ToSingle(c.A/255.0), 0f},
  295.                 new float[]
  296.                 {
  297.                     Convert.ToSingle(c.R/255.0),
  298.                     Convert.ToSingle(c.G/255.0),
  299.                     Convert.ToSingle(c.B/255.0), 0f,
  300.                     Convert.ToSingle(c.A/255.0)
  301.                 }
  302.             };
  303.             ImageAttributes imgAttribs = new ImageAttributes();
  304.             imgAttribs.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Default);
  305.             g.DrawImage(image, r, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imgAttribs);
  306.             image.Dispose();
  307.         }
  308.  
  309.  
  310.         /// <summary>
  311.         /// The Method to draw the image with custom color.
  312.         /// </summary>
  313.         /// <param name="g"> The Graphic to draw the image.</param>
  314.         /// <param name="r"> The Rectangle area of image.</param>
  315.         /// <param name="image"> The Encoded base64 image that the custom color applies on it.</param>
  316.         /// <param name="c">The Color that be applied to the image.</param>
  317.         /// <remarks></remarks>
  318.         public void DrawImageWithColor(Graphics g, Rectangle r, string image, Color c)
  319.         {
  320.             Image im = ImageFromBase64(image);
  321.             float[][] ptsArray = new float[][]
  322.             {
  323.                 new float[] {Convert.ToSingle(c.R/255.0), 0f, 0f, 0f, 0f},
  324.                 new float[] {0f, Convert.ToSingle(c.G/255.0), 0f, 0f, 0f},
  325.                 new float[] {0f, 0f, Convert.ToSingle(c.B/255.0), 0f, 0f},
  326.                 new float[] {0f, 0f, 0f, Convert.ToSingle(c.A/255.0), 0f},
  327.                 new float[]
  328.                 {
  329.                     Convert.ToSingle(c.R/255.0),
  330.                     Convert.ToSingle(c.G/255.0),
  331.                     Convert.ToSingle(c.B/255.0), 0f,
  332.                     Convert.ToSingle(c.A/255.0)
  333.                 }
  334.             };
  335.             ImageAttributes imgAttribs = new ImageAttributes();
  336.             imgAttribs.SetColorMatrix(new ColorMatrix(ptsArray), ColorMatrixFlag.Default, ColorAdjustType.Default);
  337.             g.DrawImage(im, r, 0, 0, im.Width, im.Height, GraphicsUnit.Pixel, imgAttribs);
  338.         }
  339.  
  340.         #endregion
  341.  
  342.         #region Shapes
  343.  
  344.         /// <summary>
  345.         /// The Triangle that joins 3 points to the triangle shape.
  346.         /// </summary>
  347.         /// <param name="p1">Point 1.</param>
  348.         /// <param name="p2">Point 2.</param>
  349.         /// <param name="p3">Point 3.</param>
  350.         /// <returns>The Trangle shape based on given points.</returns>
  351.         public Point[] Triangle(Point p1, Point p2, Point p3)
  352.         {
  353.             return new Point[]
  354.             {
  355.                 p1,
  356.                 p2,
  357.                 p3
  358.             };
  359.         }
  360.  
  361.         #endregion
  362.  
  363.         #region Brushes
  364.  
  365.         /// <summary>
  366.         /// The Brush with two colors one center another surounding the center based on the given rectangle area.
  367.         /// </summary>
  368.         /// <param name="centerColor">The Center color of the rectangle.</param>
  369.         /// <param name="surroundColor">The Surrounding color of the rectangle.</param>
  370.         /// <param name="p">The Point of surrounding.</param>
  371.         /// <param name="rect">The Rectangle of the brush.</param>
  372.         /// <returns>The Brush with two colors one center another surounding the center.</returns>
  373.         public static PathGradientBrush GlowBrush(Color centerColor, Color surroundColor, Point p, Rectangle rect)
  374.         {
  375.             GraphicsPath gp = new GraphicsPath { FillMode = FillMode.Winding };
  376.             gp.AddRectangle(rect);
  377.             return new PathGradientBrush(gp)
  378.             {
  379.                 CenterColor = centerColor,
  380.                 SurroundColors = new Color[] { surroundColor },
  381.                 FocusScales = p
  382.             };
  383.         }
  384.  
  385.         /// <summary>
  386.         /// The Brush from RGBA color.
  387.         /// </summary>
  388.         /// <param name="r">Red.</param>
  389.         /// <param name="g">Green.</param>
  390.         /// <param name="b">Blue.</param>
  391.         /// <param name="a">Alpha.</param>
  392.         /// <returns>The Brush from given RGBA color.</returns>
  393.         public SolidBrush SolidBrushRgbColor(int r, int g, int b, int a = 0)
  394.         {
  395.             return new SolidBrush(Color.FromArgb(a, r, g, b));
  396.         }
  397.  
  398.         /// <summary>
  399.         /// The Brush from HEX color.
  400.         /// </summary>
  401.         /// <param name="cWithoutHash">HEX Color without hash.</param>
  402.         /// <returns>The Brush from given HEX color.</returns>
  403.         public SolidBrush SolidBrushHtMlColor(string cWithoutHash)
  404.         {
  405.             return new SolidBrush(GetHtmlColor(cWithoutHash));
  406.         }
  407.  
  408.         #endregion
  409.  
  410.         #region Pens
  411.  
  412.         /// <summary>
  413.         /// The Pen from RGBA color.
  414.         /// </summary>
  415.         /// <param name="r">Red.</param>
  416.         /// <param name="g">Green.</param>
  417.         /// <param name="b">Blue.</param>
  418.         /// <param name="a">Alpha.</param>
  419.         /// <returns>The Pen from given RGBA color.</returns>
  420.         public Pen PenRgbColor(int r, int g, int b, int a, float size)
  421.         {
  422.             return new Pen(Color.FromArgb(a, r, g, b), size);
  423.         }
  424.  
  425.         /// <summary>
  426.         /// The Pen from HEX color.
  427.         /// </summary>
  428.         /// <param name="cWithoutHash">HEX Color without hash.</param>
  429.         /// <param name="size">The Size of the pen.</param>
  430.         /// <returns></returns>
  431.         public Pen PenHtMlColor(string cWithoutHash, float size = 1)
  432.         {
  433.             return new Pen(GetHtmlColor(cWithoutHash), size);
  434.         }
  435.  
  436.         #endregion
  437.  
  438.         #region Colors
  439.  
  440.         /// <summary>
  441.         ///
  442.         /// </summary>
  443.         /// <param name="cWithoutHash"></param>
  444.         /// <returns></returns>
  445.         public Color GetHtmlColor(string cWithoutHash)
  446.         {
  447.             return ColorTranslator.FromHtml("#" + cWithoutHash);
  448.         }
  449.  
  450.         /// <summary>
  451.         /// The Color from HEX by alpha property.
  452.         /// </summary>
  453.         /// <param name="alpha">Alpha.</param>
  454.         /// <param name="cWithoutHash">HEX Color without hash.</param>
  455.         /// <returns>The Color from HEX with given ammount of transparency</returns>
  456.         public Color GetAlphaHtmlColor(int alpha, string cWithoutHash)
  457.         {
  458.             return Color.FromArgb(alpha, ColorTranslator.FromHtml("#" + cWithoutHash));
  459.         }
  460.  
  461.         #endregion
  462.  
  463.         #region Methods
  464.  
  465.         /// <summary>
  466.         /// The String format to provide the alignment.
  467.         /// </summary>
  468.         /// <param name="horizontal">Horizontal alignment.</param>
  469.         /// <param name="vertical">Horizontal alignment. alignment.</param>
  470.         /// <returns>The String format.</returns>
  471.         public StringFormat SetPosition(StringAlignment horizontal = StringAlignment.Center,
  472.             StringAlignment vertical = StringAlignment.Center)
  473.         {
  474.             return new StringFormat
  475.             {
  476.                 Alignment = horizontal,
  477.                 LineAlignment = vertical
  478.             };
  479.         }
  480.  
  481.         /// <summary>
  482.         /// The Matrix array of single from color.
  483.         /// </summary>
  484.         /// <param name="c">The Color.</param>
  485.         /// <returns>The Matrix array of single from the given color</returns>
  486.         public float[][] ColorToMatrix(Color c)
  487.         {
  488.             return new float[][]
  489.             {
  490.                 new float[]
  491.                 {
  492.                     Convert.ToSingle(c.R/255),
  493.                     0,
  494.                     0,
  495.                     0,
  496.                     0
  497.                 },
  498.                 new float[]
  499.                 {
  500.                     0,
  501.                     Convert.ToSingle(c.G/255),
  502.                     0,
  503.                     0,
  504.                     0
  505.                 },
  506.                 new float[]
  507.                 {
  508.                     0,
  509.                     0,
  510.                     Convert.ToSingle(c.B/255),
  511.                     0,
  512.                     0
  513.                 },
  514.                 new float[]
  515.                 {
  516.                     0,
  517.                     0,
  518.                     0,
  519.                     Convert.ToSingle(c.A/255),
  520.                     0
  521.                 },
  522.                 new float[]
  523.                 {
  524.                     Convert.ToSingle(c.R/255),
  525.                     Convert.ToSingle(c.G/255),
  526.                     Convert.ToSingle(c.B/255),
  527.                     0f,
  528.                     Convert.ToSingle(c.A/255)
  529.                 }
  530.             };
  531.         }
  532.  
  533.         /// <summary>
  534.         /// The Image from encoded base64 image.
  535.         /// </summary>
  536.         /// <param name="base64Image">The Encoded base64 image</param>
  537.         /// <returns>The Image from encoded base64.</returns>
  538.         public Image ImageFromBase64(string base64Image)
  539.         {
  540.             using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(base64Image)))
  541.             {
  542.                 return Image.FromStream(ms);
  543.             }
  544.         }
  545.  
  546.  
  547.         #endregion
  548.  
  549.         #region Round Border
  550.  
  551.         /// <summary>
  552.         /// Credits : AeonHack
  553.         /// </summary>
  554.  
  555.         public GraphicsPath RoundRec(Rectangle r, int curve, bool topLeft = true, bool topRight = true,
  556.             bool bottomLeft = true, bool bottomRight = true)
  557.         {
  558.             GraphicsPath createRoundPath = new GraphicsPath(FillMode.Winding);
  559.             if (topLeft)
  560.             {
  561.                 createRoundPath.AddArc(r.X, r.Y, curve, curve, 180f, 90f);
  562.             }
  563.             else
  564.             {
  565.                 createRoundPath.AddLine(r.X, r.Y, r.X, r.Y);
  566.             }
  567.             if (topRight)
  568.             {
  569.                 createRoundPath.AddArc(r.Right - curve, r.Y, curve, curve, 270f, 90f);
  570.             }
  571.             else
  572.             {
  573.                 createRoundPath.AddLine(r.Right - r.Width, r.Y, r.Width, r.Y);
  574.             }
  575.             if (bottomRight)
  576.             {
  577.                 createRoundPath.AddArc(r.Right - curve, r.Bottom - curve, curve, curve, 0f, 90f);
  578.             }
  579.             else
  580.             {
  581.                 createRoundPath.AddLine(r.Right, r.Bottom, r.Right, r.Bottom);
  582.  
  583.             }
  584.             if (bottomLeft)
  585.             {
  586.                 createRoundPath.AddArc(r.X, (r.Bottom + r.Y) - curve, curve, curve, 90f, 90f);
  587.             }
  588.             else
  589.             {
  590.                 createRoundPath.AddLine(r.X, r.Bottom, r.X, r.Bottom);
  591.             }
  592.             createRoundPath.CloseFigure();
  593.             return createRoundPath;
  594.         }
  595.  
  596.         #endregion
  597.  
  598.     }
  599.     public class Ce_SubscribeButton : Control
  600.     {
  601.  
  602.         #region  Declarations
  603.  
  604.         private static readonly HelperMethods H = new HelperMethods();
  605.         private string _leftText;
  606.         private string _rightText;
  607.         private Rectangle _redPart;
  608.         private Rectangle _lightPart;
  609.         private HelperMethods.MouseMode _state;
  610.         private IStyle _style;
  611.  
  612.         #endregion
  613.  
  614.         #region  Constructors
  615.  
  616.         public Ce_SubscribeButton()
  617.         {
  618.             SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
  619.             DoubleBuffered = true;
  620.             UpdateStyles();
  621.             BackColor = Color.Transparent;
  622.             Font = new Font("Segoe UI", 10);
  623.             _style = IStyle.Red;
  624.             _state = HelperMethods.MouseMode.Normal;
  625.             _leftText = "12M";
  626.             _rightText = "Subscribe";
  627.         }
  628.  
  629.         #endregion
  630.  
  631.         #region  Events
  632.  
  633.         protected override void OnMouseDown(MouseEventArgs e)
  634.         {
  635.             base.OnMouseDown(e);
  636.             _state = HelperMethods.MouseMode.Pushed;
  637.             Cursor = Cursors.Hand;
  638.             Invalidate();
  639.         }
  640.  
  641.         protected override void OnMouseHover(EventArgs e)
  642.         {
  643.             base.OnMouseHover(e);
  644.             _state = HelperMethods.MouseMode.Hovered;
  645.             Cursor = Cursors.Hand;
  646.             Invalidate();
  647.         }
  648.  
  649.         protected override void OnMouseLeave(EventArgs e)
  650.         {
  651.             base.OnMouseLeave(e);
  652.             _state = HelperMethods.MouseMode.Normal;
  653.             Cursor = Cursors.Default;
  654.             Invalidate();
  655.         }
  656.  
  657.         protected override void OnMouseUp(MouseEventArgs e)
  658.         {
  659.             base.OnMouseUp(e);
  660.             _state = HelperMethods.MouseMode.Hovered;
  661.             Cursor = Cursors.Hand;
  662.             Invalidate();
  663.         }
  664.  
  665.  
  666.         #endregion
  667.  
  668.         #region  Properties
  669.  
  670.         [Category("Custom"), Description("Gets or sets the style for the control.")]
  671.         public IStyle Style
  672.         {
  673.             get { return _style; }
  674.             set
  675.             {
  676.                 _style = value;
  677.                 Invalidate();
  678.             }
  679.         }
  680.  
  681.         [Category("Custom"), Description("Gets or sets the text of the left side of the control.")]
  682.         public string LeftText
  683.         {
  684.             get { return _leftText; }
  685.             set
  686.             {
  687.                 _leftText = value;
  688.                 Invalidate();
  689.             }
  690.         }
  691.  
  692.         [Category("Custom"), Description("Gets or sets the text of the right side of the control.")]
  693.         public string RightText
  694.         {
  695.             get { return _rightText; }
  696.             set
  697.             {
  698.                 _rightText = value;
  699.                 Invalidate();
  700.             }
  701.         }
  702.  
  703.         #endregion
  704.  
  705.         #region  Draw Control
  706.  
  707.         protected override void OnPaint(PaintEventArgs e)
  708.         {
  709.             Graphics g = e.Graphics;
  710.             Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);
  711.             g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  712.             g.SmoothingMode = SmoothingMode.AntiAlias;
  713.             _redPart = new Rectangle(0, 0, Width / 2, Height);
  714.             _lightPart = new Rectangle(Width / 2, 0, Width - Width / 2, Height);
  715.  
  716.             switch (_state)
  717.             {
  718.  
  719.                 case HelperMethods.MouseMode.Normal:
  720.                     g.FillPath(Style == IStyle.Red ? Brushes.Red : Brushes.Green, H.RoundRec(_redPart, 6, true, false, true, false));
  721.                     g.FillRectangle(Brushes.Silver, _lightPart);
  722.                     g.DrawLine(Pens.Silver, Convert.ToInt32(Width / 2), 0, Convert.ToInt32(Width - 1), 0);
  723.                     g.DrawLine(Pens.Silver, Convert.ToInt32(Width / 2), Height - 1, Width, Height - 1);
  724.                     g.DrawLine(Pens.Silver, Width - 1, 0, Width - 1, Height);
  725.                     g.DrawString(RightText, Font, Brushes.White, _redPart, H.SetPosition());
  726.                     g.DrawString(LeftText, Font, Brushes.Gray, _lightPart, H.SetPosition());
  727.                     break;
  728.                 case HelperMethods.MouseMode.Hovered:
  729.                     g.FillPath(Style == IStyle.Red ? Brushes.Red : Brushes.Green, H.RoundRec(_redPart, 6, true, false, true, false));
  730.                     g.FillRectangle(Brushes.Silver, _lightPart);
  731.                     g.DrawLine(Pens.Silver, Convert.ToInt32(Width / 2), 0, Convert.ToInt32(Width - 1), 0);
  732.                     g.DrawLine(Pens.Silver, Convert.ToInt32(Width / 2), Height - 1, Width, Height - 1);
  733.                     g.DrawLine(Pens.Silver, Width - 1, 0, Width - 1, Height);
  734.                     g.DrawString(RightText, Font, Brushes.White, _redPart, H.SetPosition());
  735.                     g.DrawString(LeftText, Font, Brushes.Gray, _lightPart, H.SetPosition());
  736.                     break;
  737.                 case HelperMethods.MouseMode.Pushed:
  738.                     g.FillPath(Style == IStyle.Red ? Brushes.DarkRed : Brushes.DarkGreen, H.RoundRec(_redPart, 6, true, false, true, false));
  739.                     g.FillRectangle(Brushes.Silver, _lightPart);
  740.                     g.DrawLine(Pens.Silver, Convert.ToInt32(Width / 2), 0, Convert.ToInt32(Width - 1), 0);
  741.                     g.DrawLine(Pens.Silver, Convert.ToInt32(Width / 2), Height - 1, Width, Height - 1);
  742.                     g.DrawLine(Pens.Silver, Width - 1, 0, Width - 1, Height);
  743.                     g.DrawString(RightText, Font, Brushes.White, _redPart, H.SetPosition());
  744.                     g.DrawString(LeftText, Font, Brushes.Gray, _lightPart, H.SetPosition());
  745.                     break;
  746.             }
  747.  
  748.         }
  749.  
  750.         #endregion
  751.  
  752.         #region  Enumerators
  753.  
  754.         public enum IStyle
  755.         {
  756.             Green,
  757.             Red
  758.         }
  759.  
  760.         #endregion
  761.  
  762.     }
Add Comment
Please, Sign In to add comment