0

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Jeeva
  • 4,585
  • 2
  • 32
  • 56
  • possible duplicate of [Calculating size of text before drawing to a canvas](http://stackoverflow.com/questions/7719025/calculating-size-of-text-before-drawing-to-a-canvas) – Jerry Dodge May 02 '13 at 16:39

1 Answers1

3

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • I think you totally missed OP's point. He want to decrease width at expense of doubled height. – OnTheFly May 02 '13 at 16:39
  • 1
    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 02 '13 at 16:43
  • the whole purpose of adding CR is unclear for me, since resulting width is the same – OnTheFly May 03 '13 at 00:46
  • 1
    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 03 '13 at 01:36