Rem 在窗体中添加CommonDialog(名称为CommonDialog1),Command(名称为Command1)。''Rem 窗体代码如下:'Private sBackColor As Long, sRGB As tRGB
Private Type tRGB
hexColor As String
R As Byte
G As Byte
B As Byte
End TypePrivate Sub Command1_Click()
'//通过使用调色板选定颜色
'{
CommonDialog1.ShowColor
sBackColor = CommonDialog1.Color
'}
'
'//分离颜色值到RGB
sRGB.hexColor = Hex(sBackColor)
sRGB.hexColor = Space(Val(6 - Len(sRGB.hexColor))) & sRGB.hexColor
sRGB.hexColor = Replace(sRGB.hexColor, " ", "0")
sRGB.R = Val("&H" & Mid(sRGB.hexColor, 5, 2))
sRGB.G = Val("&H" & Mid(sRGB.hexColor, 3, 2))
sRGB.B = Val("&H" & Mid(sRGB.hexColor, 1, 2))
'}
'
'对选定内容进行RTF处理
'{
Dim temp_RTF As String: temp_RTF = RichTextBox1.SelRTF
Dim cir_Search As Long: cir_Search = 2
'//去背景色处理 {
Do While InStr(1, temp_RTF, "\highlight" & cir_Search) > 0
temp_RTF = Replace(temp_RTF, "\highlight" & cir_Search & " ", "")
cir_Search = cir_Search + 1
Loop
'}
'
'//加背景色处理 {
temp_RTF = Replace(temp_RTF, "{\rtf1\ansi\ansicpg936\deff0{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}", _
"{\rtf1\ansi\ansicpg936\deff0{\fonttbl{\f0\fnil\fcharset134 \'cb\'ce\'cc\'e5;}}" & Chr(10) & _
"{\colortbl;\red" & sRGB.R & "\green" & sRGB.G & "\blue" & sRGB.B & ";}")
temp_RTF = Replace(temp_RTF, RichTextBox1.SelText, "\highlight1" & RichTextBox1.SelText)
RichTextBox1.SelRTF = temp_RTF
temp_RTF = ""
'}
'}
End Sub
VB如何让richtextbox里的文字有背景色参考: http://hi.baidu.com/%BB%FA%CE%F1%CB%D2/blog/item/e64b3fe62e164424b838201e.html
vb6的版本是这样的RichTextBox1.SelColor = vbRed
随便输入文字,点击按钮就把文字变红了:代码: