65.9K
CodeProject is changing. Read more.
Home

Automatic Font Handling Class

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.95/5 (8 votes)

Nov 20, 1999

CPOL

1 min read

viewsIcon

149872

downloadIcon

2603

A class to make working with LOGFONTs easier

Sample Image

The CAutoFont class was designed to eliminate the constant, tedious task of filling a LOGFONT structure everytime you need to create or use a font. It was designed to make font manipulation a simple task. Here's a brief example:

void CMyView::OnPaint()
{
    CPaintDC dc(this);
    CAutoFont autofont("Courier New");

    autofont.SetBold(TRUE);
    autofont.SetItalic(TRUE);
    CFont *oldFont=dc.SelectObject(&autofont);
    dc.SetBkMode(TRANSPARENT);
    dc.TextOut(100,100,"Hello World!");
    dc.SelectObject(oldFont);
}

As you can see, CAutoFont works just like a standard CFont object (in fact, it's derived from CFont), except that it has methods built into it for setting its parameters without having to mess with a lengthy LOGFONT structure. Included are two methods for turning the font into a string. This is useful for sending the font to and from the registry, for example. The methods are CAutoFont::ContractFont and CAutoFont::ExtractFont.

Update

CAutoFont now has more functionality built into it. I apologize for forgetting who mentioned it, but per the suggestion of another MFCer, I've added into the ContractFont and ExtractFont functions the ability to save and restore font color. There are also two new functions. GetFontFromDialog allows you to easily incorporate a CFontDialog into your application that automatically updates the CAutoFont class. A CFont object, and a reference to the font's color are also passed back (through pointers) to the caller, if desired. The second function, SetDC, sets a HDC reference for the class to use in calls to GetFontFromDialog, and SetFontColor.