1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| Public Class LabelShadowbyDany
Inherits Label
Private cShadowForeColor As Color = Color.WhiteSmoke
Private vDistance As Integer = 2
Public Property ShadowForeColor() As Color
Get
Return cShadowForeColor
End Get
Set(ByVal value As Color)
cShadowForeColor = value
End Set
End Property
Public Property ShadowDistance() As Integer
Get
Return vDistance
End Get
Set(ByVal value As Integer)
vDistance = value
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim bufferImage As Bitmap
bufferImage = New Bitmap(Bounds.Width, Bounds.Width)
Dim gd As Graphics = Graphics.FromImage(bufferImage)
gd.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
gd.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
PaintParentBackground(gd)
gd.DrawString(Text, Font, New SolidBrush(ShadowForeColor), New Point(textPosition(0)))
gd.DrawString(Text, Font, New SolidBrush(ForeColor), New Point(textPosition(1)))
e.Graphics.DrawImage(bufferImage, 0, 0)
gd.Dispose()
bufferImage.Dispose()
End Sub
Private Sub PaintParentBackground(ByVal g As Graphics)
If Not Me.Parent Is Nothing Then
Dim rect As Rectangle = New Rectangle(Left, Top, Width, Height)
g.TranslateTransform(-rect.X, -rect.Y)
Dim pea As PaintEventArgs = New PaintEventArgs(g, rect)
pea.Graphics.SetClip(rect)
InvokePaintBackground(Parent, pea)
g.TranslateTransform(rect.X, rect.Y)
Else
g.FillRectangle(SystemBrushes.Control, ClientRectangle)
End If
End Sub
Protected Overrides Sub OnPaintBackground(ByVal pevent As PaintEventArgs)
' rien
End Sub
Private Function textPosition(ByVal Lequel As Integer) As Point
Dim sTextSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
If sTextSize.Width > Me.Bounds.Width Then
sTextSize.Width = Me.Bounds.Width
End If
If sTextSize.Height > Me.Bounds.Height Then
sTextSize.Height = Me.Bounds.Height
End If
Dim intLeft As Integer = 0
Dim intTop As Integer = 0
Dim intRigth As Integer = Me.Bounds.Width - sTextSize.Width
Dim intBottom As Integer = Me.Bounds.Height - sTextSize.Height
Dim intXCenter As Integer = CInt((Me.Bounds.Width - sTextSize.Width) / 2)
Dim intYCenter As Integer = CInt((Me.Bounds.Height - sTextSize.Height) / 2)
Dim intShadowOffset As Integer = CInt(ShadowDistance)
Select Case Lequel
Case 0
intLeft += intShadowOffset
intTop += intShadowOffset
intRigth += intShadowOffset
intBottom += intShadowOffset
intXCenter += intShadowOffset
intYCenter += intShadowOffset
End Select
Select Case Me.TextAlign
Case ContentAlignment.BottomCenter
Return New Point(intXCenter, intBottom)
Case ContentAlignment.BottomLeft
Return New Point(intLeft, intBottom)
Case ContentAlignment.BottomRight
Return New Point(intRigth, intBottom)
Case ContentAlignment.MiddleCenter
Return New Point(intXCenter, intYCenter)
Case ContentAlignment.MiddleLeft
Return New Point(intLeft, intYCenter)
Case ContentAlignment.MiddleRight
Return New Point(intRigth, intYCenter)
Case ContentAlignment.TopCenter
Return New Point(intXCenter, intTop)
Case ContentAlignment.TopLeft
Return New Point(intLeft, intTop)
Case ContentAlignment.TopRight
Return New Point(intRigth, intTop)
End Select
End Function
End Class |
Partager