如何正确使用DrawText DT_CALCRECT? [英] How do I use DrawText DT_CALCRECT properly?

查看:597
本文介绍了如何正确使用DrawText DT_CALCRECT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一个报表,该报表的页脚带有标题.我使用DrawText找出标题的尺寸.问题是文本总是被剪切,但是当我在文本末尾有回车符时,所有文本都会完美显示.

I am generating a report that has a caption in the footer. I use DrawText to find out the caption's dimensions. The problem is the text is always clipped, but when I have a carriage return at the end of the text, all the text appears perfectly.

lClientRect := Rect(0, 0, 4770, 59);
lFlags := DT_CALCRECT or DT_EXPANDTABS or Alignments[Alignment]
       or WordWraps[WordWrap] or DT_NOPREFIX or DT_TOP or DT_EXTERNALLEADING;

DrawText(lCanvas.Handle, PChar(lsCaption), Length(lsCaption), lClientRect, lFlags);

在调用DrawText之后,我检查了矩形,它是(0,0,4366,59),但是当我有回车符时,它是(0,0,4366,118).

I examined the rect after the call to DrawText, and it is (0, 0, 4366, 59), but when I have a carriage return, it is (0, 0, 4366, 118).

我对正在发生的事情一无所知.任何帮助将不胜感激.

I don't have any clue on what is happening. Any help will be appreciated.

推荐答案

回车符将第二行文本添加到字符串中,从而使计算出的矩形的高度加倍. (Windows对于换行符或回车符开始新行是很灵活的.)

The carriage return adds a second line of text to the string, thus doubling the height of the calculated rectangle. (Windows is flexible about whether a line-feed or carriage-return character starts a new line.)

关于为什么剪切文本(我认为是在底部边缘),可能是因为您使用的字体与绘制文本时使用的字体不同.

As for why the text is clipped (on the bottom edge, I assume), it might be that you're calculating the size using a different font than you have when you draw the text.

这篇关于如何正确使用DrawText DT_CALCRECT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆