Highlight the entire line in a RichTextBox [duplicate]

跟風遠走 提交于 2019-12-12 04:11:47

问题


I want to highlight the entire line, from start to end doesn't matter whether characters are present or not and line may be blank but it should highlight the complete line.

Like


回答1:


This will highlight a full line in a RichTextBox if WordWrap is off:

void highLightALine(RichTextBox rtb, int line, Color hiLight)
{
    int i1 = rtb.GetFirstCharIndexFromLine(line);
    int i2 = rtb.GetFirstCharIndexFromLine(line + 1);
    if (i2 < 0) i2 = rtb.Text.Length;

    rtb.SelectionStart = i1;
    rtb.SelectionLength = i2 - i1;
    rtb.SelectionBackColor = hiLight;
}

Note that if WordWrap is true it will still highlight the line but only as far as it is visible. Its continuation on the next line will not be changed.

Also note that only Text can be highlighted. Empty space can't be highlighted afaik. Here is an example of trying to owner-draw an RTB subclass..



来源:https://stackoverflow.com/questions/37205546/highlight-the-entire-line-in-a-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!