我正在使用此代码.它用于语法高亮显示RichTextBox.我专门找在函数ProcessLine()和OnTextChanged(),我已经修改为这样:
protected override void OnTextChanged(EventArgs e)
{
// Calculate stuff here.
m_nContentLength = this.TextLength;
int nCurrentSelectionStart = SelectionStart;
int nCurrentSelectionLength = SelectionLength;
m_bPaint = false;
// Find the start of the current line.
m_nLineStart = nCurrentSelectionStart;
while ((m_nLineStart > 0) && (Text[m_nLineStart - 1] != '\n'))
m_nLineStart--;
// Find the end of the current line.
m_nLineEnd = nCurrentSelectionStart;
while ((m_nLineEnd < Text.Length) && (Text[m_nLineEnd] != '\n'))
m_nLineEnd++;
// Calculate the length of the line.
m_nLineLength = m_nLineEnd - m_nLineStart;
// Get the current line.
m_strLine = Text.Substring(m_nLineStart, m_nLineLength);
// Process this line.
ProcessLine();
m_bPaint = true;
}
// Process a line.
private void ProcessLine()
{
// Save the position and make the whole line black
int nPosition = SelectionStart;
SelectionStart = m_nLineStart;
SelectionLength = m_nLineLength;
SelectionColor = Color.Black;
/*// Process the keywords
ProcessRegex(m_strKeywords, Settings.KeywordColor);
// Process numbers
if(Settings.EnableIntegers)
ProcessRegex("\\b(?:[0-9]*\\.)?[0-9]+\\b", Settings.IntegerColor);
// Process strings
if(Settings.EnableStrings)
ProcessRegex("\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"", Settings.StringColor);
// Process comments
if(Settings.EnableComments && !string.IsNullOrEmpty(Settings.Comment))
ProcessRegex(Settings.Comment + ".*$", Settings.CommentColor);*/
SelectionStart = nPosition;
SelectionLength = 0;
SelectionColor = Color.Red;
m_nCurSelection = nPosition;
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是,当我进入ProcessLine()in时OnTextChanged(),我会在结尾处总是有换行符m_strLine吗?最小的值m_strLine是"\n"还是最大的"任何#chachars + \n"?
就这样我有这个权利,SelectionStart如果SelectionLength是零,我的插入位置是否为零,如果SelectionLength大于零,我的插入符号是SelectStart+SelectionLength?
我正在尝试修改此代码以为许多不同的语法表达式着色,并且我计划为每一行一次查看一个字符.粘贴或加载20k +行的文件时,这怎么可能?
小智 6
我现在可以建议你使用稳定,功能更强大,更不易出错的东西,如Scintilla for .NET和Color Code.这些控件是免费和开源的.尝试一下:
ScintillaNET
ColorCode - .NET的语法突出显示/着色
RichTextBox对于处理大型文本效率极低.即使你得到了一些不错的突出显示,性能问题也会很快开始出现.