voici comment insère multiple couleur dans un label
une fois entrer votre texte dans label aller dans colorMutiple et choisir le nombre de couleur et dans value choisir la couleur.
voici la classe :
Imports System.Text.RegularExpressions
Public Class MultipleColorLabel : Inherits Control
Private _Multuplecolor As Color()
Public Property ColorMultiple As Color()
Get
Return _Multuplecolor
End Get
Set(value As Color())
_Multuplecolor = value
Me.Invalidate()
End Set
End Property
Public Sub New()
SetStyle(ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint, True)
MyBase.Text = Me.Name
Me.Font = New Font("Times New Roman", 10, FontStyle.Regular)
Me._Multuplecolor = {Color.Black}
End Sub
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter Or TextFormatFlags.NoPadding Or TextFormatFlags.NoClipping
Using format As StringFormat = New StringFormat()
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
Dim mc As MatchCollection = Regex.Matches(MyBase.Text, "[^\s]+")
Dim ranges As CharacterRange() = mc.Cast(Of Match)().[Select](Function(m) New CharacterRange(m.Index, m.Length)).ToArray()
format.SetMeasurableCharacterRanges(ranges)
Dim regions As Region() = e.Graphics.MeasureCharacterRanges(MyBase.Text, Me.Font, Me.ClientRectangle, format)
For i As Integer = 0 To ranges.Length - 1
Dim WordBounds As Rectangle = Rectangle.Round(regions(i).GetBounds(e.Graphics))
Dim word As String = MyBase.Text.Substring(ranges(i).First, ranges(i).Length)
If _Multuplecolor.Length = ranges.Length Then
TextRenderer.DrawText(e.Graphics, word, Me.Font, WordBounds, _Multuplecolor(i), flags)
Else
TextRenderer.DrawText(e.Graphics, word, Me.Font, WordBounds, Color.Black, flags)
End If
Next
End Using
End Sub
Protected Overrides Sub OnAutoSizeChanged(e As System.EventArgs)
MyBase.OnAutoSizeChanged(e)
Me.AdjustSize(Me)
MyBase.Invalidate()
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
MyBase.OnTextChanged(e)
Me.AdjustSize(Me)
MyBase.Invalidate()
End Sub
Private Sub AdjustSize(Label As MultipleColorLabel)
Dim x As Integer = (Label.Width + 15) - Label.Size.Width
Dim y As Integer = Label.Height - Label.Size.Height
Dim size As Size = TextRenderer.MeasureText(Label.Text, Label.Font)
Label.ClientSize = New Size(size.Width + x, size.Height + y)
End Sub
End Class
Aucun commentaire:
Enregistrer un commentaire