RichTextBox tip-custom highlighted display

Source: Internet
Author: User
Public Sub HighLight () Sub HighLight (RTB As RichTextBox, lColor As Long)
'Add new color to color table
'Add tags \ highlight # and \ highlight0
'Where # is new color number
Dim iPos As Long
Dim strRTF As String
Dim bkColor As Integer

With RTB
IPos =. SelStart
'Bracket selection
. SelText = Chr (& H9D) &. SelText & Chr (& H81)
StrRTF = RTB. TextRTF
'Add new color
BkColor = AddColorToTable (strRTF, lColor)
'Add highlighting
StrRTF = Replace (strRTF, "\ '9d", "\ up1 \ highlight" & CStr (bkColor )&"")
StrRTF = Replace (strRTF, "\ '81", "\ highlight0 \ up0 ")

. TextRTF = strRTF
. SelStart = iPos
End

End Sub

Function AddColorToTable () Function AddColorToTable (strRTF As String, lColor As Long) As Integer
Dim iPos As Long, jpos As Long

Dim ctbl As String
Dim tagColors
Dim nColors As Integer
Dim tagNew As String
Dim I As Integer
Dim iLen As Integer
Dim split1 As String
Dim split2 As String

'Make new color into tag
TagNew = "\ red" & CStr (lColor And & HFF )&_
"\ Green" & CStr (Int (lColor/& H100) And & HFF )&_
"\ Blue" & CStr (Int (lColor/& H10000 ))

'Find colortable
IPos = InStr (strRTF, "{\ colortbl ")

If iPos> 0 then' if table already exists
Jpos = InStr (iPos, strRTF ,";}")
'Color table
Ctbl = Mid (strRTF, iPos + 12, jpos-iPos-12)
'Array of color tags
TagColors = Split (ctbl ,";")
NColors = UBound (tagColors) + 2
'See if our color already exists in table
For I = 0 To UBound (tagColors)
If tagColors (I) = tagNew Then
AddColorToTable = I + 1
Exit Function
End If
Next I
'{\ Fonttbl {\ f0 \ fnil \ fcharset0 Verdana ;}}
'{\ Colortbl; \ red0 \ green0 \ blue0; \ red128 \ green0 \ blue255 ;}

Split1 = Left (strRTF, jpos)
Split2 = Mid (strRTF, jpos + 1)
StrRTF = split1 & tagNew & ";" & split2
AddColorToTable = nColors

Else
'Color table doesn' t exists, let's make one
IPos = InStr (strRTF, "{\ fonttbl") 'ning inning of font table
Jpos = InStr (iPos, strRTF, ";}}") + 2' end of font table
Split1 = Left (strRTF, jpos)
Split2 = Mid (strRTF, jpos + 1)
StrRTF = split1 & "{\ colortbl;" & tagNew & ";}" & split2
AddColorToTable = 1
End If

End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.