问题
是否可以加快 RichtextBox 中线条的着色速度?
情况
我有日志文件,其中有错误的行必须用彩色显示。为此,我使用了性能很差的 RichttextBox
控件。
到目前为止我达到了什么
我已经这样做来加快速度:
- 使用Richedit50W instead of the default Richedit20
- 之前构建一个需要的行号列表(首先从磁盘本地读取日志)
- 避免在循环中使用
RichTextbox50.Lines.Count()
之后,我需要 11 秒来为 2000 行中的 100 行着色,而不是之前的 40 秒。
其他问题
还有其他几个问题,如提到的 Richedit50W关于加速 RichtextBox 或着色。但没有答案有助于加快着色。
我的代码
我还能做些什么来加快速度吗?
private void ColorizeLog(List<int> ErrorLines)
{
// The List "ErrorLines" is filled with the wanted line numbers
// and was build before filling the RichtextBow with the log data
// Counting the lines before the for..next is 50% faster than:
// for (int lineNo = 0; lineNo < this.richTextbox50_Log.Lines.Count(); lineNo++)
// in combination the other usage of "countLines" deeper in the code
int countLines = this.richTextbox50_Log.Lines.Count();
for (int lineNo = 0; lineNo < countLines; lineNo++)
{
if (ErrorLines.IndexOf(lineNo) >= 0)
{
int pos1 = this.richTextbox50_Log.GetFirstCharIndexFromLine(lineNo);
int pos2 = lineNo < countLines - 1 ?
this.richTextbox50_Log.GetFirstCharIndexFromLine(lineNo + 1) - 1 :
this.richTextbox50_Log.Text.Length;
this.richTextbox50_Log.Select(pos1, pos2 - pos1);
this.richTextbox50_Log.SelectionColor = Color.White;
this.richTextbox50_Log.SelectionBackColor = Color.Red;
}
}
最佳答案
For one of my projects, I have felt the need of a text editor with syntax highlighting. At first, I used a component inherited from RichTextBox, but while using it for a large amount of text I found out that RichTextBox highlights very slowly a large number of colored fragments (from 200 and more). When such highlighting has to be made in a dynamic way, it causes a serious problem.
http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting
我认为 RichTextBox 有时只是一个 b***...
关于c# - 可以在 RichtextBox 中加快线条的着色速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37678212/