Get max line width in a RichTextBox

十年热恋 提交于 2020-03-04 23:09:31

问题


I'm trying to calculate the max horizontal scroll position of a RichTextBox, I'm aware that I could do something like:

Graphics g = richTextBox1.CreateGraphics();
float maxLineWidth = 0;
foreach(string line in richTextBox1.Lines) {
    float thisLineWidth = g.MeasureString(line, richTextBox1.Font).Width;
    if(thisLineWidth > maxLineWidth) {
        maxLineWidth = thisLineWidth;
    }
}

But is there a more efficient way?

There's a win api function GetScrollRange which is used like:

int min = 0;
int max = 0;
GetScrollRange(richTextBox1.Handle, SB_HORZ, out min, out max);

But this requires the RichTextBox to have scrollbars enabled, otherwise min/max will always be 0. I do not want my RichTextBox to have the default scrollbars enabled.

Is there anything similar I could use which does not require scrollbars to be enabled?

来源:https://stackoverflow.com/questions/59992514/get-max-line-width-in-a-richtextbox

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