Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DrawText with DT_CALCRECT - Is there a way to calculate the height of the rect WITHOUT modifying the width (with large strings)?

I have a string that i need to calculate the Rect size (text height) when drawing. My implementation uses the DrawTextW() function with DT_WORDBREAK or DT_CALCRECT flags.

An example of my string:

thisisaverylonglonglonglineoftextthatneedstofitinsideagivenrectwidth

I can see in the MSDN docs, that DrawTextW() method states:

If the largest word is wider than the rectangle, the width is expanded. If the text is less than the width of the rectangle, the width is reduced. If there is only one line of text, DrawText modifies the right side of the rectangle so that it bounds the last character in the line.

however in the MSDN docs, then DrawTextExW() method does not state this.

So i tried to calculate the height using the DrawTextExW() method, however the result is the same as with DrawTextW() function, where it extends the width of the rect to fit the largest line of text.

So how can i correctly calculate the height of the text rect with a given (fixed) width when drawing a large string (with no spaces) where DT_WORDBREAK and DT_CALCRECT are specified?

EDIT:

As a side note, does anyone know how Microsoft Excel does cell text drawing? Is there an API call for this text drawing? This was where my original question stemmed from, however the way it is implemented in Excel is to draw the text and wordbreak/wordwrap on any character (not just a space).

like image 533
Simon Avatar asked Oct 24 '11 02:10

Simon


1 Answers

You need to use the DT_WORD_ELLIPSIS flag in uFormat parameter (along with DT_WORDBREAK of course). That will prevent the widening due to long strings with no spaces. It still will not break those long strings though, but your width problem will be solved.

If you also specify DT_MODIFYSTRING, then you could kind of figure out where to break that long string yourself, prior to the final draw.

As for the difference between DrawText(W) and DrawTextEx(W): the latter provides tab formatting, setting margins and returns the actual number of drawn characters. There is no difference in (dimensioning) functionality.

like image 126
NGLN Avatar answered Sep 22 '22 08:09

NGLN