说明:此显示行号为实际行号,不论是空行还是自动换行,都计算在内,跟实际IDE的行号不同,同步滚动会有半行高度以内的误差。
实现原理,在RichTextBox 编辑器左侧放置另一RichTextBox (或其它控件也可),行号为编辑器实际文字行数,滚动时计算文字滚动高度,再根据行高算出当前行大约位置,左侧自动滚动到当前行。
如果想准确的话,可以不用行,直接拿到文字滚动高度,右侧行号也滚动到相应高度即可。
//行号 生成显示 这里rtbLineNum用的 RichTextBox,也可以用其它private void ShowLineNum(){rtbLineNum.Text = "";//计算行高,行数int linesLength = 0;var pFirst = tbEditor.GetPositionFromCharIndex(0);var pEnd = tbEditor.GetPositionFromCharIndex(tbEditor.Text.Length);if (pEnd.Y > pFirst.Y){var pSecondLine = tbEditor.GetPositionFromCharIndex(tbEditor.GetFirstCharIndexFromLine(1));var lineHeight = pSecondLine.Y - pFirst.Y;linesLength = (pEnd.Y - pFirst.Y) / lineHeight;}else{linesLength = 1;}//生成行数for (var i = 0; i < linesLength; i++){rtbLineNum.AppendText(i + 1 + "\n");}//行号右对齐rtbLineNum.SelectAll();rtbLineNum.SelectionAlignment = HorizontalAlignment.Right;}//上次滚动位置 行private int _scrollToLine = 0;//同步滚动private void SyncSrollLocation(){//首行首字符初始位置var p = new Point(1,1); //计算行高int lineHeight = 0;var pFirst = tbEditor.GetPositionFromCharIndex(0);//首行位置var pEnd = tbEditor.GetPositionFromCharIndex(tbEditor.Text.Length);//最后一行位置if (pEnd.Y > pFirst.Y)//排除只有一行的情况{var pSecondLine = tbEditor.GetPositionFromCharIndex(tbEditor.GetFirstCharIndexFromLine(1));lineHeight = pSecondLine.Y - pFirst.Y;}//滚动高度 即首行位置移动高度 int scrollHeight = p.Y - pFirst.Y;//滚动到的行的位置 由于滚动大都并非整行滚动 所以四舍五入 ***程序的误差就在这里var scrollTolineIndex = (int)Math.Round((double)(scrollHeight / lineHeight), MidpointRounding.AwayFromZero); if (_scrollToLine != scrollTolineIndex){_scrollToLine = scrollTolineIndex;var sStart = rtbLineNum.GetFirstCharIndexFromLine(scrollTolineIndex);//左侧行号 当前滚动到行 首字符位置if (sStart >= 0){rtbLineNum.SelectionStart = sStart;rtbLineNum.SelectionLength = 0;rtbLineNum.ScrollToCaret();}}}//编辑器 Resize事件private void tbEditor_Resize(object sender, EventArgs e){ShowLineNum();SyncSrollLocation();}//编辑器 TextChanged事件private void tbEditor_TextChanged(object sender, EventArgs e){ShowLineNum();SyncSrollLocation();}//编辑器 VScroll事件private void tbEditor_VScroll(object sender, EventArgs e){SyncSrollLocation();}