2015-03-13 143 views
1

我有一個RichTextBox。RichTextBox閃爍着色字

可以說我想在文本中每隔「測試」。 我的問題是不在文本中找到「測試」,我的問題是當我可以看到RichTextBox選擇過程的顏色。

我的色彩功能:

Private Sub DrawSubPart(ByVal StartIndex As Integer, ByVal EndIndex As Integer, ByVal col As Color) 
    Dim save As Integer = TextScreen.SelectionStart 
    TextScreen.SelectionStart = StartIndex     'Here I can see the selection, And I dont want to see it. 
    TextScreen.SelectionLength = EndIndex - EndIndex + 1 
    TextScreen.SelectionColor = col 
    TextScreen.SelectionLength = 0 
    TextScreen.SelectionStart = save 
End Sub 

我試圖抓住的選擇過程,它看起來像:

enter image description here

而且一毫秒之後,它看起來OK:

enter image description here

那麼如何停止選擇「閃爍」?

+0

如果你打算使用這是一個語法高亮我建議你使用ScintillaNET來代替:https://scintillanet.codeplex.com/ – 2015-03-13 18:33:19

回答

1

嘗試使用關閉自定義RichTextBox控件圖紙和滾動事件,而重新格式化的內容:

Public Class RichTextBoxEx 
    Inherits RichTextBox 

    <DllImport("user32.dll")> _ 
    Private Shared Function SendMessage(hWnd As IntPtr, wMsg As Int32, wParam As Int32, ByRef lParam As Point) As IntPtr 
    End Function 

    <DllImport("user32.dll")> _ 
    Private Shared Function SendMessage(hWnd As IntPtr, wMsg As Int32, wParam As Int32, lParam As IntPtr) As IntPtr 
    End Function 

    Const WM_USER As Integer = &H400 
    Const WM_SETREDRAW As Integer = &HB 
    Const EM_GETEVENTMASK As Integer = WM_USER + 59 
    Const EM_SETEVENTMASK As Integer = WM_USER + 69 
    Const EM_GETSCROLLPOS As Integer = WM_USER + 221 
    Const EM_SETSCROLLPOS As Integer = WM_USER + 222 

    Private _ScrollPoint As Point 
    Private _Painting As Boolean = True 
    Private _EventMask As IntPtr 
    Private _SuspendIndex As Integer = 0 
    Private _SuspendLength As Integer = 0 

    Public Sub SuspendPainting() 
    If _Painting Then 
     _SuspendIndex = Me.SelectionStart 
     _SuspendLength = Me.SelectionLength 
     SendMessage(Me.Handle, EM_GETSCROLLPOS, 0, _ScrollPoint) 
     SendMessage(Me.Handle, WM_SETREDRAW, 0, IntPtr.Zero) 
     _EventMask = SendMessage(Me.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero) 
     _Painting = False 
    End If 
    End Sub 

    Public Sub ResumePainting() 
    If Not _Painting Then 
     Me.Select(_SuspendIndex, _SuspendLength) 
     SendMessage(Me.Handle, EM_SETSCROLLPOS, 0, _ScrollPoint) 
     SendMessage(Me.Handle, EM_SETEVENTMASK, 0, _EventMask) 
     SendMessage(Me.Handle, WM_SETREDRAW, 1, IntPtr.Zero) 
     _Painting = True 
     Me.Invalidate() 
    End If 
    End Sub 
End Class 

那麼你的用法是:

RichTextBoxEx1.SuspendPainting() 
Dim save As Integer = RichTextBoxEx1.SelectionStart 
RichTextBoxEx1.SelectionStart = StartIndex 
RichTextBoxEx1.SelectionLength = EndIndex - StartIndex + 1 
RichTextBoxEx1.SelectionColor = Color.Green 
RichTextBoxEx1.SelectionLength = 0 
RichTextBoxEx1.SelectionStart = save 
RichTextBoxEx1.ResumePainting() 
+0

很好用,tnx – 2015-03-13 22:13:08