简体   繁体   中英

Draw text and make it fit inside a rectangle in GDI?

Is there's a function that can draw text and fit it inside a rectangle (the function will make the size of the text smaller as appropriate to make it fit or something).

I checked the parameters for DrawText() but I don't think it supports such a feature.

Maybe you can use GetTextMetrics to check if the text fits inside the rectangle, and if it doesn't, reduce the current font size and repeat the measurement.

GetTextMetrics:

http://msdn.microsoft.com/en-us/library/windows/desktop/dd144941%28v=vs.85%29.aspx

There is no function doing it right away, but your can write your own using DrawText with DT_CALCRECT parameter. This parameter will only calculate size of your text and return it to you. It allows also multi line text and also checks provided maximal width you can allow text to be. So if the output rectangle - as calculated by DrawText is too large, then you must calculate it again but with smaller font, you can speed up calculations by using binary method to find the most appropriate font size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM