在绘制到画布之前计算文本大小
我正在使用 Delphi 7。我非常熟悉使用画布和在画布上绘制文本,以及使用 TCanvas.TextHeight 等。当我想实现自动换行时,问题就出现了。我不仅需要将文本绘制到画布上并自动换行到给定宽度约束的最佳方法,而且还需要知道换行后文本的高度(或多少行)。在绘制文本之前,我需要准备另一张图像,该图像需要足够大才能放置换行的文本。这是一次尝试复制 iPhone 显示 SMS 消息的方式,在屏幕两侧的可变高度滚动框中有一个气球(TScrollingWinControl 是我的基础)。
I'm using Delphi 7. I'm more than familiar with using a canvas and drawing text to a canvas, and also using TCanvas.TextHeight etc. The problem arises when I want to implement Word Wrap. Not only do I need the best way to draw text to a canvas and have it automatically wrap to a given width constraint, but I also need to know how high (or how many lines) it will be after it's wrapped. I need to prepare another image before I draw the text, an image which needs to be just big enough to place the wrapped text. This is an attempt to replicate how an iPhone displays SMS messages, with a baloon on either side of the screen in a variable height scrolling box (TScrollingWinControl is my base).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用(几乎)万能的
DrawText
函数使用初始矩形和标志DT_WORDBREAK
(意味着字符串应该自动换行)和DT_CALCRECT
:由于
DT_CALCRECT
标志,第一个DrawText
不会绘制任何东西,而只会改变r
的高度> 以便它可以包含整个字符串S
(或者如果S
恰好适合单行,则减小r
的宽度;另外,如果S
包含一个不包含的单词适合单行,r
的宽度将增加)。然后你就可以用r
做任何你想做的事情,然后你就可以真正绘制字符串了。尝试一下,例如:
Use the (almost) omnipotent
DrawText
function using an initial rectangle, and the flagsDT_WORDBREAK
(meaning that the string should be word-wrapped) andDT_CALCRECT
:Due to the flag
DT_CALCRECT
, the firstDrawText
will not draw anything, but only alter the height ofr
so that it can contain the entire stringS
(or reduce the width ofr
ifS
happens to fit on a single line; in addition, ifS
contains a word that does not fit on a single line, the width ofr
will be increased). Then you can do whatever you wish withr
, and then you can draw the string for real.Try this, for example: