从HFONT创建修改后的HFONT

18

我正在使用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+上也能使用的。

2个回答

29
你想要使用GetObject函数
GetObject ( hFont, sizeof(LOGFONT), &lf );

12

大致就是这样,需要注意的是错误检查留给读者作为练习。 :-)

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

1
我来寻找铜,却发现了黄金! - Max Vlasov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接