Larry Osterman's WebLog

Confessions of an Old Fogey
Blog - Title

DrawThemeText doesn’t really support the DT_CALCRECT format value

DrawThemeText doesn’t really support the DT_CALCRECT format value

  • Comments 1

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.

  • Well, it's not the first time const-correctness hasn't been ... correct in Win32 API, is it?

    My latest experience was with WriteProcessMemory. The presented example is however the first time I've seen 'const' actually used... where it (in retrospect) shouldn't have been. :-)

    (a little O/T: as you now have all these security reviews and stuff - has anyone even bothered to add a "const correctness" checkbox for API's, public or not?)

Page 1 of 1 (1 items)