这是一个 RichTextBox 整体加颜色的函数。传入 普通文本,返回
Rich文本
BUG: 以 \ 开头的会造成出现一个空行,并且导致 显示一个标志。
程序代码:
Public Function 彩色化(cs As String) As String
Dim i As Long, j As String
Dim js() As String
j = cs & vbCrLf
j = Replace(j, "\", "\\") '单\换成双\\
js = Split(cs, vbCrLf)
If UBound(js) > 1000 Then
If MsgBox("脚本非常长,进行命令标记需要很长的时间,是否继续?", vbInformation + vbOKCancel + vbDefaultButton2, 标题) = vbCancel Then
彩色化 = cs
Exit Function
End If
End If
For i = 0 To UBound(js)
If Left(Trim(js(i)), 1) = ";" Then
j = Replace(j, js(i), "\cf1" & js(i) & "\cf0")
'ElseIf Left(Trim(js(i)), 1) = "#" Then
' j = Replace(j, js(i), "\cf2" & js(i) & "\cf0")
ElseIf Left(Trim(js(i)), 2) = "[~" Then
j = Replace(j, Mid(js(i), 2, Len(js(i)) - 2), "\cf4" & Mid(js(i), 2, Len(js(i)) - 2) & "\cf0")
End If
Next i
'j = Replace(j, js(i), "\cf1" & js(i) & "\cf0")
For i = 0 To UBound(命令)
With 命令(i)
If Len(.命令) > 2 Then
Select Case Left(.命令, 1)
Case "#"
j = Replace(j, .命令, "\cf2 " & .命令 & "\cf0 ")
Case "\"
Case "."
Case Else
If InStr(1, j, .命令 & " ") > 0 Then
j = Replace(j, .命令 & " ", "\cf3 " & .命令 & " \cf0 ", , , vbTextCompare)
End If
End Select
End If
End With
Next i
Call 取标记(txtbox.Text)
On Error GoTo err1:
For i = 0 To UBound(标记)
With 标记(i)
j = Replace(j, "[@" & .标记名称 & "]", "[\cf4 @" & .标记名称 & "\cf0 ]")
j = Replace(j, " @" & .标记名称 & " ", " \cf4 @" & .标记名称 & " \cf0 ")
j = Replace(j, "@" & .标记名称 & ">", "\cf4 @" & .标记名称 & "\cf0 >")
End With
Next i
err2:
On Error GoTo 0
j = Replace(j, vbCrLf, vbCrLf & "\par ") '换回车符
j = Replace(j, "\par \cf", "\par\cf") '换掉标记中间的空格
j = "\viewkind4\uc1\pard\lang2052\f0\fs21" & j '加头,倒次序加,显示头,颜色表,RTF头
j = "{\colortbl ;\red0\green128\blue64;\red128\green0\blue64;\red0\green0\blue128;\red128\green0\blue255;}" & vbCrLf & j
j = "{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}" & vbCrLf & j
j = j & "}" '加结尾
彩色化 = j
Exit Function
err1:
GoTo err2:
End Function