'/* ' * CodeEditor - RTFBox for SyntaxHighlighting ' * ' * (c) 2009 Christian Ivicevic ' * ' */ Public Class CodeEditor Inherits System.Windows.Forms.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 _CaseSensitive As Boolean = True Private _Language As CodeLanguages = CodeLanguages.Plain Friend Words As New DataTable Private Enum EditMessages LineIndex = 187 LineFromChar = 201 GetFirstVisibleLine = 206 CharFromPos = 215 PosFromChar = 1062 End Enum Public Enum CodeLanguages Plain = 0 MyLang1 = 1 MyLang2 = 1 End Enum Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) MyBase.OnTextChanged(e) ColorVisibleLines() End Sub Public Sub New() AcceptsTab = True InitSyntax() End Sub Function InitSyntax() ClearSyntaxWords() 'AddSyntaxWord("\b()\b", Color.Blue) If CodeLanguage = CodeLanguages.Plain Then ' No Syntax ElseIf CodeLanguage = CodeLanguage.MyLang1 Then AddSyntaxWord("\b(double|float|int|void)\b", Color.Blue) AddSyntaxWord("\b(main|dllmain|winmain)\b", Color.Green) ElseIf CodeLanguage = CodeLanguage.MyLang2 Then AddSyntaxWord("\b(double|float|int|void)\b", Color.Pink) AddSyntaxWord("\b(main|dllmain|winmain)\b", Color.Red) Else ' No Syntax End If Return True End Function Public Function ClearSyntaxWords() Words = New DataTable() Words.Columns.Add("Word") Words.PrimaryKey = New DataColumn() {Words.Columns(0)} Words.Columns.Add("Color") Return True End Function Public Function AddSyntaxWord(ByVal strWord As String, ByVal clrColor As Color) Dim MyRow As DataRow MyRow = Words.NewRow() MyRow("Word") = strWord MyRow("Color") = clrColor.Name Words.Rows.Add(MyRow) Return True End Function Public Sub ColorRtb() Dim FirstVisibleChar As Integer Dim i As Integer = 0 While i < Me.Lines.Length FirstVisibleChar = GetCharFromLineIndex(i) ColorLineNumber(i, FirstVisibleChar) i += 1 End While End Sub Public Sub ColorVisibleLines() Dim FirstLine As Integer = FirstVisibleLine() Dim LastLine As Integer = LastVisibleLine() Dim FirstVisibleChar As Integer If (FirstLine = 0) And (LastLine = 0) Then Exit Sub Else While FirstLine < LastLine FirstVisibleChar = GetCharFromLineIndex(FirstLine) ColorLineNumber(FirstLine, FirstVisibleChar) FirstLine += 1 End While End If End Sub Public Sub ColorLineNumber(ByVal LineIndex As Integer, ByVal lStart As Integer) Dim i As Integer = 0 Dim SelectionAt As Integer = SelectionStart Dim MyRow As DataRow Dim MyI As Integer LockWindowUpdate(Handle.ToInt32) MyI = lStart SelectionStart = MyI SelectionLength = Lines(LineIndex).Length SelectionColor = Color.Black Dim rm As System.Text.RegularExpressions.MatchCollection Dim m As System.Text.RegularExpressions.Match Dim TextA As String = "" If CaseSensitive = False Then TextA = Text.ToLower Else TextA = Text End If For Each MyRow In Words.Rows rm = System.Text.RegularExpressions.Regex.Matches(TextA, MyRow("Word")) For Each m In rm SelectionStart = m.Index SelectionLength = m.Length SelectionColor = Color.FromName(MyRow("color")) Next Next SelectionStart = SelectionAt SelectionLength = 0 SelectionColor = Color.Black LockWindowUpdate(0) End Sub Public Function GetCharFromLineIndex(ByVal LineIndex As Integer) As Integer Return SendMessage(Handle, EditMessages.LineIndex, LineIndex, 0) End Function Public Function FirstVisibleLine() As Integer Return SendMessage(Handle, EditMessages.GetFirstVisibleLine, 0, 0) End Function Public Function LastVisibleLine() As Integer Dim LastLine As Integer = FirstVisibleLine() + (Height / Font.Height) If LastLine > Lines.Length Or LastLine = 0 Then LastLine = Lines.Length End If Return LastLine End Function Public Property CaseSensitive() As Boolean Get Return _CaseSensitive End Get Set(ByVal Value As Boolean) _CaseSensitive = Value End Set End Property Public Property CodeLanguage() As CodeLanguages Get Return _Language End Get Set(ByVal value As CodeLanguages) _Language = value InitSyntax() End Set End Property End Class