Advertisement
Yassine_Abbani

Notification Box [Panel], [Custom Control]

May 22nd, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.31 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.Drawing.Text;
  7. using System.Windows.Forms;
  8. #endregion
  9. #region Copyright & Contact
  10. //>-------------------------------------------------------------------------------<
  11. // Tool Name    : Notification Box                                                 *
  12. // From Project : Creator Eye                                                      *
  13. // Creator      : Yassine Abbani                                                   *
  14. // Facebook     : https://www.facebook.com/YassineAbbani.user                      *
  15. // Pastebin     : https://pastebin.com/u/Yassine_Abbani                            *
  16. // Youtube      : https://www.youtube.com/channel/UCqvOCRs3HWbPH4yuZuTx8mw         *
  17. // Version      : 1.0 Beta                                                         *
  18. // Color        : Null                                                             *
  19. //>-------------------------------------------------------------------------------<
  20. #endregion
  21. #region  Notification Box
  22.  
  23. public class Ce_Notification : Control
  24. {
  25.  
  26.     #region  Variables
  27.  
  28.     private Point CloseCoordinates;
  29.     private bool IsOverClose;
  30.     private int _BorderCurve = 8;
  31.     private GraphicsPath CreateRoundPath;
  32.     private string NotificationText = null;
  33.     private Type _NotificationType;
  34.     private bool _RoundedCorners;
  35.     private bool _ShowCloseButton;
  36.     private Image _Image;
  37.     private Size _ImageSize;
  38.  
  39.     #endregion
  40.     #region  Enums
  41.  
  42.     // Create a list of Notification Types
  43.     public enum Type
  44.     {
  45.         @Notice,
  46.         @Success,
  47.         @Warning,
  48.         @Error
  49.     }
  50.  
  51.     #endregion
  52.     #region  Custom Properties
  53.     #region Notification Type
  54.     [Browsable(true), Category("Setting"), Description("Create a NotificationType property and add the Type enum to it.")]
  55.     public Type NotificationType
  56.     {
  57.         get
  58.         {
  59.             return _NotificationType;
  60.         }
  61.         set
  62.         {
  63.             _NotificationType = value;
  64.             Invalidate();
  65.         }
  66.     }
  67.     #endregion
  68.     #region ShowCloseButton
  69.     [Browsable(true), Category("Setting"), Description("Boolean value to determine whether the control should draw the close button.")]
  70.     public bool ShowCloseButton
  71.     {
  72.         get
  73.         {
  74.             return _ShowCloseButton;
  75.         }
  76.         set
  77.         {
  78.             _ShowCloseButton = value;
  79.             Invalidate();
  80.         }
  81.     }
  82.     #endregion
  83.     #region Round Corners
  84.     [Browsable(true), Category("Setting"), Description("Boolean value to determine whether the control should use border radius.")]
  85.     public bool RoundCorners
  86.     {
  87.         get
  88.         {
  89.             return _RoundedCorners;
  90.         }
  91.         set
  92.         {
  93.             _RoundedCorners = value;
  94.             Invalidate();
  95.         }
  96.     }
  97.     #endregion
  98.     #region Border Curve
  99.     [Browsable(true), Category("Setting"), Description("Integer value to determine the curve level of the borders.")]
  100.     public int BorderCurve
  101.     {
  102.         get
  103.         {
  104.             return _BorderCurve;
  105.         }
  106.         set
  107.         {
  108.             _BorderCurve = value;
  109.             Invalidate();
  110.         }
  111.     }
  112. #endregion
  113.     #region Image
  114.     [Browsable(true), Category("Setting"), Description("Image value to determine whether the control should draw an image before the header.")]
  115.     public Image Image
  116.     {
  117.         get
  118.         {
  119.             return _Image;
  120.         }
  121.         set
  122.         {
  123.             if (value == null)
  124.             {
  125.                 _ImageSize = Size.Empty;
  126.             }
  127.             else
  128.             {
  129.                 _ImageSize = value.Size;
  130.             }
  131.  
  132.             _Image = value;
  133.             Invalidate();
  134.         }
  135.     }
  136.     [Browsable(false), Category("Setting"), Description("Size value - returns the image size.")]
  137.     protected Size ImageSize
  138.     {
  139.         get
  140.         {
  141.             return _ImageSize;
  142.         }
  143.     }
  144.     #endregion
  145.     #endregion
  146.     #region  EventArgs
  147.  
  148.     protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
  149.     {
  150.         base.OnMouseMove(e);
  151.  
  152.         // Decides the location of the drawn ellipse. If mouse is over the correct coordinates, "IsOverClose" boolean will be triggered to draw the ellipse
  153.         if (e.X >= Width - 19 && e.X <= Width - 10 && e.Y > CloseCoordinates.Y && e.Y < CloseCoordinates.Y + 12)
  154.         {
  155.             IsOverClose = true;
  156.         }
  157.         else
  158.         {
  159.             IsOverClose = false;
  160.         }
  161.         // Updates the control
  162.         Invalidate();
  163.     }
  164.     protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
  165.     {
  166.         base.OnMouseDown(e);
  167.  
  168.         // Disposes the control when the close button is clicked
  169.         if (_ShowCloseButton == true)
  170.         {
  171.             if (IsOverClose)
  172.             {
  173.                 Dispose();
  174.             }
  175.         }
  176.     }
  177.  
  178.     #endregion
  179.     #region Draw Control
  180.     internal GraphicsPath CreateRoundRect(Rectangle r, int curve)
  181.     {
  182.         // Draw a border radius
  183.         try
  184.         {
  185.             CreateRoundPath = new GraphicsPath(FillMode.Winding);
  186.             CreateRoundPath.AddArc(r.X, r.Y, curve, curve, 180.0F, 90.0F);
  187.             CreateRoundPath.AddArc(r.Right - curve, r.Y, curve, curve, 270.0F, 90.0F);
  188.             CreateRoundPath.AddArc(r.Right - curve, r.Bottom - curve, curve, curve, 0.0F, 90.0F);
  189.             CreateRoundPath.AddArc(r.X, r.Bottom - curve, curve, curve, 90.0F, 90.0F);
  190.             CreateRoundPath.CloseFigure();
  191.         }
  192.         catch (Exception ex)
  193.         {
  194.             MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + "Value must be either \'1\' or higher", "Invalid Integer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  195.             // Return to the default border curve if the parameter is less than "1"
  196.             _BorderCurve = 8;
  197.             BorderCurve = 8;
  198.         }
  199.         return CreateRoundPath;
  200.     }
  201.  
  202.     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  203.     {
  204.         base.OnPaint(e);
  205.  
  206.         // Declare Graphics to draw the control
  207.         Graphics GFX = e.Graphics;
  208.         // Declare Color to paint the control's Text, Background and Border
  209.         Color ForeColor = new Color();
  210.         Color BackgroundColor = new Color();
  211.         Color BorderColor = new Color();
  212.         // Determine the header Notification Type font
  213.         Font TypeFont = new Font(Font.FontFamily, Font.Size, FontStyle.Bold);
  214.         // Decalre a new rectangle to draw the control inside it
  215.         Rectangle MainRectangle = new Rectangle(0, 0, Width - 1, Height - 1);
  216.         // Declare a GraphicsPath to create a border radius
  217.         GraphicsPath CrvBorderPath = CreateRoundRect(MainRectangle, _BorderCurve);
  218.  
  219.         GFX.SmoothingMode = SmoothingMode.HighQuality;
  220.         GFX.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  221.         GFX.Clear(Parent.BackColor);
  222.  
  223.         switch (_NotificationType)
  224.         {
  225.             case Type.Notice:
  226.                 BackgroundColor = Color.FromArgb(38, 38, 38);
  227.                 BorderColor = Color.FromArgb(117, 123, 122);
  228.                 ForeColor = Color.FromArgb(117, 123, 122);
  229.                 break;
  230.             case Type.Success:
  231.                 BackgroundColor = Color.FromArgb(38, 38, 38);
  232.                 BorderColor = Color.FromArgb(91, 195, 162);
  233.                 ForeColor = Color.FromArgb(91, 195, 162);
  234.                 break;
  235.             case Type.Warning:
  236.                 BackgroundColor = Color.FromArgb(38, 38, 38);
  237.                 BorderColor = Color.FromArgb(254, 209, 108);
  238.                 ForeColor = Color.FromArgb(254, 209, 108);
  239.                 break;
  240.             case Type.Error:
  241.                 BackgroundColor = Color.FromArgb(38, 38, 38);
  242.                 BorderColor = Color.FromArgb(217, 103, 93);
  243.                 ForeColor = Color.FromArgb(217, 103, 93);
  244.                 break;
  245.         }
  246.  
  247.         if (_RoundedCorners == true)
  248.         {
  249.             GFX.FillPath(new SolidBrush(BackgroundColor), CrvBorderPath);
  250.             GFX.DrawPath(new Pen(BorderColor), CrvBorderPath);
  251.         }
  252.         else
  253.         {
  254.             GFX.FillRectangle(new SolidBrush(BackgroundColor), MainRectangle);
  255.             GFX.DrawRectangle(new Pen(BorderColor), MainRectangle);
  256.         }
  257.  
  258.         switch (_NotificationType)
  259.         {
  260.             case Type.Notice:
  261.                 NotificationText = "NOTICE";
  262.                 break;
  263.             case Type.Success:
  264.                 NotificationText = "SUCCESS";
  265.                 break;
  266.             case Type.Warning:
  267.                 NotificationText = "WARNING";
  268.                 break;
  269.             case Type.Error:
  270.                 NotificationText = "ERROR";
  271.                 break;
  272.         }
  273.  
  274.         if (Image == null)
  275.         {
  276.             GFX.DrawString(NotificationText, TypeFont, new SolidBrush(ForeColor), new Point(10, 5));
  277.             GFX.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(10, 21, Width - 17, Height - 5));
  278.         }
  279.         else
  280.         {
  281.             GFX.DrawImage(_Image, 12, 4, 16, 16);
  282.             GFX.DrawString(NotificationText, TypeFont, new SolidBrush(ForeColor), new Point(30, 5));
  283.             GFX.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(10, 21, Width - 17, Height - 5));
  284.         }
  285.  
  286.         CloseCoordinates = new Point(Width - 26, 4);
  287.  
  288.         if (_ShowCloseButton == true)
  289.         {
  290.             // Draw the close button
  291.             GFX.DrawString("r", new Font("Marlett", 7, FontStyle.Regular), new SolidBrush(Color.FromArgb(130, 130, 130)), new Rectangle(Width - 20, 10, Width, Height), new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near });
  292.         }
  293.  
  294.         CrvBorderPath.Dispose();
  295.     }
  296.     #endregion
  297.     #region Constructors
  298.     public Ce_Notification()
  299.     {
  300.         SetStyle((System.Windows.Forms.ControlStyles)(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw), true);
  301.  
  302.         Font = new Font("Microsoft Sans Serif", 9);
  303.         this.MinimumSize = new Size(100, 40);
  304.         RoundCorners = false;
  305.         ShowCloseButton = false;
  306.     }
  307.     #endregion
  308. }
  309.  
  310. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement