Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create modified HFONT from HFONT

Tags:

c++

c

winapi

I using the Win32 API and C/C++. I have a HFONT and want to use it to create a new HFONT. The new font should use the exact same font metrics except that it should be bold. Something like:

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

The "GetLogicalFont" is the missing API (as far as I can tell anyway). Is there some other way to do it? Preferrably something that works on Windows Mobile 5+.

like image 550
David Nordvall Avatar asked Dec 16 '08 11:12

David Nordvall


2 Answers

You want to use the GetObject function.

GetObject ( hFont, sizeof(LOGFONT), &lf );
like image 158
arul Avatar answered Oct 15 '22 16:10

arul


Something like this - note that error checking is left as an exercise for the reader. :-)

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);
}
like image 25
Johann Gerell Avatar answered Oct 15 '22 15:10

Johann Gerell