我靠,RICH TEXTBOX的速度你知道么?我曾经用过他来作过语法高亮显示的功能,速度我实在是不敢恭维
解决方案 »
- 求助:vb6.0中API浏览器看不能查找函数
- 如何判断一图像是位图还是图标?
- 烦劳大侠指点...谢谢
- 對加了SMTP認証的smtp郵件服務器在vb的email程序中應該設置﹖
- 怎么样能作出象VB6里面的“查找”(就是那个小望远镜)~~~~
- 有关视频imager.ocx控件?
- 请大家帮帮忙,如何将tiff图像保存为jpg图像。
- 我想定制一个浏览器,没有右上角按钮的那种-即没有[最小化][最大化][关闭],不知用vb应如何做?
- 关于Ado.connection的问题,高手请进!!!
- 请问如何用代码得到ACCESS数据库中的表的数量及名称,用ADO
- !!!!!关于使用双屏或一屏加电视的使用!!!!
- 请问谁可以提供一些正则表达式的使用方法和资源?
Option Explicit
'------- Colorize Module ------'
' '
' By: M. Schweighauser '
' Date: 24.06. 2000 '
' E-Mail: [email protected] '
' '
'------- Colorize Module ------''------ Code Information ------'
' '
'The First/LastVisible Code is '
' from a German-Newsgroup '
' '
'------ Code Information ------''// Examples for the KeyWords-Variables (VB):
' |#Const|#Else|#ElseIf|#End|#If|Alias|Alias|And|As|Base|Binary|Boolean|Byte|ByVal|Call|Case|CBool|CByte|CCur|CDate|CDbl|CDec|CInt|CLng|Close|Compare|Const|CSng|CStr|Currency|CVar|CVErr|Decimal|Declare|DefBool|DefByte|DefCur|DefDate|DefDbl|DefDec|DefInt|DefLng|DefObj|DefSng|DefStr|DefVar|Dim|Do|Double|Each|Else|ElseIf|End|Enum|Eqv|Erase|Error|Exit|Explicit|False|For|Function|Get|Global|GoSub|GoTo|If|Imp|In|Input|Input|Integer|Is|LBound|Let|Lib|Like|Line|Lock|Long|Loop|LSet|Name|New|Next|Not|Object|On|Open|Option|Or|Output|Print|Private|Property|Public|Put|Random|Read|ReDim|Resume|Return|RSet|Seek|Select|Set|Single|Spc|Static|String|Stop|Sub|Tab|Then|Then|True|Type|UBound|Unlock|Variant|Wend|While|With|Xor|Nothing|To|'// Win API
Public Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Public Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Public Declare Function GetTextMetrics Lib "gdi32" Alias "GetTextMetricsA" (ByVal hdc As Long, lpMetrics As TEXTMETRIC) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long) As Long
'// Win API Const
Private Const EM_GETFIRSTVISIBLELINE = &HCE
Private Const EM_LINEINDEX = &HBB
Private Const EM_GETRECT = &HB2
Private Const WM_GETFONT = &H31'// Variables
Public KeyWords'//Variables for FirstVisible/LastVisibles
Dim FirstVisibleLine As Long
Dim LastVisibleLine As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypePrivate Type TEXTMETRIC
tmHeight As Long
tmAscent As Long
tmDescent As Long
tmInternalLeading As Long
tmExternalLeading As Long
tmAveCharWidth As Long
tmMaxCharWidth As Long
tmWeight As Long
tmOverhang As Long
tmDigitizedAspectX As Long
tmDigitizedAspectY As Long
tmFirstChar As Byte
tmLastChar As Byte
tmDefaultChar As Byte
tmBreakChar As Byte
tmItalic As Byte
tmUnderlined As Byte
tmStruckOut As Byte
tmPitchAndFamily As Byte
tmCharSet As Byte
End Type
Public Sub Colorize(RTFBox As RichTextBox, CommentColor, StringColor, KeysColor) Dim lTextSelPos As Long, lTextSelLen As Long
'// Save the cursor position
lTextSelPos = RTFBox.SelStart
lTextSelLen = RTFBox.SelLength
'// Lock the WindowUpdate of the ReichTextBox
LockWindowUpdate RTFBox.hWnd
'On Error GoTo ErrHandler
Dim i As Long
Dim sBuffer As String, lBufferLen As Long
Dim lSelPos As Long, lSelLen As Long
Dim sTempBuffer As String
Dim sSearchChar As String, lSearchCharLen As Long
With RTFBox
sBuffer = .Text & " "
lBufferLen = Len(sBuffer)
sTempBuffer = ""
For i = FirstVisibleChar(RTFBox) To LastVisibleChar(RTFBox, lBufferLen)
Select Case Asc(Mid(sBuffer, i, 1))
Case 34 '// Stringtexts -> " ... "
.SelStart = i - 1
i = InStr(i + 1, sBuffer, """", 1)
.SelLength = i - .SelStart
.SelColor = StringColor
Case 47, 39, 60 '// Comments Examples:
If Mid(sBuffer, i, 2) = "//" Then '// C Comment
sSearchChar = vbCrLf
lSearchCharLen = 0
ElseIf Mid(sBuffer, i, 2) = "/*" Then '// C++ Comment
sSearchChar = "*/"
lSearchCharLen = 2
ElseIf Mid(sBuffer, i, 4) = "<!--" Then '// HTML Comment
sSearchChar = "//-->"
lSearchCharLen = 5
ElseIf Mid(sBuffer, i, 1) = "'" Then '// VB Comment
sSearchChar = vbCrLf
lSearchCharLen = 0
Else '// No Comment
GoTo ExitComment
End If
'// Kill TempBuffer
sTempBuffer = ""
'// Colorize the comment string
.SelStart = i - 1
lSelLen = InStr(i, sBuffer, sSearchChar) + lSearchCharLen
If lSelLen <> lSearchCharLen Then '// FileEnd ?
lSelLen = lSelLen - i
Else
lSelLen = lBufferLen - i
End If
.SelLength = lSelLen
.SelColor = CommentColor
i = .SelStart + .SelLength
ExitComment:
Case 97 To 122, 65 To 90, 35
'// a to z , A to Z , #
'// Only this char can be colorize
If sTempBuffer = "" Then lSelPos = i
sTempBuffer = sTempBuffer & Mid(sBuffer, i, 1)
Case Else
If Trim(sTempBuffer) <> "" Then
.SelStart = lSelPos - 1
.SelLength = Len(sTempBuffer)
If InStr(1, KeyWords, "|" & sTempBuffer & "|", 1) <> 0 Then
.SelColor = KeysColor
End If
End If
sTempBuffer = ""
End Select
Next
End With
ErrHandler:
'// Set the Cursor to the old position
RTFBox.SelStart = lTextSelPos
RTFBox.SelLength = lTextSelLen
'// Unlock the WindoUpdate-Lock
LockWindowUpdate 0
End Sub
Private Function FirstVisibleChar(RTFBox As RichTextBox) As Long
FirstVisibleLine = SendMessage(RTFBox.hWnd, EM_GETFIRSTVISIBLELINE, 0, 0&)
FirstVisibleChar = SendMessageByNum(RTFBox.hWnd, EM_LINEINDEX, FirstVisibleLine, 0&)
If FirstVisibleChar = 0 Then FirstVisibleChar = 1
End Function
Private Function LastVisibleChar(RTFBox As RichTextBox, LenFile As Long) As Long
Dim rc As RECT
Dim tm As TEXTMETRIC
Dim hdc As Long
Dim lFont As Long
Dim OldFont As Long
Dim di As Long
Dim lc As Long
Dim VisibleLines As Long
lc = SendMessage(RTFBox.hWnd, EM_GETRECT, 0, rc)
lFont = SendMessage(RTFBox.hWnd, WM_GETFONT, 0, 0)
hdc = GetDC(RTFBox.hWnd)
If lFont <> 0 Then OldFont = SelectObject(hdc, lFont)
di = GetTextMetrics(hdc, tm)
If lFont <> 0 Then lFont = SelectObject(hdc, OldFont)
VisibleLines = (rc.Bottom - rc.Top) / tm.tmHeight
di = ReleaseDC(RTFBox.hWnd, hdc)
LastVisibleLine = SendMessage(RTFBox.hWnd, EM_GETFIRSTVISIBLELINE, 0, 0&)
LastVisibleLine = LastVisibleLine + VisibleLines
LastVisibleChar = SendMessageByNum(RTFBox.hWnd, EM_LINEINDEX, LastVisibleLine, 0&)
If LastVisibleChar = -1 Or LastVisibleChar = 0 Then LastVisibleChar = LenFile
End Function这样调用:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'// Only Colorize-Keywords, when the user press the ReturnKey
'// --> So whe save time
If KeyAscii = vbKeyReturn Then Colorize frmMain.Text1, vbGreen, vbRed, vbBlue
Text1.SelColor = vbBlack
End Sub
其中的Text1其实是一个RichTextBox
VC、VB、ULTRAEDIT、PLUSEDIT等的编辑控件是自己特制的并非RICHEDIT(不信把RICHED.dll、riched32.dll、riched20.dll删掉看看他们能否工作)。
在www.winmain.com下有个CODEMAX可用。
KeyWords="¦#Const¦#Else¦#ElseIf¦#End¦#If¦Alias¦Alias¦And¦As¦Base¦Binary¦Boolean¦Byte¦ByVal¦Call¦Case¦CBool¦CByte¦CCur¦CDate¦CDbl¦CDec¦CInt¦CLng¦Close¦Compare¦Const¦CSng¦CStr¦Currency¦CVar¦CVErr¦Decimal¦Declare¦DefBool¦DefByte¦DefCur¦DefDate¦DefDbl¦DefDec¦DefInt¦DefLng¦DefObj¦DefSng¦DefStr¦DefVar¦Dim¦Do¦Double¦Each¦Else¦ElseIf¦End¦Enum¦Eqv¦Erase¦Error¦Exit¦Explicit¦False¦For¦Function¦Get¦Global¦GoSub¦GoTo¦If¦Imp¦In¦Input¦Input¦Integer¦Is¦LBound¦Let¦Lib¦Like¦Line¦Lock¦Long¦Loop¦LSet¦Name¦New¦Next¦Not¦Object¦On¦Open¦Option¦Or¦Output¦Print¦Private¦Property¦Public¦Put¦Random¦Read¦ReDim¦Resume¦Return¦RSet¦Seek¦Select¦Set¦Single¦Spc¦Static¦String¦Stop¦Sub¦Tab¦Then¦Then¦True¦Type¦UBound¦Unlock¦Variant¦Wend¦While¦With¦Xor¦Nothing¦To¦"Colorize frmMain.RichText1, vbGreen, vbRed, vbBlue马上就能看到效果
|#Const|#Else|#ElseIf|#End|#If|Alias|Alias|And|As|Base|Binary|Boolean|Byte|ByVal|Call|Case|CBool|CByte|CCur|CDate|CDbl|CDec|CInt|CLng|Close|Compare|Const|CSng|CStr|Currency|CVar|CVErr|Decimal|Declare|DefBool|DefByte|DefCur|DefDate|DefDbl|DefDec|DefInt|DefLng|DefObj|DefSng|DefStr|DefVar|Dim|Do|Double|Each|Else|ElseIf|End|Enum|Eqv|Erase|Error|Exit|Explicit|False|For|Function|Get|Global|GoSub|GoTo|If|Imp|In|Input|Input|Integer|Is|LBound|Let|Lib|Like|Line|Lock|Long|Loop|LSet|Name|New|Next|Not|Object|On|Open|Option|Or|Output|Print|Private|Property|Public|Put|Random|Read|ReDim|Resume|Return|RSet|Seek|Select|Set|Single|Spc|Static|String|Stop|Sub|Tab|Then|Then|True|Type|UBound|Unlock|Variant|Wend|While|With|Xor|Nothing|To|
不可能吧,我在C++里用的好好的。
自带的例子在装载时就出错了:class not registered.Looking for object with CLSID:{..............................
看你的出错信息,在VB的例子里应该是用OCX而非DLL的,那得注册。
把Colorize过程有关的代码放在一个bas中
在你的窗口当中放一个Richtext控件RichText1
输入Hello World
然后
KeyWords="¦Hello¦"
Colorize RichText1, vbGreen, vbRed, vbBlue
看看Hello是不是变色了(蓝色)?
这个Colorrize过程还是相当不错的,颜色的顺序依次是注释,字符串,关键字
18S,比网上的相关例子快两S左右
怎么也难以提速了
我想可能的话就只有两个方法了
一种是多重链表存放关键词(现在关键词为257个)
可惜这个VB是没法做的,我现在用的是两维数组
再一个就是对RichEdit的取词以用高亮提速了
但现在已经存在字符串中进行查找了不知Delphi和VC是如何做的:(