C#:在不选择文本的情况下更改WinForm RichTextBox的字体样式

我在我的代码中使用RichTextBox ,我在其中显示语法突出显示的代码。 现在,在每次按键时,我都必须重新解析所有令牌并重新为它们重新着色。 但是,在WinForm richtextbox为单个单词着色的唯一方法是逐个选择这些单词并使用SelectionFont对它们进行着色。

但是如果用户输入的速度非常快,那么我选择单个单词会导致非常明显的闪烁(所选单词具有Windows蓝色背景,导致闪烁)。 有没有什么方法可以在不选择它们的情况下为单个单词着色(从而在所选文本周围产生蓝色高光)。 我尝试使用SuspendLayout()在我的着色期间禁用渲染,但这没有帮助。 提前致谢!

这是我的代码:

码:

 private void editBox_TextChanged(object sender, EventArgs e) { syntaxHighlightFromRegex(); } private void syntaxHighlightFromRegex() { this.editBox.SuspendLayout(); string REG_EX_KEYWORDS = @"\bSELECT\b|\bFROM\b|\bWHERE\b|\bCONTAINS\b|\bIN\b|\bIS\b|\bLIKE\b|\bNONE\b|\bNOT\b|\bNULL\b|\bOR\b"; matchRExpression(this.editBox, REG_EX_KEYWORDS, KeywordsSyntaxHighlightFont, KeywordSyntaxHighlightFontColor); } private void matchRExpression(RichTextBox textBox, string regexpression, Font font, Color color) { System.Text.RegularExpressions.MatchCollection matches = Regex.Matches(this.editBox.Text, regexpression, RegexOptions.IgnoreCase); foreach (Match match in matches) { textBox.Select(match.Index, match.Length); textBox.SelectionColor = color; textBox.SelectionFont = font; } } 

在MyRichTextBox内部(从RichTextBox中删除):

 public void BeginUpdate() { SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero); } public void EndUpdate() { SendMessage(this.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero); } [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); private const int WM_SETREDRAW = 0x0b; 

即使你看起来像汉斯的语法高亮文本框,它看起来并不像你正在使用它。

在突出显示这些单词时,您需要记住光标在进行突出显示之前的位置和长度,因为在您的代码中,您正在移动光标而不是将其放回原位。

如果没有错误检查,请尝试将代码更改为:

 void editBox_TextChanged(object sender, EventArgs e) { this.editBox.BeginUpdate(); int lastIndex = editBox.SelectionStart; int lastLength = editBox.SelectionLength; syntaxHighlightFromRegex(); editBox.Select(lastIndex, lastLength); this.editBox.SelectionColor = Color.Black; this.editBox.EndUpdate(); this.editBox.Invalidate(); } 

糟糕的是,我错误地使用了Hans代码。 我应该调用BeginUpdate()来停止绘制控件,并调用EndUpdate()来再次开始绘制它。 我反过来这样做。

感谢所有人的帮助,大家(尤其是汉斯)!