SqLasticSoftware

Bildirim Kutusu

Feb 20th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.72 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5. #region NotificationBox
  6.  
  7. class NotificationBox : Control
  8. {
  9.  
  10.     #region Variables
  11.  
  12.     private Point CloseCoordinates;
  13.     private bool IsOverClose;
  14.     private int _BorderCurve = 8;
  15.     private GraphicsPath CreateRoundPath;
  16.     private string NotificationText = null;
  17.     private Type _NotificationType;
  18.     private bool _RoundedCorners;
  19.     private bool _ShowCloseButton;
  20.     private Image _Image;
  21.  
  22.     private Size _ImageSize;
  23.     #endregion
  24.     #region Enums
  25.  
  26.     // Create a list of Notification Types
  27.     public enum Type
  28.     {
  29.         Notice,
  30.         Success,
  31.         Warning,
  32.         Error,
  33.         Other
  34.     }
  35.  
  36.     #endregion
  37.     #region Custom Properties
  38.  
  39.     public Type NotificationType
  40.     {
  41.         get { return _NotificationType; }
  42.         set
  43.         {
  44.             _NotificationType = value;
  45.             Invalidate();
  46.         }
  47.     }
  48.  
  49.     public bool RoundCorners
  50.     {
  51.         get { return _RoundedCorners; }
  52.         set
  53.         {
  54.             _RoundedCorners = value;
  55.             Invalidate();
  56.         }
  57.     }
  58.  
  59.     public bool ShowCloseButton
  60.     {
  61.         get { return _ShowCloseButton; }
  62.         set
  63.         {
  64.             _ShowCloseButton = value;
  65.             Invalidate();
  66.         }
  67.     }
  68.  
  69.     public int BorderCurve
  70.     {
  71.         get { return _BorderCurve; }
  72.         set
  73.         {
  74.             _BorderCurve = value;
  75.             Invalidate();
  76.         }
  77.     }
  78.  
  79.     public Image Image
  80.     {
  81.         get { return _Image; }
  82.         set
  83.         {
  84.             if (value == null)
  85.             {
  86.                 _ImageSize = Size.Empty;
  87.             }
  88.             else
  89.             {
  90.                 _ImageSize = value.Size;
  91.             }
  92.  
  93.             _Image = value;
  94.             Invalidate();
  95.         }
  96.     }
  97.  
  98.     protected Size ImageSize
  99.     {
  100.         get { return _ImageSize; }
  101.     }
  102.  
  103.     #endregion
  104.     #region EventArgs
  105.  
  106.     protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
  107.     {
  108.         base.OnMouseMove(e);
  109.  
  110.         // Decides the location of the drawn ellipse. If mouse is over the correct coordinates, "IsOverClose" boolean will be triggered to draw the ellipse
  111.         if (e.X >= Width - 19 && e.X <= Width - 10 && e.Y > CloseCoordinates.Y && e.Y < CloseCoordinates.Y + 12)
  112.         {
  113.             IsOverClose = true;
  114.         }
  115.         else
  116.         {
  117.             IsOverClose = false;
  118.         }
  119.         Invalidate();
  120.     }
  121.     protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
  122.     {
  123.         base.OnMouseDown(e);
  124.  
  125.         if (_ShowCloseButton == true)
  126.         {
  127.             if (IsOverClose)
  128.             {
  129.                 Dispose();
  130.             }
  131.         }
  132.     }
  133.  
  134.     #endregion
  135.  
  136.     internal GraphicsPath CreateRoundRect(Rectangle r, int curve)
  137.     {
  138.         // Draw a border radius
  139.         try
  140.         {
  141.             CreateRoundPath = new GraphicsPath(FillMode.Winding);
  142.             CreateRoundPath.AddArc(r.X, r.Y, curve, curve, 180f, 90f);
  143.             CreateRoundPath.AddArc(r.Right - curve, r.Y, curve, curve, 270f, 90f);
  144.             CreateRoundPath.AddArc(r.Right - curve, r.Bottom - curve, curve, curve, 0f, 90f);
  145.             CreateRoundPath.AddArc(r.X, r.Bottom - curve, curve, curve, 90f, 90f);
  146.             CreateRoundPath.CloseFigure();
  147.         }
  148.         catch (Exception ex)
  149.         {
  150.             MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + "Value must be either '1' or higher", "Invalid Integer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  151.             // Return to the default border curve if the parameter is less than "1"
  152.             _BorderCurve = 8;
  153.             BorderCurve = 8;
  154.         }
  155.         return CreateRoundPath;
  156.     }
  157.  
  158.     public NotificationBox()
  159.     {
  160.         // AllPaintingInWmPaint - deals with drawing both the background and the foreground
  161.         // UserPaint - the control paints itself, not by the default system operation
  162.         // OptimizedDoubleBuffer - reduce graphics flicker when (AllPaintingInWmPaint) is used
  163.         // ResizeRedraw - the control redraw itself when it's resized
  164.         SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
  165.  
  166.         Font = new Font("Tahoma", 9);
  167.         this.Size = new Size(135, 40);
  168.         this.MinimumSize = new Size(100, 40);
  169.         RoundCorners = true;
  170.         ShowCloseButton = true;
  171.     }
  172.  
  173.     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  174.     {
  175.         base.OnPaint(e);
  176.  
  177.         Graphics GFX = e.Graphics;
  178.         Color ForeColor = default(Color);
  179.         Color BackgroundColor = default(Color);
  180.         Color BorderColor = default(Color);
  181.         Font TypeFont = new Font(Font.FontFamily, Font.Size, FontStyle.Bold);
  182.         Rectangle MainRectangle = new Rectangle(0, 0, Width - 1, Height - 1);
  183.         GraphicsPath CrvBorderPath = CreateRoundRect(MainRectangle, _BorderCurve);
  184.  
  185.         GFX.SmoothingMode = SmoothingMode.HighQuality;
  186.         GFX.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  187.         GFX.Clear(Parent.BackColor);
  188.  
  189.         switch (_NotificationType)
  190.         {
  191.             case Type.Notice:
  192.                 BackgroundColor = Color.FromArgb(227, 247, 252);
  193.                 BorderColor = Color.FromArgb(142, 217, 246);
  194.                 ForeColor = Color.DimGray;
  195.                 break;
  196.             case Type.Success:
  197.                 BackgroundColor = Color.FromArgb(233, 255, 217);
  198.                 BorderColor = Color.FromArgb(166, 202, 138);
  199.                 ForeColor = Color.DimGray;
  200.                 break;
  201.             case Type.Warning:
  202.                 BackgroundColor = Color.FromArgb(255, 248, 196);
  203.                 BorderColor = Color.FromArgb(242, 199, 121);
  204.                 ForeColor = Color.DimGray;
  205.                 break;
  206.             case Type.Error:
  207.                 BackgroundColor = Color.FromArgb(255, 236, 236);
  208.                 BorderColor = Color.FromArgb(245, 172, 166);
  209.                 ForeColor = Color.DimGray;
  210.                 break;
  211.             case Type.Other:
  212.                 BackgroundColor = Color.FromArgb(230, 230, 230);
  213.                 BorderColor = Color.FromArgb(180, 180, 180);
  214.                 ForeColor = Color.DimGray;
  215.                 break;
  216.         }
  217.  
  218.         if (_RoundedCorners == true)
  219.         {
  220.             GFX.FillPath(new SolidBrush(BackgroundColor), CrvBorderPath);
  221.             GFX.DrawPath(new Pen(BorderColor), CrvBorderPath);
  222.         }
  223.         else
  224.         {
  225.             GFX.FillRectangle(new SolidBrush(BackgroundColor), MainRectangle);
  226.             GFX.DrawRectangle(new Pen(BorderColor), MainRectangle);
  227.         }
  228.  
  229.         switch (_NotificationType)
  230.         {
  231.             case Type.Notice:
  232.                 NotificationText = "NOTICE";
  233.                 break;
  234.             case Type.Success:
  235.                 NotificationText = "SUCCESS";
  236.                 break;
  237.             case Type.Warning:
  238.                 NotificationText = "WARNING";
  239.                 break;
  240.             case Type.Error:
  241.                 NotificationText = "ERROR";
  242.                 break;
  243.             case Type.Other:
  244.                 NotificationText = "OTHER";
  245.                 break;
  246.         }
  247.  
  248.         if ((Image == null))
  249.         {
  250.             GFX.DrawString(NotificationText, TypeFont, new SolidBrush(ForeColor), new Point(10, 5));
  251.             GFX.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(10, 21, Width - 17, Height - 5));
  252.         }
  253.         else
  254.         {
  255.             GFX.DrawImage(_Image, 12, 4, 16, 16);
  256.             GFX.DrawString(NotificationText, TypeFont, new SolidBrush(ForeColor), new Point(30, 5));
  257.             GFX.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(10, 21, Width - 17, Height - 5));
  258.         }
  259.  
  260.         CloseCoordinates = new Point(Width - 26, 4);
  261.  
  262.         if (_ShowCloseButton == true)
  263.         {
  264.             // If the mouse is over the specified coordinates, ellipse will be drawn to give the control more interactivity
  265.             if (IsOverClose == true)
  266.             {
  267.                 GFX.FillEllipse(new SolidBrush(BorderColor), new Rectangle(Width - 23, 4, 17, 17));
  268.             }
  269.             // Draw the close button
  270.             GFX.DrawString("r", new Font("Marlett", 7, FontStyle.Regular), new SolidBrush(ForeColor), new Rectangle(Width - 20, 10, Width, Height), new StringFormat
  271.             {
  272.                 Alignment = StringAlignment.Near,
  273.                 LineAlignment = StringAlignment.Near
  274.             });
  275.         }
  276.  
  277.         // Clean up
  278.         CrvBorderPath.Dispose();
  279.     }
  280. }
  281.  
  282. #endregion
Advertisement
Add Comment
Please, Sign In to add comment