DrawText with DT_CALCRECT - Is there a way to calculate the height of a rectangle WITHOUT changing the width (with large lines)?

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

An example of my line:

 thisisaverylonglonglonglineoftextthatneedstofitinsideagivenrectwidth 

I see in MSDN DrawTextW() that DrawTextW() indicates:

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

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

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

So, how can I correctly calculate the height of a text rectangle with a given (fixed) width when drawing a large line (without spaces), where DT_WORDBREAK and DT_CALCRECT are indicated?

EDIT:

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

+6
source share
1 answer

You need to use the DT_WORD_ELLIPSIS flag in the uFormat parameter (along with DT_WORDBREAK , of course). This will prevent expansion due to long lines without spaces. However, it still will not break these long lines, but the problem with the width will be solved.

If you also specify DT_MODIFYSTRING , you can figure out where to split this long line before the final draw.

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

+7
source

Source: https://habr.com/ru/post/899934/


All Articles