Advertisement
Yassine_Abbani

Advanced [Panel], [Custom Control]

May 23rd, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 20.72 KB | None | 0 0
  1. #region Derectives
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Windows.Forms;
  7. #endregion
  8.  
  9. #region Browse
  10. /* Copyright & Contact
  11. * --------------------------------------------------------------------------------<
  12. * Tool Name    : Panel                                                            *
  13. * From Project : Creator Eye                                                      *
  14. * Project Lang : C#                                                               *
  15. * Creator      : Yassine Abbani                                                   *
  16. * Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  17. * Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  18. * Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  19. * Version      : 1.3 Beta                                                         *
  20. * Color        : MultiColor + Gradient                                            *
  21. * Style        : Dark                                                             *
  22. *>--------------------------------------------------------------------------------<
  23. */
  24. /* Features
  25. * ------------------------
  26. * The interesting properties are:
  27. * Brush Type -> Change the background fill type (Solid Color, Hatch Fill, Linear Gradient, Path Gradient
  28. * Linear Gradient Angle -> Changes the gradient angle when Brush Type is set to Linear Gradient
  29. * Hatch  Style -> This is the hatch type when Brush Type is set to Hatch Fill
  30. *
  31. * Border Width -> Sets the thickness of the border
  32. * Border Color -> Sets the color of the border
  33. *
  34. * Back Color -> This is the usual Background Color
  35. * Second Back Color -> This is the second background color if the BrushType is a gradient, and the line color if BrushType is set to Hatch Fill
  36. * Radius -> this is the radius of the circle defining the corners
  37. * Image -> The image to display in Background
  38. * Image Layout -> Choosing the image display method
  39. * Texture Mode -> if Image Layout is set to Tile, this property sets the repeat mode of the image
  40. */
  41. /*  history
  42. * ------------------------
  43. * 1.1 (18 Jan 2018):
  44. * Add Gradients (from 2 to n colors, with some special effects).
  45. * Add Gradients Color Angle (colors will be Changed with  Costume Paint).
  46. * Add Gradients Transparent Color with Alpha Value.
  47. * Add Border  With Color and the Width property.
  48.  *
  49. * 1.2  (09 Mar 2018):
  50. * Remove Gradient transparent property.
  51. * Add Rounded rectangles - rounding edges of a rectangle.
  52. * Add Border  Styles with Costume property.
  53. *
  54. * 1.3 (20 May 2018):
  55. * Add Functionally that allows to Import Image Into Your Panel.
  56. * Add Functionally that allows to Located Your Image.  
  57. *
  58. */
  59. #endregion
  60.  
  61. #region Panel
  62. public class CePanel : Panel
  63. {
  64.  
  65.  
  66.     #region Variables
  67.     /// <summary>
  68.     /// Grahpic variables
  69.     /// </summary>
  70.     private GraphicsPath _RRBackground = GetRoundedRect(new Rectangle(0, 0, 49, 49), 1);
  71.     private Color _BackColor = Color.FromArgb(38, 38, 38);
  72.     private Color _SecondBackColor = Color.FromArgb(38, 38, 38);
  73.     private Color _BorderColor = Color.FromArgb(38, 38, 38);
  74.     private Border3DStyle _BorderStyle = Border3DStyle.Sunken;
  75.     private PenType __BrushType = PenType.LinearGradient;
  76.     private HatchStyle _HatchStyle = HatchStyle.BackwardDiagonal;
  77.     private Brush _BorderBrush = new SolidBrush(Color.FromArgb(38, 38, 38));
  78.     private Brush _BackroundBrush = new SolidBrush(Color.FromArgb(38, 38, 38));
  79.     private Brush _BackroundImageBrush = new SolidBrush(Color.FromArgb(38, 38, 38));
  80.     private int _Angle = 0;
  81.     private int _BorderWidth = 1;
  82.     private WrapMode _TextureMode = WrapMode.Tile;
  83.     private Image _Image = null;
  84.     private Image _BgImage = null;
  85.     private Rectangle _DisplayRectangle = new Rectangle(0, 0, 49, 49);
  86.     private ImageLayout _ImageLayout = ImageLayout.None;
  87.     private Blend _Blend = new Blend(4);
  88.     private float _Rayon = 4.0F;
  89.     private System.ComponentModel.IContainer components = null;
  90.     #endregion
  91.     #region EventArgs
  92.     protected override void OnResize(EventArgs eventargs)
  93.     {
  94.         try
  95.         {
  96.             base.OnResize(eventargs);
  97.             this._SetBox();
  98.             _SetBackGrounBrush();
  99.             _SetBrush();
  100.         }
  101.         catch { }
  102.     }
  103.     // Paint Background event =======================================================
  104.     protected override void OnPaintBackground(PaintEventArgs pevent)
  105.     {
  106.         base.OnPaintBackground(pevent);
  107.  
  108.         try
  109.         {
  110.             PanelPaint(pevent.Graphics);
  111.         }
  112.         catch { }
  113.     }
  114.     // Overirde of DisplayRectangle==================================================        
  115.     #endregion
  116.     #region Method
  117.     #region Constructor
  118.     public CePanel()
  119.     {
  120.         InitializeComponent();
  121.         base.DoubleBuffered = true;
  122.  
  123.     }
  124.     #region IContainer container
  125.     public CePanel(IContainer container)
  126.     {
  127.         container.Add(this);
  128.         InitializeComponent();
  129.         base.DoubleBuffered = true;
  130.     }
  131.     #endregion
  132.     #endregion
  133.     #region Destructor
  134.     protected override void Dispose(bool disposing)
  135.     {
  136.         if (disposing && (components != null))
  137.         {
  138.             components.Dispose();
  139.         }
  140.         base.Dispose(disposing);
  141.     }
  142.     #endregion
  143.     #region InitializeComponent
  144.     private void InitializeComponent()
  145.     {
  146.         this.SuspendLayout();
  147.         System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CePanel));
  148.         this.ResumeLayout(false);
  149.         base.BorderStyle = System.Windows.Forms.BorderStyle.None;
  150.         base.BackColor = Color.Transparent;
  151.  
  152.         this._Blend = new Blend(4);
  153.         this._Blend.Factors = new float[] { 1.0F, 0.99F, 0.01F, 0.0F };
  154.         this._Blend.Positions = new float[] { 0.0F, 0.49F, 0.51F, 1.0F };
  155.         this._SetBox();
  156.  
  157.     }
  158.  
  159.     #endregion
  160.     #endregion
  161.     #region Properties
  162.  
  163.     #region
  164.     [Browsable(false)]
  165.     protected override bool DoubleBuffered
  166.     {
  167.         get { return base.DoubleBuffered; }
  168.         set { }
  169.     }
  170.     #endregion
  171.     #region    Set Gradient Angle
  172.     [Browsable(true), Category("Gradient"), Description("Sets the Gradient Angle by Select Shape Type : LinearGradient")]
  173.     public int LinearGradientAngle
  174.     {
  175.         get { return _Angle; }
  176.         set
  177.         {
  178.             _Angle = value;
  179.             _SetBrush();
  180.             Refresh();
  181.         }
  182.     }
  183.     #endregion
  184.     #region Border Width
  185.     /// <summary>
  186.     /// Set The width of Border.
  187.     /// </summary>
  188.     [Browsable(true), Category("Border"), Description("Set The width of Border.")]
  189.     public int BorderWidth
  190.     {
  191.         get { return _BorderWidth; }
  192.         set
  193.         {
  194.  
  195.             _BorderWidth = value;
  196.             if (_BorderWidth < 1)
  197.                 _BorderWidth = 1;
  198.             if (_BorderWidth > 10)
  199.                 _BorderWidth = 10;
  200.  
  201.             _SetBrush();
  202.             Refresh();
  203.         }
  204.     }
  205.     #endregion
  206.     #region Border Color
  207.     [Browsable(true), Category("Border"), Description("Set The Color ")]
  208.     public Color BorderColor
  209.     {
  210.         get { return _BorderColor; }
  211.         set
  212.         {
  213.             _BorderColor = value;
  214.             _SetBrush();
  215.             Refresh();
  216.         }
  217.     }
  218.  
  219.     #endregion
  220.     #region Shape
  221.     /// <summary>
  222.     /// Gets or sets the style of the Panel.
  223.     /// </summary>
  224.     [Browsable(true), Category("Shape"), Description("Gets or sets the style of the Panel.")]
  225.     public PenType BrushType
  226.     {
  227.         get { return __BrushType; }
  228.         set
  229.         {
  230.             if (value == PenType.TextureFill) return;
  231.             __BrushType = value;
  232.             _SetBrush();
  233.             Refresh();
  234.         }
  235.     }
  236.     #endregion
  237.     #region Hatch
  238.     /// <summary>
  239.     /// Gets or sets the Hatch style of the Panel.
  240.     /// </summary>
  241.     [Browsable(true)]
  242.     public HatchStyle HatchStyle
  243.     {
  244.         get { return _HatchStyle; }
  245.         set
  246.         {
  247.  
  248.             _HatchStyle = value;
  249.             _SetBrush();
  250.             Refresh();
  251.         }
  252.     }
  253.     #endregion
  254.  
  255.     #region BackColor
  256.     public new Color BackColor
  257.     {
  258.         get { return _BackColor; }
  259.         set
  260.         {
  261.             _BackColor = value;
  262.             _SetBrush();
  263.             Refresh();
  264.         }
  265.     }
  266.     #endregion
  267.     #region Second BackColor
  268.     /// <summary>
  269.     /// Gets or sets begin gradient color.
  270.     /// </summary>
  271.     [Browsable(true), Category("Gradient"), Description("Gets or sets begin gradient color.")]
  272.     public Color BackColor2
  273.     {
  274.         get { return _SecondBackColor; }
  275.         set
  276.         {
  277.             _SecondBackColor = value;
  278.             _SetBrush();
  279.             Refresh();
  280.         }
  281.     }
  282.  
  283.     #endregion
  284.  
  285.     #region Rayon
  286.     [Browsable(true)]
  287.     public float Rayon
  288.     {
  289.         get { return _Rayon; }
  290.         set
  291.         {
  292.             _Rayon = value;
  293.             this._SetBox();
  294.             _SetBrush();
  295.             Refresh();
  296.         }
  297.     }
  298.     #endregion
  299.     #region WrapMode
  300.     [Browsable(true)]
  301.     public WrapMode TextureMode
  302.     {
  303.         get
  304.         {
  305.             return _TextureMode;
  306.         }
  307.         set
  308.         {
  309.             _TextureMode = value;
  310.             _SetBackGrounBrush();
  311.             _SetBrush();
  312.             Refresh();
  313.         }
  314.     }
  315.     #endregion
  316.     #region Image
  317.     [Browsable(true)]
  318.     public Image Image
  319.     {
  320.         get
  321.         {
  322.             return _Image;
  323.         }
  324.         set
  325.         {
  326.             _Image = value;
  327.             _SetBackGrounBrush();
  328.             _SetBrush();
  329.             Refresh();
  330.         }
  331.     }
  332.     [Browsable(true)]
  333.     public ImageLayout ImageLayout
  334.     {
  335.         get
  336.         {
  337.             return _ImageLayout;
  338.         }
  339.         set
  340.         {
  341.             _ImageLayout = value;
  342.             _SetBackGrounBrush();
  343.             _SetBrush();
  344.             Refresh();
  345.  
  346.         }
  347.     }
  348.     [Browsable(false)]
  349.     public override ImageLayout BackgroundImageLayout
  350.     {
  351.         get
  352.         {
  353.             return base.BackgroundImageLayout;
  354.         }
  355.         set
  356.         {
  357.         }
  358.     }
  359.     [Browsable(false)]
  360.     public override Image BackgroundImage
  361.     {
  362.         get
  363.         { return base.BackgroundImage; }
  364.         set
  365.         {
  366.             Image = value;
  367.         }
  368.     }
  369.  
  370.  
  371.     #endregion
  372.     #region Border3dStyle
  373.     [Browsable(true), Category("Border"), Description("The width of an edge.")]
  374.     public new Border3DStyle BorderStyle
  375.     {
  376.         get
  377.         { return _BorderStyle; }
  378.         set
  379.         {
  380.             _BorderStyle = value;
  381.             _SetBackGrounBrush();
  382.             _SetBrush();
  383.             Refresh();
  384.  
  385.         }
  386.     }
  387.     #endregion
  388.  
  389.  
  390.     private void _SetBox()
  391.     {
  392.         _RRBackground = GetRoundedRect(new Rectangle(0, 0, this.Width - 1, this.Height - 1), _Rayon);
  393.         int lRayon = (int)_Rayon;
  394.         _DisplayRectangle = new Rectangle(lRayon, lRayon, this.Width - 2 * lRayon, this.Height - 2 * lRayon);
  395.  
  396.     }
  397.  
  398.     private Brush _GetBackGroundBrush()
  399.     {
  400.         Brush lReturn = new SolidBrush(_BackColor);
  401.         switch (__BrushType)
  402.         {
  403.             case PenType.PathGradient:
  404.                 PathGradientBrush lPathGradientBrush = new PathGradientBrush(_RRBackground); ;
  405.  
  406.                 Color[] lColors = new Color[_RRBackground.PathPoints.Length];
  407.                 for (int iIndex = 0; iIndex < _RRBackground.PathPoints.Length; iIndex++)
  408.                 {
  409.                     lColors[iIndex] = _BackColor;
  410.                 }
  411.                 lPathGradientBrush.CenterColor = _SecondBackColor;
  412.                 lPathGradientBrush.SurroundColors = lColors;
  413.                 lReturn = lPathGradientBrush;
  414.                 break;
  415.             case PenType.LinearGradient:
  416.                 lReturn = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), _BackColor, _SecondBackColor, _Angle);
  417.                 break;
  418.             case PenType.HatchFill:
  419.                 lReturn = new HatchBrush(_HatchStyle, _SecondBackColor, _BackColor);
  420.                 break;
  421.         }
  422.         return lReturn;
  423.     }
  424.  
  425.     private Brush _GetImageBackGroundBrush()
  426.     {
  427.         Brush lReturn = _GetBackGroundBrush();
  428.         if (_Image != null)
  429.         {
  430.             Bitmap lBgImage = new Bitmap(Width, Height);
  431.             Graphics lGraph = Graphics.FromImage(lBgImage);
  432.             Rectangle lRectSource = new Rectangle(0, 0, _Image.Width, _Image.Height);
  433.             if (_BackColor != Color.Transparent)
  434.                 lGraph.FillRectangle(lReturn, 0, 0, Width, Height);
  435.             switch (_ImageLayout)
  436.             {
  437.                 case ImageLayout.None:
  438.                     lGraph.DrawImage(_Image, _Rayon, _Rayon, (float)(Width - 2 * _Rayon), (float)(Height - 2 * _Rayon));
  439.                     break;
  440.                 case ImageLayout.Stretch:
  441.                     lGraph.DrawImage(_Image, new Rectangle(0, 0, Width, Height));
  442.                     break;
  443.                 case ImageLayout.Center:
  444.                     //Rectangle lRectDest=  new Rectangle (, , _Image.Width,_Image.Height);
  445.                     lGraph.DrawImage(_Image, (lBgImage.Width - _Image.Width) / 2, (lBgImage.Height - _Image.Height) / 2);
  446.  
  447.                     break;
  448.                 case ImageLayout.Tile:
  449.                     lBgImage.Dispose();
  450.                     lBgImage = null;
  451.                     GC.Collect();
  452.                     lBgImage = new Bitmap(_Image);
  453.                     break;
  454.                 case ImageLayout.Zoom:
  455.                     float lHeightCoef = (float)Height / _Image.Height;
  456.                     float lWidthCoef = (float)Width / _Image.Width;
  457.                     int lWidth = 0;
  458.                     int lHeight = 0;
  459.                     int lX = 0;
  460.                     int lY = 0;
  461.                     if ((_Image.Width * lHeightCoef) > Width)
  462.                     {
  463.                         lWidth = (int)Width;
  464.                         lHeight = (int)(_Image.Height * lWidthCoef);
  465.                         lX = 0;
  466.                         lY = (int)(Height - lHeight) / 2;
  467.                     }
  468.                     else
  469.                     {
  470.                         lHeight = (int)Height;
  471.                         lWidth = (int)(_Image.Width * lHeightCoef);
  472.                         lX = (int)(Width - lWidth) / 2;
  473.                         lY = 0;
  474.                     }
  475.                     lGraph.DrawImage(_Image, new Rectangle(lX, lY, lWidth, lHeight));
  476.                     break;
  477.             }
  478.             lReturn.Dispose();
  479.             lReturn = new TextureBrush(lBgImage, _TextureMode);
  480.         }
  481.         return lReturn;
  482.     }
  483.  
  484.     private void _SetBrush()
  485.     {
  486.         try
  487.         {
  488.             _BackroundBrush = _GetBackGroundBrush();
  489.             _BackroundImageBrush = _GetImageBackGroundBrush();
  490.  
  491.             Color lDarkColor = ChangeLight(80, _BorderColor);
  492.             Color lLightColor = ChangeLight(170, _BorderColor);
  493.             float hyp = (float)Math.Sqrt(Height * Height + Width * Width);
  494.             float lAngle = (float)(Math.Acos(Height / hyp) * 180 / Math.PI);
  495.  
  496.             switch (_BorderStyle)
  497.             {
  498.                 case Border3DStyle.Flat:
  499.                     _BorderBrush = new SolidBrush(Color.Transparent);
  500.                     break;
  501.  
  502.                 default:
  503.                     _BorderBrush = new SolidBrush(_BorderColor);
  504.                     break;
  505.                 case Border3DStyle.Raised:
  506.                 case Border3DStyle.RaisedInner:
  507.                 case Border3DStyle.RaisedOuter:
  508.                     _BorderBrush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height)
  509.                                             , lDarkColor, lLightColor, lAngle);
  510.                     ((LinearGradientBrush)_BorderBrush).Blend = _Blend;
  511.                     break;
  512.                 case Border3DStyle.Sunken:
  513.                 case Border3DStyle.SunkenInner:
  514.                 case Border3DStyle.SunkenOuter:
  515.                     _BorderBrush = new LinearGradientBrush(new Rectangle(0, 0, Width, Height)
  516.                                             , lLightColor, lDarkColor, lAngle);
  517.                     ((LinearGradientBrush)_BorderBrush).Blend = _Blend;
  518.                     break;
  519.             }
  520.  
  521.         }
  522.         catch { }
  523.     }
  524.     private void _SetBackGrounBrush()
  525.     {
  526.  
  527.         this._SetBox();
  528.         Graphics lGraph;
  529.         _SetBrush();
  530.     }
  531.     protected void PanelPaint(Graphics pGraph)
  532.     {
  533.  
  534.  
  535.  
  536.         if (Color.Transparent != _BackColor)
  537.             pGraph.FillPath(new SolidBrush(_BackColor), _RRBackground);
  538.         //Appliquer la texture de Fond
  539.         pGraph.FillPath(_BackroundBrush, _RRBackground);
  540.         //Appliquer l'image
  541.         if (_Image != null)
  542.             pGraph.FillPath(_BackroundImageBrush, _RRBackground);
  543.         //Redessiner le Bord
  544.         pGraph.DrawPath(new Pen(_BorderBrush, _BorderWidth), _RRBackground);
  545.  
  546.     }
  547.     #endregion
  548.     #region Draw Control
  549.  
  550.     public static GraphicsPath GetCapsule(RectangleF baseRect)
  551.     {
  552.         float diameter;
  553.         RectangleF arc;
  554.         GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
  555.         try
  556.         {
  557.             if (baseRect.Width > baseRect.Height)
  558.             {
  559.                 // return horizontal capsule
  560.                 diameter = baseRect.Height;
  561.                 SizeF sizeF = new SizeF(diameter, diameter);
  562.                 arc = new RectangleF(baseRect.Location, sizeF);
  563.                 path.AddArc(arc, 90, 180);
  564.                 arc.X = baseRect.Right - diameter;
  565.                 path.AddArc(arc, 270, 180);
  566.             }
  567.             else if (baseRect.Width < baseRect.Height)
  568.             {
  569.                 // return vertical capsule
  570.                 diameter = baseRect.Width;
  571.                 SizeF sizeF = new SizeF(diameter, diameter);
  572.                 arc = new RectangleF(baseRect.Location, sizeF);
  573.                 path.AddArc(arc, 180, 180);
  574.                 arc.Y = baseRect.Bottom - diameter;
  575.                 path.AddArc(arc, 0, 180);
  576.             }
  577.             else
  578.             {
  579.                 // return circle
  580.                 path.AddEllipse(baseRect);
  581.             }
  582.         }
  583.         catch (Exception ex)
  584.         {
  585.             path.AddEllipse(baseRect);
  586.             Console.WriteLine(ex.Message);
  587.         }
  588.         finally
  589.         {
  590.             path.CloseFigure();
  591.         }
  592.         return path;
  593.     }
  594.  
  595.     public static GraphicsPath GetRoundedRect(RectangleF baseRect, float radius)
  596.     {
  597.         // if corner radius is less than or equal to zero,
  598.         // return the original rectangle
  599.         if (radius <= 0.0F)
  600.         {
  601.             GraphicsPath mPath = new GraphicsPath();
  602.             mPath.AddRectangle(baseRect);
  603.             mPath.CloseFigure();
  604.             return mPath;
  605.         }
  606.  
  607.         // if the corner radius is greater than or equal to
  608.         // half the width, or height (whichever is shorter)
  609.         // then return a capsule instead of a lozenge
  610.         if (radius >= (Math.Min(baseRect.Width, baseRect.Height)) / 2.0)
  611.             return GetCapsule(baseRect);
  612.  
  613.         // create the arc for the rectangle sides and declare
  614.         // a graphics path object for the drawing
  615.         float diameter = radius * 2.0F;
  616.         SizeF sizeF = new SizeF(diameter, diameter);
  617.         RectangleF arc = new RectangleF(baseRect.Location, sizeF);
  618.         GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
  619.  
  620.         // top left arc
  621.         path.AddArc(arc, 180, 90);
  622.  
  623.         // top right arc
  624.         arc.X = baseRect.Right - diameter;
  625.         path.AddArc(arc, 270, 90);
  626.  
  627.         // bottom right arc
  628.         arc.Y = baseRect.Bottom - diameter;
  629.         path.AddArc(arc, 0, 90);
  630.  
  631.         // bottom left arc
  632.         arc.X = baseRect.Left;
  633.         path.AddArc(arc, 90, 90);
  634.  
  635.         path.CloseFigure();
  636.         return path;
  637.     }
  638.  
  639.     /// <summary>
  640.     ///
  641.     /// </summary>
  642.     /// <param name="pCoef">Percentage (100 =no change)</param>
  643.     /// <param name="pColor"></param>
  644.     /// <returns></returns>
  645.     public static Color ChangeLight(float pCoef, Color pColor)
  646.     {
  647.         pCoef = pCoef / 100;
  648.         double dR = (double)pColor.R;
  649.         double dG = (double)pColor.G;
  650.         double dB = (double)pColor.B;
  651.         dR = dR * pCoef;
  652.         dG = dG * pCoef;
  653.         dB = dB * pCoef;
  654.  
  655.         if (dG > 255) dG = 255;
  656.         if (dR > 255) dR = 255;
  657.         if (dB > 255) dB = 255;
  658.         return Color.FromArgb((int)dR, (int)dG, (int)dB);
  659.     }
  660.  
  661.     public override Rectangle DisplayRectangle
  662.     {
  663.         get
  664.         {
  665.             return _DisplayRectangle;
  666.  
  667.         }
  668.     }
  669.  
  670.     #endregion
  671.  
  672. }
  673. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement