1

我正在开发一个CMyHeaderCtrl从 MFC 类派生的自定义标题控件,CHeaderCtrl并在应用程序主题化时覆盖该DrawItem方法以进行一些自定义绘图。起初我尝试确定标题项目的主题字体,但它失败并GetThemeFont返回结果'element not found' (0x80070490)

使用此控件的应用程序与 Common Controls 6 相关联。下面是一些示例代码:

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);
        }
    }
}

TMT_CAPTIONFONT我也已经尝试过除了like之外的其他属性TMT_SMALLCAPTIONFONTTMT_BODYFONT等等。这里有什么问题?

4

1 回答 1

2

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);
}
于 2012-09-04T09:43:16.220 回答