Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Directives
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Drawing.Text;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Drawing.Imaging;
- using System.Linq;
- #endregion
- #region Browse
- /* Copyright & Contact
- * --------------------------------------------------------------------------------<
- * Tool Name : Button *
- * From Project : Creator Eye *
- * Project Lang : C# *
- * Creator : Yassine Abbani *
- * Facebook : https://www.facebook.com/YassineAbbani.user *
- * Pastebin : https://pastebin.com/u/Yassine_Abbani *
- * Youtube : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw *
- * Version : 1.0 Beta *
- * Color : Dark *
- * Style : Glower *
- *>--------------------------------------------------------------------------------<
- */
- /* Features
- * ------------------------
- * Custom Properties:
- * Add Glow Effect To Button
- */
- /* history
- * ------------------------
- * 1.0 (21 Feb 2018):
- * This Costume Control Was Designed By Creator Eye UI for add that button to her library
- * Created by using Timer as animator + Custom Draw Control for Creat that Glow Effect
- *
- */
- #endregion
- #region Glow Button
- sealed class CeGlowButton : Button
- {
- #region Enums
- enum MouseState
- {
- Normal, Hover, Pressed
- }
- #endregion
- #region Variables
- private readonly SolidBrush _flatBgBrush = new SolidBrush(Color.FromArgb(20, 20, 20));
- private readonly Pen _penTopLine = new Pen(Color.FromArgb(77, 77, 77));
- private readonly Pen _penOuterBorder = new Pen(Color.FromArgb(40, 40, 40));
- private readonly SolidBrush _textBrush = new SolidBrush(Color.White);
- private readonly Color _gradient1 = Color.FromArgb(30, 30, 30);
- private readonly Color _gradient2 = Color.FromArgb(5, 5, 5);
- private MouseState _mouseState = MouseState.Normal;
- private const int MaxGlowAlpha = 100;
- private const int GlowSpeed = 5;
- private int _glowAlpha;
- private bool _glowBrighter;
- private bool _showGlow;
- private readonly Timer _timer = new Timer();
- #endregion
- #region Constructors
- public CeGlowButton()
- {
- DoubleBuffered = true;
- _timer.Interval = 1;
- _timer.Tick += _timer_Tick;
- _showGlow = true;
- }
- #endregion
- #region Event Methods
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- _mouseState = MouseState.Pressed;
- _showGlow = false;
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- _mouseState = MouseState.Hover;
- _showGlow = true;
- Invalidate();
- }
- protected override void OnMouseMove(MouseEventArgs mevent)
- {
- base.OnMouseMove(mevent);
- _mouseState = MouseState.Hover;
- _showGlow = true;
- _glowBrighter = true;
- if (_glowAlpha < MaxGlowAlpha)
- {
- _timer.Start();
- }
- }
- protected override void OnMouseLeave(EventArgs e)
- {
- base.OnMouseLeave(e);
- _mouseState = MouseState.Normal;
- _showGlow = true;
- _glowBrighter = false;
- if (_glowAlpha > 0)
- {
- _timer.Start();
- }
- }
- #endregion
- #region Draw Control
- #region Paint
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- e.Graphics.Clear(Color.Black);
- e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
- switch (_mouseState)
- {
- case MouseState.Hover:
- using (
- LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height),
- _gradient2, _gradient1))
- {
- e.Graphics.FillRectangle(lgb, DisplayRectangle);
- }
- _textBrush.Color = Color.White;
- break;
- case MouseState.Pressed:
- e.Graphics.FillRectangle(_flatBgBrush, DisplayRectangle);
- _textBrush.Color = Color.FromArgb(145, 145, 145);
- break;
- default: // Normal
- using (
- LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height),
- _gradient1, _gradient2))
- {
- e.Graphics.FillRectangle(lgb, DisplayRectangle);
- }
- _textBrush.Color = Color.White;
- break;
- }
- // Draw top line
- e.Graphics.DrawLine(_penTopLine, 0f, 0f, Width, 0f);
- // Draw other border lines
- e.Graphics.DrawLine(_penOuterBorder, 0f, 0f, 0f, Height - 1); // Left
- e.Graphics.DrawLine(_penOuterBorder, 0f, Height - 1, Width - 1, Height - 1); // Bottom
- e.Graphics.DrawLine(_penOuterBorder, Width - 1, 0f, Width - 1, Height - 1); // Right
- // Draw text
- DrawText(e.Graphics);
- }
- #endregion
- private void DrawText(Graphics graphics)
- {
- string text = Text.ToUpper();
- SizeF textSize = graphics.MeasureString(text, Font);
- float x = (Width / 2) - (textSize.Width / 2);
- float y = (Height / 2) - (textSize.Height / 2);
- graphics.DrawString(text, Font, _textBrush, x, y);
- if (_showGlow)
- {
- // Draw text glow
- DrawGlow(graphics, new Rectangle((int)x, (int)y, (int)textSize.Width, (int)textSize.Height));
- }
- }
- private void DrawGlow(Graphics graphics, Rectangle textRect)
- {
- Rectangle glowRect = ExpandRectangle(textRect, 2);
- Point[] pts = new[]
- {
- new Point(glowRect.X, glowRect.Y),
- new Point(glowRect.X + glowRect.Width, glowRect.Y),
- new Point(glowRect.X + glowRect.Width, glowRect.Y + glowRect.Height),
- new Point(glowRect.X, glowRect.Y + glowRect.Height)
- };
- using (PathGradientBrush pgb = new PathGradientBrush(pts))
- {
- pgb.CenterPoint = new PointF(Width / 2, Height / 2);
- pgb.CenterColor = Color.FromArgb(_glowAlpha, Color.White);
- pgb.SurroundColors = new[] { Color.Transparent };
- pgb.FocusScales = new PointF(0.25f, 0.25f);
- graphics.FillRectangle(pgb, glowRect);
- }
- }
- private void _timer_Tick(object sender, EventArgs e)
- {
- if (_glowBrighter)
- {
- if ((_glowAlpha += GlowSpeed) >= MaxGlowAlpha)
- {
- _timer.Stop();
- _glowAlpha = MaxGlowAlpha;
- }
- }
- else
- {
- if ((_glowAlpha -= GlowSpeed) <= 0)
- {
- _timer.Stop();
- _glowAlpha = 0;
- }
- }
- Invalidate();
- }
- private Rectangle ExpandRectangle(Rectangle rect, int expansion)
- {
- const int extraWidth = 4;
- return new Rectangle(rect.X - expansion - extraWidth, rect.Y - expansion,
- rect.Width + (expansion * 2) + (extraWidth * 2), rect.Height + (expansion * 2));
- }
- #endregion
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement