Advertisement
Yassine_Abbani

Emboss [Label], [Costume Control]

May 11th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.20 KB | None | 0 0
  1.     #region Imports
  2.  
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Text;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. using System.IO;
  14. using System.Drawing.Imaging;
  15. using System.Linq;
  16.  
  17. #endregion
  18.     #region Copyright & contact
  19. // Creator : Yassine Abbani
  20. // Facebook :  https://www.facebook.com/yassineabbani.user
  21. // Youtube  :  https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw
  22. // website  :  CheatEye
  23. // Pastebin  : https://pastebin.com/u/Yassine_Abbani
  24. #endregion
  25.    #region EmbossLabel
  26. public enum EmbossDirection { TopLeft, TopRight, BottomLeft, BottomRight };
  27. public enum EmbossStyle { ShadowCast, Extrude };
  28. class Ce_EmbossLabel : Control
  29. {
  30.     private int _dropAmount = 5;
  31.     private int _textContrast = 4;
  32.     // This needs to be global because the GFX will be reassigned
  33.     // and I need like to retain the quality in the UpdateGraphicsBuffer method
  34.     private bool _isHighQuality = true;
  35.     private EmbossStyle _embossStyle = EmbossStyle.Extrude;
  36.     private TextRenderingHint _textRenderingHint = TextRenderingHint.AntiAliasGridFit;
  37.     private BufferedGraphics _bufGraphics;
  38.     private readonly BufferedGraphicsContext _bufContext = BufferedGraphicsManager.Current;
  39.     private SolidBrush _surfaceBrush;
  40.     private SolidBrush _embossBrush = new SolidBrush(Color.Black);
  41.     private Size _textSize;
  42.     private EmbossDirection _embossDirection = EmbossDirection.BottomRight;
  43.     private ContentAlignment _textAlign = ContentAlignment.MiddleLeft;
  44.  
  45.     public Ce_EmbossLabel()
  46.     {
  47.         this.Font = new Font("Tahoma", 30f);
  48.         // You Can Chance The ForeColor
  49.         this.ForeColor = Color.YellowGreen;
  50.  
  51.         this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
  52.             ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  53.  
  54.         UpdateGraphicsBuffer();
  55.     }
  56.  
  57.     protected override void OnPaint(PaintEventArgs e)
  58.     {
  59.         _bufGraphics.Graphics.Clear(this.BackColor);
  60.         Render();
  61.         _bufGraphics.Render(e.Graphics);
  62.     }
  63.  
  64.     protected override void OnResize(EventArgs e)
  65.     {
  66.         base.OnResize(e);
  67.         UpdateGraphicsBuffer();
  68.         this.Update();
  69.     }
  70.  
  71.     private void UpdateGraphicsBuffer()
  72.     {
  73.         // We cannot create a bitmap if any of these values are 0
  74.         if (this.Size.Width > 0 && this.Size.Height > 0)
  75.         {
  76.             _bufContext.MaximumBuffer = new Size(this.Width + 1, this.Height + 1);
  77.             _bufGraphics = _bufContext.Allocate(this.CreateGraphics(), this.ClientRectangle);
  78.  
  79.             if (_isHighQuality)
  80.             {
  81.                 _bufGraphics.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  82.                 _bufGraphics.Graphics.CompositingQuality = CompositingQuality.HighQuality;
  83.                 _bufGraphics.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  84.             }
  85.             else
  86.             {
  87.                 _bufGraphics.Graphics.SmoothingMode = SmoothingMode.Default;
  88.                 _bufGraphics.Graphics.CompositingQuality = CompositingQuality.Default;
  89.                 _bufGraphics.Graphics.PixelOffsetMode = PixelOffsetMode.Default;
  90.             }
  91.         }
  92.     }
  93.  
  94.     /// <summary>
  95.     /// Gets the location of the text according to the text alignment
  96.     /// </summary>
  97.     private Point GetAlignmentLocation()
  98.     {
  99.         switch (_textAlign)
  100.         {
  101.             case ContentAlignment.BottomCenter:
  102.                 {
  103.                     int x = (this.Width / 2) - (_textSize.Width / 2);
  104.                     int y = this.Height - _textSize.Height;
  105.                     return new Point(x, y);
  106.                 }
  107.  
  108.             case ContentAlignment.BottomLeft:
  109.                 {
  110.                     return new Point(0, this.Height - _textSize.Height);
  111.                 }
  112.  
  113.             case ContentAlignment.BottomRight:
  114.                 {
  115.                     return new Point(this.Width - _textSize.Width, this.Height - _textSize.Height);
  116.                 }
  117.  
  118.             case ContentAlignment.MiddleCenter:
  119.                 {
  120.                     int x = (this.Width / 2) - (_textSize.Width / 2);
  121.                     int y = (this.Height / 2) - (_textSize.Height / 2);
  122.                     return new Point(x, y);
  123.                 }
  124.  
  125.             case ContentAlignment.MiddleLeft:
  126.                 {
  127.                     int y = this.Height / 2 - (_textSize.Height / 2);
  128.                     return new Point(0, y);
  129.                 }
  130.  
  131.             case ContentAlignment.MiddleRight:
  132.                 {
  133.                     int y = (this.Height / 2) - (_textSize.Height / 2);
  134.                     int x = this.Width - _textSize.Width;
  135.                     return new Point(x, y);
  136.                 }
  137.  
  138.             case ContentAlignment.TopCenter:
  139.                 {
  140.                     int x = (this.Width / 2) - (_textSize.Width / 2);
  141.                     return new Point(x, 0);
  142.                 }
  143.  
  144.             case ContentAlignment.TopRight:
  145.                 {
  146.                     int x = this.Width - _textSize.Width;
  147.                     return new Point(x, 0);
  148.                 }
  149.  
  150.             default: return Point.Empty;
  151.         }
  152.     }
  153.  
  154.     private void Render()
  155.     {
  156.         // Update textsize here for simplicity
  157.         _textSize = _bufGraphics.Graphics.MeasureString(this.Text, this.Font).ToSize();
  158.         Point shadowLayerPos, topLayerPos;
  159.         // Disable size restrictions so we can set the actual size
  160.         this.MinimumSize = this.MaximumSize = new Size(0, 0);
  161.  
  162.         if (this.AutoSize)
  163.         {
  164.             // No text alignment needed with autosize
  165.             shadowLayerPos = topLayerPos = Point.Empty;
  166.             // Only adjust size when autosizing
  167.             this.Size = new Size(_textSize.Width, _textSize.Height);
  168.             // Lock size
  169.             this.MinimumSize = this.MaximumSize = this.Size;
  170.         }
  171.         else
  172.         {
  173.             shadowLayerPos = GetAlignmentLocation();
  174.             topLayerPos = shadowLayerPos;
  175.         }
  176.  
  177.         OffsetShadowLayer(ref shadowLayerPos);
  178.  
  179.         if (_embossStyle.Equals(EmbossStyle.Extrude))
  180.         {
  181.             // Repaint the shadow along a diagonal path to create a 3D effect
  182.             while (!shadowLayerPos.X.Equals(topLayerPos.X) && !shadowLayerPos.Y.Equals(topLayerPos.Y))
  183.             {
  184.                 if (shadowLayerPos.X < topLayerPos.X)
  185.                 {
  186.                     shadowLayerPos.X++;
  187.                 }
  188.                 else if (shadowLayerPos.X > topLayerPos.X)
  189.                 {
  190.                     shadowLayerPos.X--;
  191.                 }
  192.  
  193.                 if (shadowLayerPos.Y < topLayerPos.Y)
  194.                 {
  195.                     shadowLayerPos.Y++;
  196.                 }
  197.                 else if (shadowLayerPos.Y > topLayerPos.Y)
  198.                 {
  199.                     shadowLayerPos.Y--;
  200.                 }
  201.  
  202.                 _bufGraphics.Graphics.DrawString(this.Text, this.Font, _embossBrush, shadowLayerPos);
  203.             }
  204.         }
  205.         else if (_embossStyle.Equals(EmbossStyle.ShadowCast))
  206.         {
  207.             _bufGraphics.Graphics.DrawString(this.Text, this.Font, _embossBrush, shadowLayerPos);
  208.         }
  209.  
  210.         _bufGraphics.Graphics.DrawString(this.Text, this.Font, _surfaceBrush, topLayerPos);
  211.     }
  212.  
  213.     /// <summary>
  214.     /// Offsets the shadow layer, the shadow will move in relation to
  215.     /// the top layer, not the other way around
  216.     /// </summary>
  217.     private void OffsetShadowLayer(ref Point layerPos)
  218.     {
  219.         switch (_embossDirection)
  220.         {
  221.             case EmbossDirection.TopLeft:
  222.                 layerPos = new Point(layerPos.X - _dropAmount,
  223.                     layerPos.Y - _dropAmount);
  224.                 break;
  225.  
  226.             case EmbossDirection.TopRight:
  227.                 layerPos = new Point(layerPos.X + _dropAmount,
  228.                     layerPos.Y - _dropAmount);
  229.                 break;
  230.  
  231.             case EmbossDirection.BottomLeft:
  232.                 layerPos = new Point(layerPos.X - _dropAmount,
  233.                     layerPos.Y + _dropAmount);
  234.                 break;
  235.  
  236.             case EmbossDirection.BottomRight:
  237.                 layerPos = new Point(layerPos.X + _dropAmount,
  238.                     layerPos.Y + _dropAmount);
  239.                 break;
  240.         }
  241.     }
  242.  
  243.     #region Properties
  244.     [Category("Appearance")]
  245.     [Description("Determines whether or not the graphics are high quality")]
  246.     [DefaultValue(true)]
  247.     public bool HighQuality
  248.     {
  249.         get { return _isHighQuality; }
  250.         set
  251.         {
  252.             _isHighQuality = value;
  253.             this.Invalidate();
  254.         }
  255.     }
  256.  
  257.     [Category("Appearance")]
  258.     [Description("The distance in pixels the end of the emboss will be")]
  259.     [DefaultValue(5)]
  260.     public int EmbossAmount
  261.     {
  262.         get { return _dropAmount; }
  263.         set
  264.         {
  265.             _dropAmount = value;
  266.             this.Invalidate();
  267.         }
  268.     }
  269.  
  270.     [Category("Appearance")]
  271.     [DisplayName("Shadow Position")]
  272.     [Description("The position of the shadow")]
  273.     [DefaultValue((EmbossDirection)3)]
  274.     public EmbossDirection ShadowPos
  275.     {
  276.         get { return _embossDirection; }
  277.         set
  278.         {
  279.             _embossDirection = value;
  280.             this.Invalidate();
  281.         }
  282.     }
  283.  
  284.     [DefaultValue(typeof(Color), "YellowGreen")]
  285.     public override Color ForeColor
  286.     {
  287.         get { return base.ForeColor; }
  288.         set
  289.         {
  290.             base.ForeColor = value;
  291.             _surfaceBrush = new SolidBrush(value);
  292.             this.Invalidate();
  293.         }
  294.     }
  295.  
  296.     [Browsable(true)]
  297.     public override string Text
  298.     {
  299.         get { return base.Text; }
  300.         set
  301.         {
  302.             base.Text = value;
  303.             this.Invalidate();
  304.             this.Update(); // Needed to update bouds
  305.         }
  306.     }
  307.  
  308.     public override Font Font
  309.     {
  310.         get { return base.Font; }
  311.         set
  312.         {
  313.             base.Font = value;
  314.             this.Invalidate();
  315.             this.Update();
  316.         }
  317.     }
  318.  
  319.     [Category("Layout")]
  320.     [DefaultValue(ContentAlignment.MiddleLeft)]
  321.     public ContentAlignment TextAlign
  322.     {
  323.         get { return _textAlign; }
  324.         set
  325.         {
  326.             _textAlign = value;
  327.             this.Invalidate();
  328.         }
  329.     }
  330.  
  331.     [Browsable(true)]
  332.     [DefaultValue(true)]
  333.     public override bool AutoSize
  334.     {
  335.         get { return base.AutoSize; }
  336.         set
  337.         {
  338.             base.AutoSize = value;
  339.             this.Refresh();
  340.         }
  341.     }
  342.  
  343.     [Category("Appearance")]
  344.     [DefaultValue(TextRenderingHint.AntiAliasGridFit)]
  345.     public TextRenderingHint TextRenderingHint
  346.     {
  347.         get { return _textRenderingHint; }
  348.         set
  349.         {
  350.             _textRenderingHint = value;
  351.             UpdateGraphicsBuffer();
  352.             this.Invalidate();
  353.         }
  354.     }
  355.  
  356.     [Description("The gamma correction value used for rendering antialiased " +
  357.     "and ClearType text.The values must be between 0 and 12. The default value is 4.")]
  358.     [Category("Appearance")]
  359.     [DefaultValue(4)]
  360.     public int TextContrast
  361.     {
  362.         get { return _textContrast; }
  363.         set
  364.         {
  365.             _textContrast = value;
  366.             UpdateGraphicsBuffer();
  367.             this.Invalidate();
  368.         }
  369.     }
  370.  
  371.     [Description("The type of extrusion to use")]
  372.     [Category("Appearance")]
  373.     [DefaultValue(EmbossStyle.Extrude)]
  374.     public EmbossStyle EmbossStyle
  375.     {
  376.         get { return _embossStyle; }
  377.         set
  378.         {
  379.             _embossStyle = value;
  380.             this.Invalidate();
  381.         }
  382.     }
  383.  
  384.     [Editor(typeof(ColorDialog), typeof(ColorDialog))]
  385.     [Description("The color of the embossment")]
  386.     [Category("Appearance")]
  387.     [DefaultValue(typeof(Color), "Black")]
  388.     public Color EmbossColor
  389.     {
  390.         get
  391.         {
  392.             using (Pen pen = new Pen(_embossBrush))
  393.             {
  394.                 return pen.Color;
  395.             }
  396.         }
  397.         set
  398.         {
  399.             _embossBrush = new SolidBrush(value);
  400.             this.Invalidate();
  401.         }
  402.     }
  403.     #endregion
  404. }
  405. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement