使 DrawText 断开字符串

发布于 2024-11-04 09:34:39 字数 155 浏览 3 评论 0原文

因此,如果字符串有空格,或者如果我在字符串末尾放入 \r\n ,我可以使 drawtext 中断字符串。但是,其中没有空格或换行符的长字符串会继续经过绘图矩形并被剪切。如果可能的话,我想阻止这种情况发生。

在我看来,绘图文本的格式标志中没有任何东西能让这种情况发生。有什么想法吗?

So i can make drawtext break a string if the string has spaces, or if i put in \r\n at the end of the string. However, a long string that has no spaces or line breaks in it continues past the drawing rectangle and gets clipped. I would like to prevent this, if possible.

Nothing in the format flags for drawtext stood out to me that would make this happen. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-11-11 09:34:39

实际上,这就是您在大多数情况下想要的,来自 MSDN

DT_EDITCONTROL

复制多行编辑的文本显示特征
控制。具体来说,平均字符宽度的计算方式为
与编辑控件的方式相同,并且该函数不
显示部分可见的最后一行。

它没有提到的是,如果需要的话,它也会在中间中断单词,就像编辑控件一样。它确实具有不显示部分可见底线的其他效果,但在大多数用法中,我认为这并不重要,因为您可能会提前使用 DT_CALCRECT 以确保有足够的空间(如果没有,则不会绘制部分可见的底线)无论如何,可见线可能是最好的)。如果它真的很重要,您总是可以通过裁剪做一些事情,例如给 DrawText 一个更高的矩形,但已经在设备上下文上设置了一个较小的裁剪矩形。

Actually, this is what you want in most cases, from MSDN:

DT_EDITCONTROL

Duplicates the text-displaying characteristics of a multiline edit
control. Specifically, the average character width is calculated in
the same manner as for an edit control, and the function does not
display a partially visible last line.

What it fails to mention is that it also breaks words in the middle if needed, like an edit control does. It does have the other effect of not showing partially visible bottom lines, but in most usages I do not think that will matter because you are probably using DT_CALCRECT ahead of time to make sure you have enough space (and if not, not drawing a partially visible line is probably best anyway). If it really mattered, you could always do something with clipping, such as give DrawText a taller rectangle, but have a smaller clip rectangle set on the device context already.

初相遇 2024-11-11 09:34:39

通常要做的是使用DT_WORDBREAK标志。来自 MSDN 上的 DrawText 文档,这:

断言。线路是自动的
如果一个单词会在单词之间断开
延伸超过矩形的边缘
由 lpRect 参数指定。一个
回车换行顺序
也打破了界限。

如果未指定,则输出打开
一行。

然而,你有一条牢不可破的线 - 也就是说,没有任何文字。 您需要自己处理中断。您可以通过两种方式解决此问题:

  • 使用 DrawTextDT_CALCRECT 来计算矩形 - 循环,缩短字符串,直到找到第一行的字符串,然后对剩余的字符串重复。也就是说,从索引 0-n 中找到水平适合绘图区域宽度的子集;从n+1-m中找到再次水平拟合的字符串;重复,直到击中弦的末端。在每个子字符串之间使用 \r\n 连接这些子字符串以强制手动中断,然后使用 DrawText 进行绘制,它将正确换行。

  • 使用DrawTextEx 并传入一个指向 DRAWTEXTPARAMS 结构。它有一个 uiLengthDrawn 成员,它是“由 DrawTextEx 处理的字符数,包括空白字符。该数字可以是字符串的长度或位于绘图下方的第一行的索引区域。”您不应该传入DT_NOCLIP。这可能(无需测试)将为您提供与第一种方法相同的信息,而无需循环:让我不确定的关键位是绘图区域“下方”的短语,如果您的线条牢不可破,它可能会认为它处理了整个文本。

第一种方法始终有效,并且是我用于自己的富文本处理的简单版本。

What you would normally do is use the DT_WORDBREAK flag. From the DrawText documentation on MSDN, this:

Breaks words. Lines are automatically
broken between words if a word would
extend past the edge of the rectangle
specified by the lpRect parameter. A
carriage return-line feed sequence
also breaks the line.

If this is not specified, output is on
one line.

However, you have a single unbreakable line - that is, there are no words. You will need to look after the breaks yourself. You can solve this two ways:

  • Use DrawText with DT_CALCRECT to calculate the size of the rectangle - loop, shortening your string, until you've found the string for the first line, then repeat for the remaining string. That is, find the subset from index 0-n which fits horizontally in the width of your drawing area; find the string from n+1-m which fits horizontally again; repeat until you hit the end of your string. Concatenate these substrings with \r\n between each to force a manual break, then draw with DrawText and it will wrap correctly.

  • Use DrawTextEx and pass in a pointer to a DRAWTEXTPARAMS structure. It has a uiLengthDrawn member which is "the number of characters processed by DrawTextEx, including white-space characters. The number can be the length of the string or the index of the first line that falls below the drawing area." You should not pass in DT_NOCLIP. This probably (without testing it) will work to give you much the same information as the first method without the looping: the key bit that makes me uncertain is the phrase "below" the drawing area, and if your line is unbreakable it may think it processed the entire text.

The first method will always work and is a simple version of what I use for my own rich text processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文