C#:在不选择文本的情况下更改 WinForm RichTextBox 的字体样式 [英] C#: Changing font style of WinForm RichTextBox without selecting the text

查看:63
本文介绍了C#:在不选择文本的情况下更改 WinForm RichTextBox 的字体样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a RichTextBox in my code where I show syntax-highlighted code. Now, on every keystroke, I have to re-parse all the tokens and re-color them all over again. But the only way to color individual words in a WinForm richtextbox is to select those words one by one and color them using SelectionFont.

但如果用户打字速度非常快,我选择单个单词会引起非常明显的闪烁(所选单词具有 Windows 蓝色背景的东西,这会导致闪烁).有什么方法可以解决这个问题,我可以在不选择它们的情况下为单个单词着色(从而导致所选文本周围出现蓝色突出显示).我尝试使用 SuspendLayout() 在着色期间禁用渲染,但这没有帮助.提前致谢!

But if the user is typing really fast, there is a very noticeable flickering caused by my selecting individual words (selected words have that Windows blue-background thing and that is causing the flickering). Is there any way around that where I can color individual words without selecting them (and hence causing that blue highlight around the selected text). I tried using SuspendLayout() to disable rendering during my coloring but that didn't help. Thanks in advance!

这是我的代码:

代码:

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

Inside the MyRichTextBox (dervied from 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;

推荐答案

糟糕,我错误地使用了 Hans 代码.我应该调用 BeginUpdate() 停止绘制控制,调用 EndUpdate() 重新开始绘制.我是反过来做的.

Oops turns out I was using Hans code incorrectly. I was supposed to call BeginUpdate() to stop drawing control and EndUpdate() to start drawing it again. I was doing it the other way around.

感谢大家的帮助(尤其是 Hans)!

Thanks for all the help, everyone (especially Hans)!

这篇关于C#:在不选择文本的情况下更改 WinForm RichTextBox 的字体样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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