从HFONT创建修改过的HFONT

我使用Win32 API和C / C ++。 我有一个HFONT,想用它来创建一个新的HFONT。 新字体应使用完全相同的字体指标,但它应该是粗体。 就像是:

HFONT CreateBoldFont(HFONT hFont) { LOGFONT lf; GetLogicalFont(hFont, &lf); lf.lfWeight = FW_BOLD; return CreateFontIndirect(&lf); } 

“GetLogicalFont”是缺少的API(据我所知,无论如何)。 还有其他方法吗? 最好是适用于Windows Mobile 5+的东西。

您想使用GetObject函数 。

 GetObject ( hFont, sizeof(LOGFONT), &lf ); 

这样的事情 – 注意错误检查留给读者练习。 🙂

 static HFONT CreateBoldWindowFont(HWND window) { const HFONT font = (HFONT)::SendMessage(window, WM_GETFONT, 0, 0); LOGFONT fontAttributes = { 0 }; ::GetObject(font, sizeof(fontAttributes), &fontAttributes); fontAttributes.lfWeight = FW_BOLD; return ::CreateFontIndirect(&fontAttributes); } static void PlayWithBoldFont() { const HFONT boldFont = CreateBoldWindowFont(someWindow); . . // Play with it! . ::DeleteObject(boldFont); }