Take the 2-minute tour ×

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);

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.

share|improve this question
possible duplicate of Calculating size of text before drawing to a canvas –  Jerry Dodge May 2 '13 at 16:39
@JerryDodge Thank you for the tip –  Jeeva May 3 '13 at 3:06
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
I think you totally missed OP's point. He want to decrease width at expense of doubled height. –  OnTheFly May 2 '13 at 16:39
What do you mean, @User? The width of the rectangle is reduced in both cases (from 4770 to 4366). The only different output is the height, which I think it adequately explained by having different numbers of lines of text. Using the calculated height to position the text for drawing will not clip the bottom in that case because there's twice as much height available. In fact, the second line is clipped in that case, but since the second line is blank, it's hard to notice. –  Rob Kennedy May 2 '13 at 16:43
the whole purpose of adding CR is unclear for me, since resulting width is the same –  OnTheFly May 3 '13 at 0:46
I don't think there was a purpose, @User. The extra character on the end was probably accidental, but with it present, Jeeva observed the program behaving the way it was originally intended to. My assumption is that 4366 is enough to represent all the text on a single line. Regardless, I see no reason we would ever expect a terminal carriage return to affect the width of any text. –  Rob Kennedy May 3 '13 at 1:36
add comment

Your Answer


