我正在尝试突出显示游标所在的整行,就像在Dev-C ++和其他IDE中一样。不突出显示文本,而是在控件的整行上绘制透明色。这就是我想要的东西
答案 0 :(得分:3)
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的代码