Hoi,
hab mich schon öfters mit dem Thema beschäftigt.
Der Code ist nich sorgfältig getestet. Aber er läuft soweit ganz gut.
Das doofe ist aber die RichTextBox selbst. Dadruch das sie so langsam ist, sieht man öfters ein flackern der markierten stellen ...
Deswegen habe ich es damals so gemacht, dass ich den Text erst dann gefärbt habe, wenn sich die aktuelle Zeile verändert hat (eig. genauso wie in VB6). Wenn man eine Zeile verändert hat, hat diese sich komplett schwarz gefärbt (auch wie in VB6).
Hier jetzt ein bisschen einfacher:
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" ( _
ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_GETSEL = &HB0
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_GETFIRSTVISIBLELINE = &HCE
Private Const MAX_CHAR_COUNT = 40 '==> Anzahl der Zeichen
Private Function GetCurPos(RichTextBox As RichTextBox, _
Optional ByRef lngLine As Long, _
Optional ByRef lngCursorPos As Long, _
Optional ByRef lngFirstChar As Long, _
Optional ByRef lngLineLength As Long) As Long
lngCursorPos = SendMessage(RichTextBox.hwnd, EM_GETSEL, 0, ByVal 0&) \ _
65536
lngLine = SendMessage(RichTextBox.hwnd, EM_LINEFROMCHAR, lngCursorPos, _
ByVal 0&)
lngFirstChar = SendMessage(RichTextBox.hwnd, EM_LINEINDEX, lngLine, _
ByVal 0&)
lngLineLength = SendMessage(RichTextBox.hwnd, EM_LINELENGTH, _
lngFirstChar, ByVal 0&)
GetCurPos = RichTextBox.SelStart - lngFirstChar
End Function
Private Sub RichTextBox1_Change()
Dim lngLine As Long
Dim lngCursorPos As Long
Dim lngFirstChar As Long
Dim lngLineLength As Long
Dim lngCurPos As Long
lngCurPos = GetCurPos(RichTextBox1, lngLine, lngCursorPos, _
lngFirstChar, lngLineLength)
Dim lngSelStart As Long
lngSelStart = RichTextBox1.SelStart
RichTextBox1.SelStart = lngFirstChar
RichTextBox1.SelLength = lngLineLength
RichTextBox1.SelColor = vbBlack
RichTextBox1.SelStart = lngSelStart
If lngLineLength > MAX_CHAR_COUNT Then
RichTextBox1.SelStart = lngFirstChar + 40
RichTextBox1.SelLength = lngLineLength - 40
RichTextBox1.SelColor = vbRed
RichTextBox1.SelStart = lngSelStart
End If
End Sub Mit freundlichen Gr??en,
Andy G. |