DrawThemeText doesn’t really support the DT_CALCRECT format value

I just noticed this while working on a bug fix.

We have some code that attempts to determine the size of a piece of text by calling DrawThemeText with the DT_CALCRECT format value.

According to this page (as of 9/30/2008) the DT_CALCRECT option determines the width and height of the display rectangle.

Unfortunately the DT_CALCRECT option doesn’t actually return the width and height of the display rectangle.  Instead it doesn’t modify the input LPRECT field.  I asked the developer and he indicated that the reason was that the pRect parameter to DrawThemeText is actually defined as a LPCRECT and thus the implicit contract for the API is that it doesn’t change the contents of the pRect field.

 

There are two ways of working around this problem.  The first (and recommended) way is to call GetThemeTextExtent to retrieve the dimensions of the rectangle.  The second (which works on Vista and later operating systems) is to call DrawThemeTextEx passing in the DTT_CALCRECT flag – in that case the DrawThemeTextEx API will return the size of the text in the pRect parameter.

 

I’ve sent emails to the SDK folks so it’s entirely likely that the page will be updated in the future but until it is, hopefully this will help someone using the search engines to find what’s going on.