Public Class EditorTesto
Inherits RichTextBox
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hWnd As Integer) As Integer
Private _Highlighting As Boolean = True
Public Property Highlighting() As Boolean
Get
Return _Highlighting
End Get
Set(ByVal value As Boolean)
_Highlighting = value
If value = True Then
Me.ColoraTutto()
Else
Me.ForeColor = Color.Black
LockWindowUpdate(Me.Handle.ToInt32)
Me.Select(0, Me.Text.Length)
Me.SelectionColor = Color.Black
Me.DeselectAll()
LockWindowUpdate(0)
End If
End Set
End Property
Private Sub Me_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.Control = True And e.KeyCode = Keys.V Then
e.SuppressKeyPress = False
Try
Me.Incolla()
Catch ex As Exception
End Try
Else
If Highlighting = True Then
LockWindowUpdate(Me.Handle.ToInt32)
Me.ColoraRiga(Me.GetLineFromCharIndex(Me.SelectionStart))
LockWindowUpdate(0)
End If
End If
End Sub
Sub ColoraTutto()
LockWindowUpdate(Me.Handle.ToInt32)
For Indice As Integer = 0 To Me.Lines.Length
Me.ColoraRiga(Indice)
Next
LockWindowUpdate(0)
End Sub
Sub ColoraRiga(ByVal Riga As Integer)
Try
If Me.Lines(Riga).Length = 0 Then
Exit Sub
End If
Catch ex As Exception
Exit Sub
End Try
Dim posCursore As Integer = Me.SelectionStart
Dim TagAperto As Boolean = False
Dim ColoreCorr As Color
Dim prossimoColore As Color = Nothing
Dim SeProssimoColore As Boolean = False
For Indice As Integer = 0 To Me.Lines(Riga).Length - 1
If Me.Lines(Riga).Chars(Indice) = "<" Then
SeProssimoColore = False
ColoreCorr = My.Settings.EditorTestoColoreTags
TagAperto = True
ElseIf Me.Lines(Riga).Chars(Indice) = " " Then
SeProssimoColore = False
If TagAperto = True Then
ColoreCorr = My.Settings.EditorTestoColoreAttributi
End If
ElseIf Me.Lines(Riga).Chars(Indice) = "=" Then
SeProssimoColore = False
If TagAperto = True Then
ColoreCorr = My.Settings.EditorTestoColoreValori
End If
ElseIf Me.Lines(Riga).Chars(Indice) = "[" Then
SeProssimoColore = False
If TagAperto = True Then
ColoreCorr = My.Settings.EditorTestoColoreQuadre
End If
ElseIf Me.Lines(Riga).Chars(Indice) = "]" Then
SeProssimoColore = False
If TagAperto = True Then
ColoreCorr = My.Settings.EditorTestoColoreQuadre
prossimoColore = My.Settings.EditorTestoColoreTags
SeProssimoColore = True
End If
ElseIf Me.Lines(Riga).Chars(Indice) = "]" Then
ColoreCorr = My.Settings.EditorTestoColoreQuadre
prossimoColore = Color.Black
SeProssimoColore = True
ElseIf Me.Lines(Riga).Chars(Indice) = "-" Then
Try
If Me.Lines(Riga).Substring(Indice - 2).StartsWith("<!--") Then
TagAperto = False
ColoreCorr = My.Settings.EditortestoColoreCommenti
End If
Catch ex As Exception
End Try
ElseIf Me.Lines(Riga).Chars(Indice) = ">" Then
ColoreCorr = My.Settings.EditorTestoColoreTags
prossimoColore = Color.Black
SeProssimoColore = True
TagAperto = False
ElseIf SeProssimoColore = True Then
ColoreCorr = prossimoColore
SeProssimoColore = False
End If
Me.Select(Me.GetFirstCharIndexFromLine(Riga) + Indice, 1)
Me.SelectionColor = ColoreCorr
Me.DeselectAll()
Next
Me.SelectionStart = posCursore
End Sub
Sub Incolla()
If Me.SelectionLength <> 0 Then
Me.Text.Remove(Me.SelectionStart, Me.SelectionLength)
End If
Me.Text.Insert(Me.SelectionStart, My.Computer.Clipboard.GetText)
If Highlighting = True Then
Me.ColoraTutto()
End If
End Sub
End Class