0
我想突出顯示光標所在的整行,就像在Dev-C++和其他IDE中一樣。不突出顯示文字,而是在整個控制線上繪製透明的顏色。這就是我想要的C# - 突出顯示RichTextBox中的當前行
我想突出顯示光標所在的整行,就像在Dev-C++和其他IDE中一樣。不突出顯示文字,而是在整個控制線上繪製透明的顏色。這就是我想要的C# - 突出顯示RichTextBox中的當前行
int lastLine = 0;
private void HighlightCurrentLine()
{
// Save current selection
int selectionStart = rtb.SelectionStart;
int selectionLength = rtb.SelectionLength;
// Get character positions for the current line
int firstCharPosition = rtb.GetFirstCharIndexOfCurrentLine();
int lineNumber = rtb.GetLineFromCharIndex(firstCharPosition);
int lastCharPosition = rtb.GetFirstCharIndexFromLine(lineNumber + 1);
if (lastCharPosition == -1)
lastCharPosition = rtb.TextLength;
// Clear any previous color
if (lineNumber != lastLine)
{
int previousFirstCharPosition = rtb.GetFirstCharIndexFromLine(lastLine);
int previousLastCharPosition = rtb.GetFirstCharIndexFromLine(lastLine + 1);
if (previousLastCharPosition == -1)
previousLastCharPosition = rtb.TextLength;
rtb.SelectionStart = previousFirstCharPosition;
rtb.SelectionLength = previousLastCharPosition - previousFirstCharPosition;
rtb.SelectionBackColor = SystemColors.Window;
lastLine = lineNumber;
}
// Set new color
rtb.SelectionStart = firstCharPosition;
rtb.SelectionLength = lastCharPosition - firstCharPosition;
if (rtb.SelectionLength > 0)
rtb.SelectionBackColor = Color.PaleTurquoise;
// Reset selection
rtb.SelectionStart = selectionStart;
rtb.SelectionLength = selectionLength;
}
代碼從MSDN
這不正是我說我不想......我不想文本選擇做的,但我想整條生產線要突出顯示,而不是選中.. – tr0yspradling