SqLasticSoftware

Bildirim Kutusu

Feb 20th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.52 KB | None | 0 0
  1. Imports System.Drawing.Drawing2D
  2. Class NotificationBox
  3.     Inherits Control
  4.  
  5. #Region " Variables "
  6.  
  7.     Private CloseCoordinates As Point
  8.     Private IsOverClose As Boolean
  9.     Private _BorderCurve As Integer = 8
  10.     Private CreateRoundPath As GraphicsPath
  11.     Private NotificationText As String = Nothing
  12.     Private _NotificationType As Type
  13.     Private _RoundedCorners As Boolean
  14.     Private _ShowCloseButton As Boolean
  15.     Private _Image As Image
  16.     Private _ImageSize As Size
  17.  
  18. #End Region
  19. #Region " Enums "
  20.  
  21.     ' Create a list of Notification Types
  22.     Enum Type
  23.         [Notice]
  24.         [Success]
  25.         [Warning]
  26.         [Error]
  27.         [Other]
  28.     End Enum
  29.  
  30. #End Region
  31. #Region " Custom Properties "
  32.  
  33.  
  34.     Public Property NotificationType As Type
  35.         Get
  36.             Return _NotificationType
  37.         End Get
  38.         Set(ByVal value As Type)
  39.             _NotificationType = value
  40.             Invalidate()
  41.         End Set
  42.     End Property
  43.  
  44.     Public Property RoundCorners As Boolean
  45.         Get
  46.             Return _RoundedCorners
  47.         End Get
  48.         Set(ByVal value As Boolean)
  49.             _RoundedCorners = value
  50.             Invalidate()
  51.         End Set
  52.     End Property
  53.  
  54.     Public Property ShowCloseButton As Boolean
  55.         Get
  56.             Return _ShowCloseButton
  57.         End Get
  58.         Set(ByVal value As Boolean)
  59.             _ShowCloseButton = value
  60.             Invalidate()
  61.         End Set
  62.     End Property
  63.  
  64.     Public Property BorderCurve As Integer
  65.         Get
  66.             Return _BorderCurve
  67.         End Get
  68.         Set(ByVal value As Integer)
  69.             _BorderCurve = value
  70.             Invalidate()
  71.         End Set
  72.     End Property
  73.  
  74.     Property Image() As Image
  75.         Get
  76.             Return _Image
  77.         End Get
  78.         Set(ByVal value As Image)
  79.             If value Is Nothing Then
  80.                 _ImageSize = Size.Empty
  81.             Else
  82.                 _ImageSize = value.Size
  83.             End If
  84.  
  85.             _Image = value
  86.             Invalidate()
  87.         End Set
  88.     End Property
  89.        
  90.     Protected ReadOnly Property ImageSize() As Size
  91.         Get
  92.             Return _ImageSize
  93.         End Get
  94.     End Property
  95.  
  96. #End Region
  97. #Region " EventArgs "
  98.  
  99.     Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
  100.         MyBase.OnMouseMove(e)
  101.  
  102.         ' Decides the location of the drawn ellipse. If mouse is over the correct coordinates, "IsOverClose" boolean will be triggered to draw the ellipse
  103.         If e.X >= Width - 19 AndAlso e.X <= Width - 10 AndAlso e.Y > CloseCoordinates.Y AndAlso e.Y < CloseCoordinates.Y + 12 Then
  104.             IsOverClose = True
  105.         Else
  106.             IsOverClose = False
  107.         End If
  108.         Invalidate()
  109.     End Sub
  110.     Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
  111.         MyBase.OnMouseDown(e)
  112.  
  113.         ' Disposes the control when the close button is clicked
  114.         If _ShowCloseButton = True Then
  115.             If IsOverClose Then
  116.                 Dispose()
  117.             End If
  118.         End If
  119.     End Sub
  120.  
  121. #End Region
  122.  
  123.     Friend Function CreateRoundRect(ByVal r As Rectangle, ByVal curve As Integer) As GraphicsPath
  124.         ' Draw a border radius
  125.         Try
  126.             CreateRoundPath = New GraphicsPath(FillMode.Winding)
  127.             CreateRoundPath.AddArc(r.X, r.Y, curve, curve, 180.0F, 90.0F)
  128.             CreateRoundPath.AddArc(r.Right - curve, r.Y, curve, curve, 270.0F, 90.0F)
  129.             CreateRoundPath.AddArc(r.Right - curve, r.Bottom - curve, curve, curve, 0.0F, 90.0F)
  130.             CreateRoundPath.AddArc(r.X, r.Bottom - curve, curve, curve, 90.0F, 90.0F)
  131.             CreateRoundPath.CloseFigure()
  132.         Catch ex As Exception
  133.             MessageBox.Show(ex.Message & vbNewLine & vbNewLine & "Value must be either '1' or higher", "Invalid Integer", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  134.             ' Return to the default border curve if the parameter is less than "1"
  135.             _BorderCurve = 8
  136.             BorderCurve = 8
  137.         End Try
  138.         Return CreateRoundPath
  139.     End Function
  140.  
  141.     Sub New()
  142.         ' AllPaintingInWmPaint - deals with drawing both the background and the foreground
  143.         ' UserPaint - the control paints itself, not by the default system operation
  144.         ' OptimizedDoubleBuffer - reduce graphics flicker when (AllPaintingInWmPaint) is used
  145.         ' ResizeRedraw - the control redraw itself when it's resized
  146.         SetStyle(ControlStyles.AllPaintingInWmPaint Or _
  147.                  ControlStyles.UserPaint Or _
  148.                  ControlStyles.OptimizedDoubleBuffer Or _
  149.                  ControlStyles.ResizeRedraw, True)
  150.  
  151.         Font = New Font("Tahoma", 9)
  152.                 Me.Size = new Size(135, 40)
  153.         Me.MinimumSize = New Size(100, 40)
  154.         RoundCorners = True
  155.         ShowCloseButton = True
  156.     End Sub
  157.  
  158.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  159.         MyBase.OnPaint(e)
  160.  
  161.         Dim GFX As Graphics = e.Graphics
  162.         Dim ForeColor, BackgroundColor, BorderColor As Color
  163.         Dim TypeFont As New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
  164.         Dim MainRectangle As New Rectangle(0, 0, Width - 1, Height - 1)
  165.         Dim CrvBorderPath As GraphicsPath = CreateRoundRect(MainRectangle, _BorderCurve)
  166.  
  167.         GFX.SmoothingMode = SmoothingMode.HighQuality
  168.         GFX.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
  169.         GFX.Clear(Parent.BackColor)
  170.  
  171.         Select Case _NotificationType
  172.             Case Type.Notice
  173.                 BackgroundColor = Color.FromArgb(227, 247, 252)
  174.                 BorderColor = Color.FromArgb(142, 217, 246)
  175.                 ForeColor = Color.DimGray
  176.             Case Type.Success
  177.                 BackgroundColor = Color.FromArgb(233, 255, 217)
  178.                 BorderColor = Color.FromArgb(166, 202, 138)
  179.                 ForeColor = Color.DimGray
  180.             Case Type.Warning
  181.                 BackgroundColor = Color.FromArgb(255, 248, 196)
  182.                 BorderColor = Color.FromArgb(242, 199, 121)
  183.                 ForeColor = Color.DimGray
  184.             Case Type.Error
  185.                 BackgroundColor = Color.FromArgb(255, 236, 236)
  186.                 BorderColor = Color.FromArgb(245, 172, 166)
  187.                 ForeColor = Color.DimGray
  188.             Case Type.Other
  189.                 BackgroundColor = Color.FromArgb(230, 230, 230)
  190.                 BorderColor = Color.FromArgb(180, 180, 180)
  191.                 ForeColor = Color.DimGray
  192.         End Select
  193.  
  194.         If _RoundedCorners = True Then
  195.             GFX.FillPath(New SolidBrush(BackgroundColor), CrvBorderPath)
  196.             GFX.DrawPath(New Pen(BorderColor), CrvBorderPath)
  197.         Else
  198.             GFX.FillRectangle(New SolidBrush(BackgroundColor), MainRectangle)
  199.             GFX.DrawRectangle(New Pen(BorderColor), MainRectangle)
  200.         End If
  201.  
  202.         Select Case _NotificationType
  203.             Case Type.Notice
  204.                 NotificationText = "NOTICE"
  205.             Case Type.Success
  206.                 NotificationText = "SUCCESS"
  207.             Case Type.Warning
  208.                 NotificationText = "WARNING"
  209.             Case Type.Error
  210.                 NotificationText = "ERROR"
  211.             Case Type.Other
  212.                 NotificationText = "OTHER"
  213.         End Select
  214.  
  215.         If IsNothing(Image) Then
  216.             GFX.DrawString(NotificationText, TypeFont, New SolidBrush(ForeColor), New Point(10, 5))
  217.             GFX.DrawString(Text, Font, New SolidBrush(ForeColor), New Rectangle(10, 21, Width - 17, Height - 5))
  218.         Else
  219.             GFX.DrawImage(_Image, 12, 4, 16, 16)
  220.             GFX.DrawString(NotificationText, TypeFont, New SolidBrush(ForeColor), New Point(30, 5))
  221.             GFX.DrawString(Text, Font, New SolidBrush(ForeColor), New Rectangle(10, 21, Width - 17, Height - 5))
  222.         End If
  223.  
  224.         ' Coordinates in which an ellipse will be drawn at on MouseOver event
  225.         CloseCoordinates = New Point(Width - 26, 4)
  226.  
  227.         If _ShowCloseButton = True Then
  228.             If IsOverClose = True Then
  229.                 GFX.FillEllipse(New SolidBrush(BorderColor), New Rectangle(Width - 23, 4, 17, 17))
  230.             End If
  231.             GFX.DrawString("r", New Font("Marlett", 7, FontStyle.Regular), New SolidBrush(ForeColor), New Rectangle(Width - 20, 10, Width, Height), New StringFormat() With {.Alignment = StringAlignment.Near, .LineAlignment = StringAlignment.Near})
  232.         End If
  233.  
  234.         ' Clean up
  235.         CrvBorderPath.Dispose()
  236.     End Sub
  237. End Class
  238.  
  239. #End Region
Advertisement
Add Comment
Please, Sign In to add comment