Advertisement
Yassine_Abbani

Glow [Button], [Custom Control]

May 8th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.87 KB | None | 0 0
  1. #region Directives
  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 Browse
  19. /* Copyright & Contact
  20. * --------------------------------------------------------------------------------<
  21. * Tool Name    : Button                                                           *
  22. * From Project : Creator Eye                                                      *
  23. * Project Lang : C#                                                               *
  24. * Creator      : Yassine Abbani                                                   *
  25. * Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  26. * Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  27. * Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  28. * Version      : 1.0 Beta                                                         *
  29. * Color        : Dark                                                             *
  30. * Style        : Glower                                                           *
  31. *>--------------------------------------------------------------------------------<
  32. */
  33. /* Features
  34. * ------------------------
  35. *   Custom Properties:
  36.  *   Add Glow Effect To Button
  37. */
  38. /*  history
  39. * ------------------------
  40. * 1.0 (21 Feb 2018):
  41.  *  This Costume Control Was Designed By Creator Eye UI for add that button to her library
  42.  *  Created by using Timer as animator + Custom Draw Control for Creat that Glow Effect
  43. *
  44. */
  45. #endregion
  46.  
  47. #region Glow Button
  48. sealed class CeGlowButton : Button
  49. {
  50.     #region Enums
  51.     enum MouseState
  52.     {
  53.         Normal, Hover, Pressed
  54.     }
  55.     #endregion
  56.     #region Variables
  57.     private readonly SolidBrush _flatBgBrush = new SolidBrush(Color.FromArgb(20, 20, 20));
  58.     private readonly Pen _penTopLine = new Pen(Color.FromArgb(77, 77, 77));
  59.     private readonly Pen _penOuterBorder = new Pen(Color.FromArgb(40, 40, 40));
  60.  
  61.     private readonly SolidBrush _textBrush = new SolidBrush(Color.White);
  62.  
  63.     private readonly Color _gradient1 = Color.FromArgb(30, 30, 30);
  64.     private readonly Color _gradient2 = Color.FromArgb(5, 5, 5);
  65.  
  66.     private MouseState _mouseState = MouseState.Normal;
  67.  
  68.     private const int MaxGlowAlpha = 100;
  69.     private const int GlowSpeed = 5;
  70.     private int _glowAlpha;
  71.     private bool _glowBrighter;
  72.     private bool _showGlow;
  73.     private readonly Timer _timer = new Timer();
  74. #endregion
  75.     #region Constructors
  76.     public CeGlowButton()
  77.     {
  78.         DoubleBuffered = true;
  79.         _timer.Interval = 1;
  80.         _timer.Tick += _timer_Tick;
  81.         _showGlow = true;
  82.     }
  83.     #endregion
  84.     #region Event Methods
  85.     protected override void OnMouseDown(MouseEventArgs e)
  86.     {
  87.         base.OnMouseDown(e);
  88.         _mouseState = MouseState.Pressed;
  89.         _showGlow = false;
  90.     }
  91.  
  92.     protected override void OnMouseUp(MouseEventArgs e)
  93.     {
  94.         base.OnMouseUp(e);
  95.         _mouseState = MouseState.Hover;
  96.         _showGlow = true;
  97.         Invalidate();
  98.     }
  99.  
  100.     protected override void OnMouseMove(MouseEventArgs mevent)
  101.     {
  102.         base.OnMouseMove(mevent);
  103.         _mouseState = MouseState.Hover;
  104.         _showGlow = true;
  105.         _glowBrighter = true;
  106.         if (_glowAlpha < MaxGlowAlpha)
  107.         {
  108.             _timer.Start();
  109.         }
  110.     }
  111.  
  112.     protected override void OnMouseLeave(EventArgs e)
  113.     {
  114.         base.OnMouseLeave(e);
  115.         _mouseState = MouseState.Normal;
  116.         _showGlow = true;
  117.         _glowBrighter = false;
  118.         if (_glowAlpha > 0)
  119.         {
  120.             _timer.Start();
  121.         }
  122.     }
  123.     #endregion
  124.     #region Draw Control
  125.     #region Paint
  126.     protected override void OnPaint(PaintEventArgs e)
  127.     {
  128.         base.OnPaint(e);
  129.         e.Graphics.Clear(Color.Black);
  130.         e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
  131.  
  132.         switch (_mouseState)
  133.         {
  134.             case MouseState.Hover:
  135.                 using (
  136.                     LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height),
  137.                                                                       _gradient2, _gradient1))
  138.                 {
  139.                     e.Graphics.FillRectangle(lgb, DisplayRectangle);
  140.                 }
  141.                 _textBrush.Color = Color.White;
  142.                 break;
  143.             case MouseState.Pressed:
  144.                 e.Graphics.FillRectangle(_flatBgBrush, DisplayRectangle);
  145.                 _textBrush.Color = Color.FromArgb(145, 145, 145);
  146.                 break;
  147.             default: // Normal
  148.                 using (
  149.                     LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height),
  150.                                                                       _gradient1, _gradient2))
  151.                 {
  152.                     e.Graphics.FillRectangle(lgb, DisplayRectangle);
  153.                 }
  154.                 _textBrush.Color = Color.White;
  155.                 break;
  156.         }
  157.  
  158.         // Draw top line
  159.         e.Graphics.DrawLine(_penTopLine, 0f, 0f, Width, 0f);
  160.  
  161.         // Draw other border lines
  162.         e.Graphics.DrawLine(_penOuterBorder, 0f, 0f, 0f, Height - 1); // Left
  163.         e.Graphics.DrawLine(_penOuterBorder, 0f, Height - 1, Width - 1, Height - 1); // Bottom
  164.         e.Graphics.DrawLine(_penOuterBorder, Width - 1, 0f, Width - 1, Height - 1); // Right
  165.  
  166.         // Draw text
  167.         DrawText(e.Graphics);
  168.     }
  169.     #endregion
  170.     private void DrawText(Graphics graphics)
  171.     {
  172.         string text = Text.ToUpper();
  173.         SizeF textSize = graphics.MeasureString(text, Font);
  174.         float x = (Width / 2) - (textSize.Width / 2);
  175.         float y = (Height / 2) - (textSize.Height / 2);
  176.         graphics.DrawString(text, Font, _textBrush, x, y);
  177.  
  178.         if (_showGlow)
  179.         {
  180.             // Draw text glow
  181.             DrawGlow(graphics, new Rectangle((int)x, (int)y, (int)textSize.Width, (int)textSize.Height));
  182.         }
  183.     }
  184.  
  185.     private void DrawGlow(Graphics graphics, Rectangle textRect)
  186.     {
  187.         Rectangle glowRect = ExpandRectangle(textRect, 2);
  188.         Point[] pts = new[]
  189.             {
  190.                 new Point(glowRect.X, glowRect.Y),
  191.                 new Point(glowRect.X + glowRect.Width, glowRect.Y),
  192.                 new Point(glowRect.X + glowRect.Width, glowRect.Y + glowRect.Height),
  193.                 new Point(glowRect.X, glowRect.Y + glowRect.Height)
  194.             };
  195.         using (PathGradientBrush pgb = new PathGradientBrush(pts))
  196.         {
  197.             pgb.CenterPoint = new PointF(Width / 2, Height / 2);
  198.             pgb.CenterColor = Color.FromArgb(_glowAlpha, Color.White);
  199.             pgb.SurroundColors = new[] { Color.Transparent };
  200.             pgb.FocusScales = new PointF(0.25f, 0.25f);
  201.             graphics.FillRectangle(pgb, glowRect);
  202.         }
  203.     }
  204.  
  205.     private void _timer_Tick(object sender, EventArgs e)
  206.     {
  207.         if (_glowBrighter)
  208.         {
  209.             if ((_glowAlpha += GlowSpeed) >= MaxGlowAlpha)
  210.             {
  211.                 _timer.Stop();
  212.                 _glowAlpha = MaxGlowAlpha;
  213.             }
  214.         }
  215.         else
  216.         {
  217.             if ((_glowAlpha -= GlowSpeed) <= 0)
  218.             {
  219.                 _timer.Stop();
  220.                 _glowAlpha = 0;
  221.             }
  222.         }
  223.         Invalidate();
  224.     }
  225.  
  226.     private Rectangle ExpandRectangle(Rectangle rect, int expansion)
  227.     {
  228.         const int extraWidth = 4;
  229.         return new Rectangle(rect.X - expansion - extraWidth, rect.Y - expansion,
  230.                              rect.Width + (expansion * 2) + (extraWidth * 2), rect.Height + (expansion * 2));
  231.     }
  232.  
  233.     #endregion
  234. }
  235.  
  236. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement