RichTextBox 在为单词着色时闪烁 [英] RichTextBox flickers while coloring words

查看:72
本文介绍了RichTextBox 在为单词着色时闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RichTextBox.

I have a RichTextBox.

假设我想绘制文本中的每个单词测试".我的问题不是在文本中找到测试",我的问题是当我给单词着色时,我可以看到 RichTextBox 选择过程.

lets say I want to draw every word "test" in the text. My problem is not to find the "test" in the text, My problem is when I color the word I can see the RichTextBox Selection process.

我的颜色函数:

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

我试图捕捉选择过程,它看起来像这样:

I tried to catch the Selection process and it look like that:

一毫秒后它看起来没问题:

And after one millisecond it looks ok:

那么我怎样才能阻止选择闪烁"呢?

So how can I stop the Selection "flickering" ?

推荐答案

尝试使用自定义 RichTextBox 控件,该控件在您重新设置内容格式时关闭绘图和滚动事件:

Try using a custom RichTextBox control that turns off the drawing and scrolling events while you reformat the contents:

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()

这篇关于RichTextBox 在为单词着色时闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆