C# 拉绳自动换行或显示整个文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9507842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:40:47  来源:igfitidea点击:

Drawstring word wrap or display entire text

c#winformsc#-3.0

提问by Sharpeye500

This is the output i get when i use DrawString.

这是我使用 DrawString 时得到的输出。

I=Smith,John II=Johnson,Mark III=Anderson,James IV=William,Craig V=Ford,He...

I=史密斯,约翰二世=约翰逊,马克三世=安德森,詹姆斯四世=威廉,克雷格五世=福特,他...

page is a float datatype which value is based on e.PageSettings.Margins.Left;

page 是一个浮点数据类型,其值基于 e.PageSettings.Margins.Left;

e.Graphics.DrawString(Text, new System.Drawing.Font("Arial", 8F, FontStyle.Regular), Brushes.Black, page, 30);

e.Graphics.DrawString(Text, new System.Drawing.Font("Arial", 8F, FontStyle.Regular), Brushes.Black, page, 30);

In the above example, it is

在上面的例子中,它是

e.Graphics.DrawString(Text, new System.Drawing.Font("Arial", 8F, FontStyle.Regular), Brushes.Black, page, 30);

I tried using this

我试过用这个

StringFormat format = new StringFormat();            
format.FormatFlags = StringFormatFlags.FitBlackBox;

 e.Graphics.DrawString(Text, new System.Drawing.Font("Arial", 8F, FontStyle.Regular), Brushes.Black, page, 30, format);

How do i expand/word wrap so that i can have the entire words instead of '...' at the end?

我如何展开/自动换行,以便我可以在最后使用整个单词而不是“...”?

I=Smith,John II=Johnson,Mark III=Anderson,James IV=William,Craig V=Ford,Henry

I=史密斯,约翰二世=约翰逊,马克三世=安德森,詹姆斯四世=威廉,克雷格五世=福特,亨利

采纳答案by Sorean

You can "word wrap" the text by using a bounding rectangle.

您可以使用边界矩形对文本进行“自动换行”。

Use Graphics.DrawString Method (String, Font, Brush, RectangleF, StringFormat)

使用Graphics.DrawString 方法(字符串、字体、画笔、RectangleF、StringFormat)

The RectangleF specifies the draw area and it will automatically "wrap" your text for you.

RectangleF 指定绘图区域,它会自动为您“包裹”您的文本。