Imports System.Drawing.Drawing2D
Public Class GradientLabel
Inherits System.Windows.Forms.Label
Public Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
'TextRender method.
Me._sf = TextRenderer.MeasureText(value, Me.Font)
'MeasureString method.
'Using g As Graphics = Me.CreateGraphics()
' Me._sf = g.MeasureString(value, Me.Font)
'End Using
End Set
End Property
Public Overrides Property Font As System.Drawing.Font
Get
Return MyBase.Font
End Get
Set(ByVal value As System.Drawing.Font)
MyBase.Font = Value
Me.Invalidate()
End Set
End Property
Private _ColorHigh As Color = Color.WhiteSmoke
Public Property ColorHigh As Color
Get
Return Me._ColorHigh
End Get
Set(ByVal value As Color)
Me._ColorHigh = value
Me.Invalidate()
End Set
End Property
Private _ColorLow As Color = Color.Silver
Public Property ColorLow As Color
Get
Return Me._ColorLow
End Get
Set(ByVal value As Color)
Me._ColorLow = value
Me.Invalidate()
End Set
End Property
Dim _sf As SizeF = New SizeF(0, 0)
Private ReadOnly Property sf() As SizeF
Get
Return Me._sf
End Get
End Property
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim rfb As RectangleF 'Brush area rectangle
Dim rfa As RectangleF = New RectangleF(0, 0, Me.Width, Me.Height+16) 'String area rectangle.
Dim pf As PointF
Dim gp As New GraphicsPath
Using Format As New StringFormat()
Select Case Me.TextAlign
Case ContentAlignment.TopLeft
Format.LineAlignment = StringAlignment.Near
Format.Alignment = StringAlignment.Near
Case ContentAlignment.TopCenter
Format.LineAlignment = StringAlignment.Near
Format.Alignment = StringAlignment.Center
Case ContentAlignment.TopRight
Format.LineAlignment = StringAlignment.Near
Format.Alignment = StringAlignment.Far
Case ContentAlignment.MiddleLeft
Format.LineAlignment = StringAlignment.Center
Format.Alignment = StringAlignment.Near
Case ContentAlignment.MiddleCenter
Format.LineAlignment = StringAlignment.Center
Format.Alignment = StringAlignment.Center
Case ContentAlignment.MiddleRight
Format.LineAlignment = StringAlignment.Center
Format.Alignment = StringAlignment.Far
Case ContentAlignment.BottomLeft
Format.LineAlignment = StringAlignment.Far
Format.Alignment = StringAlignment.Near
Case ContentAlignment.BottomCenter
Format.LineAlignment = StringAlignment.Far
Format.Alignment = StringAlignment.Center
Case ContentAlignment.BottomRight
Format.LineAlignment = StringAlignment.Far
Format.Alignment = StringAlignment.Far
End Select
gp.AddString(Me.Text, Me.Font.FontFamily, Me.Font.Style, sf.Height, rfa, Format)
Select Format.LineAlignment
Case StringAlignment.Near: pf = New PointF(0, 0)
Case StringAlignment.Center: pf = New PointF(0, CSng(Me.Height/2-(gp.GetBounds.Height+24)/2))
Case Stringalignment.Far: pf = New PointF(0, CSng(Me.Height-(gp.GetBounds.Height+24)))
End Select
rfb = New RectangleF(pf, New SizeF(Me.Width, gp.GetBounds.Height+24))
e.Graphics.SmoothingMode = SmoothingMode.HighQuality 'Change the smoothing mode before doing any actual drawing.
'e.Graphics.FillRectangle(Brushes.Black, rfb) 'Brush indicator
Using base As New LinearGradientBrush(rfb, ColorHigh, ColorLow, 90),
border As New LinearGradientBrush(rfb, ColorHigh, ColorHigh, 90)
e.Graphics.FillPath(base, gp)
e.Graphics.DrawPath(New Pen(border, 2), gp)
End Using
End Using
End Sub
End Class