简体   繁体   English

Graphics.Drawstring WordWrap不符合预期

[英]Graphics.Drawstring WordWrap is not as expected

In my billing application, bills are printed using Graphics.DrawString method, etc. When using Graphics.DrawString with word wrap to draw the ItemName it wrap words into many lines. 在我的计费应用程序中,使用Graphics.DrawString方法等来打印账单。当使用带换行的Graphics.DrawString来绘制ItemName时,它将单词分成多行。 so it is consuming many rows. 因此它消耗了很多行。

I recreated the problem using simple code. 我使用简单的代码重新创建了问题。 With this code I'm expecting output like the following image: Expected Output designed using MS Paint 使用此代码,我期望输出如下图所示: 使用MS Paint设计的期望输出

This is my sample code. 这是我的示例代码。

 string FONTNAME = "Tahoma";
 text = "C# is a simple, modern, general-purpose, object-oriented programming language";
 e.Graphics.DrawString(text, new Font(FONTNAME, 12, FontStyle.Bold), drawBrush, new RectangleF(0, 0, 100.0F, 200F), new StringFormat());
 e.Graphics.DrawRectangle(new System.Drawing.Pen(drawBrush), 0, 0, 100.0F, 200F);

But I'm getting the following output: Output Image 但是我得到以下输出: 输出图像

Is there any option to break the words instead of word wrap ? 有没有选择打破单词而不是自动换行的选择?

No, Graphics.DrawString does not have any option to break on characters (as opposed to whole words). 不, Graphics.DrawString没有任何选项可以中断字符(相对于整个单词)。

To control wrapping, you can pass a StringFormat object initialised with a StringFormatFlags . 为了控制包装,可以传递StringFormat与初始化对象StringFormatFlags The only relevant flag is StringFormatFlags.NoWrap . 唯一相关的标志是StringFormatFlags.NoWrap The .NET documentation isn't very helpful, but if we look at the GDI+ documentation (which is the underlying library), we read: .NET文档不是很有帮助,但是如果我们查看GDI +文档 (这是基础库),则会看到:

StringFormatFlagsNoWrap StringFormatFlagsNoWrap

Specifies that the wrapping of text to the next line is disabled. 指定禁用文本到下一行的换行。 … When drawing text within a rectangle, by default, text is broken at the last word boundary that is inside the rectangle's boundary and wrapped to the next line. …在矩形内绘制文本时,默认情况下,文本在矩形边界内的最后一个单词边界处折断并包装到下一行。

Thus, the only options are wrapping at word boundaries, or performing no wrapping at all. 因此,唯一的选择是在单词边界处换行,或者根本不进行换行。

To get the output you want, you will need to measure character-by-character and draw multiple strings (one for each line). 要获得所需的输出,您将需要逐个字符地测量并绘制多个字符串(每行一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM