2019独角兽企业重金招聘Python工程师标准>>>
这个功能要添加两个控件Panel/RichTextBox
//方法private void showLineNo(){//获得当前坐标信息Point p this.richTextBox1.Location;int crntFirstIndex this.richTextBox1.GetCharIndexFromPosition(p);int crntFirstLine this.richTextBox1.GetLineFromCharIndex(crntFirstIndex);Point crntFirstPos this.richTextBox1.GetPositionFromCharIndex(crntFirstIndex);//p.Y this.richTextBox1.Height;//int crntLastIndex this.richTextBox1.GetCharIndexFromPosition(p);int crntLastLine this.richTextBox1.GetLineFromCharIndex(crntLastIndex);Point crntLastPos this.richTextBox1.GetPositionFromCharIndex(crntLastIndex);//////准备画图Graphics g this.panel1.CreateGraphics();Font font new Font(this.richTextBox1.Font,this.richTextBox1.Font.Style);SolidBrush brush new SolidBrush(Color.Green);//////画图开始//刷新画布Rectangle rect this.panel1.ClientRectangle;brush.Color this.panel1.BackColor;g.FillRectangle(brush, 0, 0, this.panel1.ClientRectangle.Width,this.panel1.ClientRectangle.Height);brush.Color Color.Green;//重置画笔颜色////绘制行号int lineSpace 0;if (crntFirstLine ! crntLastLine){lineSpace (crntLastPos.Y - crntFirstPos.Y) / (crntLastLine - crntFirstLine);}else{lineSpace Convert.ToInt32(this.richTextBox1.Font.Size);}int brushX this.panel1.ClientRectangle.Width - Convert.ToInt32(font.Size * 3);int brushY crntLastPos.Y Convert.ToInt32(font.Size*0.21f);//惊人的算法啊for (int i crntLastLine; i > crntFirstLine;i-- ){g.DrawString((i 1).ToString(), font, brush, brushX, brushY);brushY - lineSpace;}g.Dispose();font.Dispose();brush.Dispose();}
以上方法为绘制行号的方法将其放在panel的paint方法中
转:https://my.oschina.net/huige2012/blog/68110