Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Drawing.Drawing2D
- Class NotificationBox
- Inherits Control
- #Region " Variables "
- Private CloseCoordinates As Point
- Private IsOverClose As Boolean
- Private _BorderCurve As Integer = 8
- Private CreateRoundPath As GraphicsPath
- Private NotificationText As String = Nothing
- Private _NotificationType As Type
- Private _RoundedCorners As Boolean
- Private _ShowCloseButton As Boolean
- Private _Image As Image
- Private _ImageSize As Size
- #End Region
- #Region " Enums "
- ' Create a list of Notification Types
- Enum Type
- [Notice]
- [Success]
- [Warning]
- [Error]
- [Other]
- End Enum
- #End Region
- #Region " Custom Properties "
- Public Property NotificationType As Type
- Get
- Return _NotificationType
- End Get
- Set(ByVal value As Type)
- _NotificationType = value
- Invalidate()
- End Set
- End Property
- Public Property RoundCorners As Boolean
- Get
- Return _RoundedCorners
- End Get
- Set(ByVal value As Boolean)
- _RoundedCorners = value
- Invalidate()
- End Set
- End Property
- Public Property ShowCloseButton As Boolean
- Get
- Return _ShowCloseButton
- End Get
- Set(ByVal value As Boolean)
- _ShowCloseButton = value
- Invalidate()
- End Set
- End Property
- Public Property BorderCurve As Integer
- Get
- Return _BorderCurve
- End Get
- Set(ByVal value As Integer)
- _BorderCurve = value
- Invalidate()
- End Set
- End Property
- Property Image() As Image
- Get
- Return _Image
- End Get
- Set(ByVal value As Image)
- If value Is Nothing Then
- _ImageSize = Size.Empty
- Else
- _ImageSize = value.Size
- End If
- _Image = value
- Invalidate()
- End Set
- End Property
- Protected ReadOnly Property ImageSize() As Size
- Get
- Return _ImageSize
- End Get
- End Property
- #End Region
- #Region " EventArgs "
- Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
- MyBase.OnMouseMove(e)
- ' Decides the location of the drawn ellipse. If mouse is over the correct coordinates, "IsOverClose" boolean will be triggered to draw the ellipse
- If e.X >= Width - 19 AndAlso e.X <= Width - 10 AndAlso e.Y > CloseCoordinates.Y AndAlso e.Y < CloseCoordinates.Y + 12 Then
- IsOverClose = True
- Else
- IsOverClose = False
- End If
- Invalidate()
- End Sub
- Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
- MyBase.OnMouseDown(e)
- ' Disposes the control when the close button is clicked
- If _ShowCloseButton = True Then
- If IsOverClose Then
- Dispose()
- End If
- End If
- End Sub
- #End Region
- Friend Function CreateRoundRect(ByVal r As Rectangle, ByVal curve As Integer) As GraphicsPath
- ' Draw a border radius
- Try
- CreateRoundPath = New GraphicsPath(FillMode.Winding)
- CreateRoundPath.AddArc(r.X, r.Y, curve, curve, 180.0F, 90.0F)
- CreateRoundPath.AddArc(r.Right - curve, r.Y, curve, curve, 270.0F, 90.0F)
- CreateRoundPath.AddArc(r.Right - curve, r.Bottom - curve, curve, curve, 0.0F, 90.0F)
- CreateRoundPath.AddArc(r.X, r.Bottom - curve, curve, curve, 90.0F, 90.0F)
- CreateRoundPath.CloseFigure()
- Catch ex As Exception
- MessageBox.Show(ex.Message & vbNewLine & vbNewLine & "Value must be either '1' or higher", "Invalid Integer", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- ' Return to the default border curve if the parameter is less than "1"
- _BorderCurve = 8
- BorderCurve = 8
- End Try
- Return CreateRoundPath
- End Function
- Sub New()
- ' AllPaintingInWmPaint - deals with drawing both the background and the foreground
- ' UserPaint - the control paints itself, not by the default system operation
- ' OptimizedDoubleBuffer - reduce graphics flicker when (AllPaintingInWmPaint) is used
- ' ResizeRedraw - the control redraw itself when it's resized
- SetStyle(ControlStyles.AllPaintingInWmPaint Or _
- ControlStyles.UserPaint Or _
- ControlStyles.OptimizedDoubleBuffer Or _
- ControlStyles.ResizeRedraw, True)
- Font = New Font("Tahoma", 9)
- Me.Size = new Size(135, 40)
- Me.MinimumSize = New Size(100, 40)
- RoundCorners = True
- ShowCloseButton = True
- End Sub
- Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
- MyBase.OnPaint(e)
- Dim GFX As Graphics = e.Graphics
- Dim ForeColor, BackgroundColor, BorderColor As Color
- Dim TypeFont As New Font(Font.FontFamily, Font.Size, FontStyle.Bold)
- Dim MainRectangle As New Rectangle(0, 0, Width - 1, Height - 1)
- Dim CrvBorderPath As GraphicsPath = CreateRoundRect(MainRectangle, _BorderCurve)
- GFX.SmoothingMode = SmoothingMode.HighQuality
- GFX.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
- GFX.Clear(Parent.BackColor)
- Select Case _NotificationType
- Case Type.Notice
- BackgroundColor = Color.FromArgb(227, 247, 252)
- BorderColor = Color.FromArgb(142, 217, 246)
- ForeColor = Color.DimGray
- Case Type.Success
- BackgroundColor = Color.FromArgb(233, 255, 217)
- BorderColor = Color.FromArgb(166, 202, 138)
- ForeColor = Color.DimGray
- Case Type.Warning
- BackgroundColor = Color.FromArgb(255, 248, 196)
- BorderColor = Color.FromArgb(242, 199, 121)
- ForeColor = Color.DimGray
- Case Type.Error
- BackgroundColor = Color.FromArgb(255, 236, 236)
- BorderColor = Color.FromArgb(245, 172, 166)
- ForeColor = Color.DimGray
- Case Type.Other
- BackgroundColor = Color.FromArgb(230, 230, 230)
- BorderColor = Color.FromArgb(180, 180, 180)
- ForeColor = Color.DimGray
- End Select
- If _RoundedCorners = True Then
- GFX.FillPath(New SolidBrush(BackgroundColor), CrvBorderPath)
- GFX.DrawPath(New Pen(BorderColor), CrvBorderPath)
- Else
- GFX.FillRectangle(New SolidBrush(BackgroundColor), MainRectangle)
- GFX.DrawRectangle(New Pen(BorderColor), MainRectangle)
- End If
- Select Case _NotificationType
- Case Type.Notice
- NotificationText = "NOTICE"
- Case Type.Success
- NotificationText = "SUCCESS"
- Case Type.Warning
- NotificationText = "WARNING"
- Case Type.Error
- NotificationText = "ERROR"
- Case Type.Other
- NotificationText = "OTHER"
- End Select
- If IsNothing(Image) Then
- GFX.DrawString(NotificationText, TypeFont, New SolidBrush(ForeColor), New Point(10, 5))
- GFX.DrawString(Text, Font, New SolidBrush(ForeColor), New Rectangle(10, 21, Width - 17, Height - 5))
- Else
- GFX.DrawImage(_Image, 12, 4, 16, 16)
- GFX.DrawString(NotificationText, TypeFont, New SolidBrush(ForeColor), New Point(30, 5))
- GFX.DrawString(Text, Font, New SolidBrush(ForeColor), New Rectangle(10, 21, Width - 17, Height - 5))
- End If
- ' Coordinates in which an ellipse will be drawn at on MouseOver event
- CloseCoordinates = New Point(Width - 26, 4)
- If _ShowCloseButton = True Then
- If IsOverClose = True Then
- GFX.FillEllipse(New SolidBrush(BorderColor), New Rectangle(Width - 23, 4, 17, 17))
- End If
- 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})
- End If
- ' Clean up
- CrvBorderPath.Dispose()
- End Sub
- End Class
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment