Imports HWND = System.IntPtr
Public Class Form_Rules
Private Const WM_SETREDRAW As Integer = &HB
Private Shared Function SendMessage(ByVal hwnd As HWND, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
End Function
Dim index As Integer = 0
Private Sub Form_Rules_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
RichTextBox.WordWrap = False
End Sub
Private Sub RichTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox.TextChanged
SendMessage(RichTextBox.Handle, WM_SETREDRAW, 0, IntPtr.Zero)
RichTextBox.SelectAll()
RichTextBox.SelectionColor = Color.Black
Dim keystr As String = "and,end,if,then,or,else,+,true,false"
For i = 0 To UBound(keystr.Split(",")) - 1
Call GetKey(keystr.Split(",")(i), RichTextBox.Text)
RichTextBox.Select(index, 0)
RichTextBox.SelectionColor = Color.Black
Next
SendMessage(RichTextBox.Handle, WM_SETREDRAW, 1, IntPtr.Zero)
' RichTextBox.Refresh()
End Sub
Public Function GetKey(ByVal P As String, ByVal S As String) As Integer
Dim CNT As Integer = 0
Dim M As Integer = P.Length
Dim N As Integer = S.Length
Dim SS As Char() = S.ToCharArray()
Dim PP As Char() = P.ToCharArray()
If M > N Then Return 0
For I = 0 To N - M + 1
Dim J As Integer = 0
For J = 0 To M
Try
If SS(I + J) <> PP(J) Then
Exit For
End If
Catch ex As Exception
End Try
If J = P.Length Then
RichTextBox.Select(I, P.Length)
RichTextBox.SelectionColor = Color.Red
CNT = CNT + 1
End If
Next
Next
Return CNT
End Function
Public Class Form_Rules
Private Const WM_SETREDRAW As Integer = &HB
Private Shared Function SendMessage(ByVal hwnd As HWND, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
End Function
Dim index As Integer = 0
Private Sub Form_Rules_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
RichTextBox.WordWrap = False
End Sub
Private Sub RichTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles RichTextBox.TextChanged
SendMessage(RichTextBox.Handle, WM_SETREDRAW, 0, IntPtr.Zero)
RichTextBox.SelectAll()
RichTextBox.SelectionColor = Color.Black
Dim keystr As String = "and,end,if,then,or,else,+,true,false"
For i = 0 To UBound(keystr.Split(",")) - 1
Call GetKey(keystr.Split(",")(i), RichTextBox.Text)
RichTextBox.Select(index, 0)
RichTextBox.SelectionColor = Color.Black
Next
SendMessage(RichTextBox.Handle, WM_SETREDRAW, 1, IntPtr.Zero)
' RichTextBox.Refresh()
End Sub
Public Function GetKey(ByVal P As String, ByVal S As String) As Integer
Dim CNT As Integer = 0
Dim M As Integer = P.Length
Dim N As Integer = S.Length
Dim SS As Char() = S.ToCharArray()
Dim PP As Char() = P.ToCharArray()
If M > N Then Return 0
For I = 0 To N - M + 1
Dim J As Integer = 0
For J = 0 To M
Try
If SS(I + J) <> PP(J) Then
Exit For
End If
Catch ex As Exception
End Try
If J = P.Length Then
RichTextBox.Select(I, P.Length)
RichTextBox.SelectionColor = Color.Red
CNT = CNT + 1
End If
Next
Next
Return CNT
End Function
End Class
防止闪烁:
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As IntPtr) As IntPtr
LockWindowUpdate(RichTextBox.Handle)
LockWindowUpdate(0)
本文介绍了一个使用VB.NET实现的简单文本编辑器代码高亮功能。该示例通过禁用和启用窗口重绘来减少闪烁,并针对特定关键字进行颜色标记。文中还展示了如何在文本框内容改变时刷新显示。

6062

被折叠的 条评论
为什么被折叠?



