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
|
Public Class MonLabelBebete
Inherits Label
Private Sub PaintParentBackground(ByVal e As PaintEventArgs)
If Not Me.Parent Is Nothing Then
Dim rect As Rectangle = New Rectangle(Left, Top, Width, Height)
e.Graphics.TranslateTransform(-rect.X, -rect.Y)
Dim pea As PaintEventArgs = New PaintEventArgs(e.Graphics, rect)
pea.Graphics.SetClip(rect)
InvokePaintBackground(Parent, pea)
InvokePaint(Parent, pea)
e.Graphics.TranslateTransform(rect.X, rect.Y)
Else
e.Graphics.FillRectangle(SystemBrushes.Control, ClientRectangle)
End If
End Sub
Protected Overrides Sub OnPaintBackground( _
ByVal pevent As PaintEventArgs)
PaintParentBackground(pevent)
End Sub
Protected Overrides Sub OnTextChanged( _
ByVal e As EventArgs)
MyBase.OnTextChanged(e)
invalidateParent()
End Sub
Private Sub invalidateParent()
If Not Me.Parent Is Nothing Then
' Force le parent a repeindre le rectangle client du label sans en informer
' les controls enfants
Me.Parent.Invalidate(Me.Bounds, True)
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
TextRenderer.DrawText(e.Graphics, Me.Text, _
Me.Font, textPosition, Me.ForeColor)
End Sub
Private Function textPosition() 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)
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