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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
Public Class MarkLabelColor : Inherits Control
Private _Keywords As MarkKey()
Public Property KeyWords As MarkKey()
Get
Return _Keywords
End Get
Set(value As MarkKey())
_Keywords = value : 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", 12, FontStyle.Regular)
Dim Lab As New MarkKey("Mark", Color.Blue)
Dim Lab1 As New MarkKey("Label", Color.Red)
Dim Lab2 As New MarkKey("Color", Color.Lime)
Dim Lab3 As New MarkKey("1", Color.Fuchsia)
Me._Keywords = New MarkKey() {Lab, Lab1, Lab2, Lab3}
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, New Point(0, 0), Me.ForeColor)
If Me.Text.Length <= 0 Then Return
For i As Integer = 0 To _Keywords.Length - 1
Dim keyWord As String = _Keywords(i).KeyWord
Dim keyColor As Color = _Keywords(i).KeyColor
Dim keyWidth As Integer = TextRenderer.MeasureText(keyWord, Me.Font).Width
Dim IndexOffset As Integer = Me.Text.IndexOf(keyWord)
While IndexOffset >= 0
Dim StrFront As String = Me.Text.Substring(0, IndexOffset)
If StrFront.Length > 0 Then
Dim strWidth As Integer = TextRenderer.MeasureText(StrFront & keyWord, Me.Font).Width
TextRenderer.DrawText(e.Graphics, keyWord, Me.Font, New Point((strWidth - keyWidth), 0), keyColor)
Else
TextRenderer.DrawText(e.Graphics, keyWord, Me.Font, New Point(0, 0), keyColor)
End If
IndexOffset += keyWord.Length
If IndexOffset >= Me.Text.Length Then Exit While
Dim StrBehind As String = Me.Text.Substring(IndexOffset)
If StrBehind.Length <= 0 Then Exit While
Dim index2 As Integer = StrBehind.IndexOf(keyWord)
If index2 < 0 Then Exit While
IndexOffset += index2
End While
Next
End Sub
'dans Form1_Load :
'MarkLabelColor1.ResetKeyWord(Label, color.red)
Public Sub ResetKeyWord(ByVal keyString As String, ByVal keyColor As Color)
Me._KeyWords = New MarkKey() {New MarkKey(keyString, keyColor)}
Me.Refresh()
End Sub
'dans Form1_Load:
'MarkLabelColor1.ResetAllKeyWord({"Label", "1"}, color.red)
Public Sub ResetAllKeyWord(ByVal keyList As String(), ByVal MyColor As Color)
Dim query As IEnumerable(Of MarkKey) = From str In keyList Select New MarkKey(str, MyColor)
Me._KeyWords = query.ToArray
Me.Refresh()
End Sub
'dans Form1_Load:
'MarkLabelColor1.ResetAllKeyWordColor({("Label", Color.Blue), ("1", Color.Red)})
Public Sub ResetAllKeyWordColor(MyList() As (Data As String, MyColor As Color))
Dim query As IEnumerable(Of MarkKey) = From str In MyList Select New MarkKey(str.Data, str.MyColor)
Me._Keywords = query.ToArray
Me.Refresh()
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
Protected Overrides Sub OnFontChanged(e As EventArgs)
MyBase.OnFontChanged(e)
Me.AdjustSize(Me)
MyBase.Invalidate()
End Sub
Private Sub AdjustSize(Label As MarkLabelColor)
Dim x As Integer = Label.Width - 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)
Label.MinimumSize = New Size(size.Width + x, size.Height + y)
Label.MaximumSize = New Size(size.Width + x, size.Height + y)
End Sub
End Class
Public Class MarkKey
Public Sub New(ByVal word As String, ByVal MyColor As Color)
_KeyWord = word
_KeyColor = MyColor
End Sub
Private _KeyWord As String
Public Property KeyWord As String
Get
Return _KeyWord
End Get
Set(value As String)
If Not String.IsNullOrEmpty(value) Then
_KeyWord = value
Else
Throw New ArgumentException("Veuillez entrer un texte pour étre en couleur !")
End If
End Set
End Property
Private _KeyColor As Color
Public Property KeyColor As Color
Get
Return _KeyColor
End Get
Set(value As Color)
If Not IsNothing(value) Then
_KeyColor = value
Else
Throw New ArgumentException("Veuillez choisir une couleur par rapport au texte !")
End If
End Set
End Property
End Class |
Partager