使 DrawText 断开字符串
因此,如果字符串有空格,或者如果我在字符串末尾放入 \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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,这就是您在大多数情况下想要的,来自 MSDN:
它没有提到的是,如果需要的话,它也会在中间中断单词,就像编辑控件一样。它确实具有不显示部分可见底线的其他效果,但在大多数用法中,我认为这并不重要,因为您可能会提前使用 DT_CALCRECT 以确保有足够的空间(如果没有,则不会绘制部分可见的底线)无论如何,可见线可能是最好的)。如果它真的很重要,您总是可以通过裁剪做一些事情,例如给 DrawText 一个更高的矩形,但已经在设备上下文上设置了一个较小的裁剪矩形。
Actually, this is what you want in most cases, from MSDN:
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.
您通常要做的是使用
DT_WORDBREAK
标志。来自 MSDN 上的 DrawText 文档,这:然而,你有一条牢不可破的线 - 也就是说,没有任何文字。 您需要自己处理中断。您可以通过两种方式解决此问题:
使用
DrawText
和DT_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: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
withDT_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 withDrawText
and it will wrap correctly.Use
DrawTextEx
and pass in a pointer to aDRAWTEXTPARAMS
structure. It has auiLengthDrawn
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 inDT_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.