Reputation: 2661
My program draws text strings into rectangles by working out the width and height of the text, and selecting a smaller font if its too big for the rectangle. But originally i only used single lined text, now i need some multi lined, i used to use GetTextExtentPoint32 but if theres \n in the string, it seems to think of that as a normal character.
DrawText with DT_CALCRECT only returns the height of the text...
Is there a simple way to do this?
Upvotes: 0
Views: 1539
Reputation: 14508
Docs for DrawText state that while it only returns the height, it modifies the rectangle you pass it. Are you checking the rectangle, or only the return value? It sounds like you would actually want to pass in a rectangle with a large width (ie. maximum width you want to allow), and DrawText will reduce as necessary. (If you pass in a small width, it will expand it only enough to fit the largest word.)
From MSDN:
If there are multiple lines of text, DrawText uses the width of the rectangle pointed to by the lpRect parameter and extends the base of the rectangle to bound the last line of text. If the largest word is wider than the rectangle, the width is expanded.
Upvotes: 1
Reputation: 27480
You should do roughly this (pseudo code):
size text_dim(0,0);
foreach( line in text.split("\n") )
{
size line_dim = GetTextExtentPoint32(line.start,line.length);
text_dim.y += line_dim.y;
text_dim.x = max(text_dim.x,line_dim.x);
}
return text_dim;
Upvotes: 1