A frequent operation in my Windows Table control, which I am reworking and moving into a DLL, is to get the height of a row. This is the maximum of

  • the height of text in the current font, in pixels
  • the current small icon height, in pixels (GetSystemMetrics(SM_CYSMICON))
  • the height of checkboxes, in pixels (determined on a WM_THEMECHANGED, when checkbox information is recalculated)

Calculating the text height, as far as I know, requires getting a DC, selecting the font in (and getting the SYSTEM_FONT if that's NULL), getting the text metrics, selecting the font out, and releasing the DC, all of which can error out/fail/etc. This means that virtually every function in my control can fail.

I can avoid this by storing the text height somewhere else, only calculating it when it changes. I know that text height is a property related to the DPI of the DC that GetDC(hwnd) returns. I would like my control to be DPI-agnostic because DPI awareness is per-process, not per-DLL/per-window.

At the same time, knowing when GetSystemMetrics(SM_CYSMICON) changes would also be useful.

So my questions are simple:

  • Is there a message that I can look for that will tell me that my DPI has changed and that I need to recalculate my text height?
  • Is there a message that will tell me that SM_CYSMICON has changed and that I need to recalculate everything? Is it the same one? (I know there is no reliable way to detect a GetSystemMetrics() failure (since 0 is a valid return and it does not set the last error code), so I am assuming it cannot fail with a valid parameter and am simply calling it each time I need to calculate the row height; this is just so I can queue a redraw when the value does change.) Would it also work for SM_CXSMICON?
  • In addition, looking back at my code, GetThemePartSize() takes a DC as well; do theme items like checkbox images scale with DPI? And if so, what messages do I look for in that case? The same one?
  • Alternative: is there a non-failing way to get the text height that I don't know about, given only a HWND and HFONT?

I will be happy to take a solution that was introduced in either Windows XP or Windows Vista; if there's a solution that was introduced in a newer version of Windows, then knowing about it could also be beneficial.

Thanks.

0

There are 0 best solutions below