GetThemeFont function not working

0

I'm working on a custom header control CMyHeaderCtrl which is derived from the MFC class CHeaderCtrl and overrides the DrawItem method to do some custom drawing when the application is themed. At first I try to determine the theme font for header items, but it fails and GetThemeFont returns the result 'element not found' (0x80070490).

The application which uses this control is linked against Common Controls 6. Here is some sample code:

void MyHeaderCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    if(IsThemeActive() && IsAppThemed() && ComCtlVersionOK())
    {
        if(HTHEME hTheme = OpenThemeData(m_hWnd, L"HEADER"))
        {
            LOGFONTW lfw;
            HRESULT hr = GetThemeFont(hTheme, lpDrawItemStruct->hDC, HP_HEADERITEM, HIS_NORMAL, TMT_CAPTIONFONT, &lfw);
            ASSERT(hr == S_OK);

            // ...          

            CloseThemeData(hTheme);
        }
    }
}

I also already tried other properties than TMT_CAPTIONFONT like TMT_SMALLCAPTIONFONT, TMT_BODYFONTand so on. What could be wrong here?

c++
mfc
fonts
themes
common-controls
asked on Stack Overflow Sep 3, 2012 by alexfr

1 Answer

1

I've never had any luck getting GetThemeFont() to return anything other than E_PROP_ID_UNSUPPORTED (0x80070490), either. Although it's not explicitly stated in MSDN, the idea seems to be that GetThemeFont() would only return something if the theme defined a font different from the default for the particular part and state specified by the other argument. At least, that's what one MSDN blog suggests: http://blogs.msdn.com/b/cjacks/archive/2006/06/02/614575.aspx

Given that, it seems that the correct approach is to try GetThemeFont(), and if that fails, try GetThemeSysFont(), something like this:

HTHEME theme = OpenThemeData(wnd,L"HEADER");
if (theme != 0)
{
  LOGFONTW lf;
  HRESULT hr = GetThemeFont(theme,dc,
    HP_HEADERITEM,HIS_NORMAL,TMT_CAPTIONFONT,&lf);
  if (FAILED(hr))
    hr = GetThemeSysFont(theme,TMT_CAPTIONFONT,&lf);
  ASSERT(SUCCEEDED(hr));
  // Do something with the font ...
  CloseThemeData(theme);
}
answered on Stack Overflow Sep 4, 2012 by DavidK

User contributions licensed under CC BY-SA 3.0