问题
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