Click here to Skip to main content
15,881,812 members
Articles / Multimedia / GDI

Automatic Font Handling Class

Rate me:
Please Sign up or sign in to vote.
4.95/5 (8 votes)
14 Aug 2014CPOL1 min read 145.3K   2.6K   41   29
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
I'm a Software Architect, working for a telecom in Portland, OR. My specialties are C#, Win32 C/C++ programming, Visual Basic (6 and .NET), and ASP(.NET), XML/XSL, and database programming. I'm currently learning F#. Completely different than anything I've ever done, but very cool. I'm married to the most wonderful woman, have a beautiful daughter that I'm very proud of, and a step-son that's rocking in high school. I'm also a 2nd degree blackbelt in Olympic style (WTF) Taekwondo, and have trained in Brazilian Jiujitsu, Jeet Kun Do, Krav Maga.

Comments and Discussions

 
Questionlicense Pin
doublewaterfish12-Aug-14 23:39
doublewaterfish12-Aug-14 23:39 
AnswerRe: license Pin
rahulsia8113-Aug-14 17:20
rahulsia8113-Aug-14 17:20 
GeneralRe: license Pin
Jamie Nordmeyer13-Aug-14 17:59
Jamie Nordmeyer13-Aug-14 17:59 
QuestionLicense Pin
Member 896106528-May-12 23:05
Member 896106528-May-12 23:05 
QuestionSetFontColor ?? Pin
ChrisRibe16-Mar-07 8:05
ChrisRibe16-Mar-07 8:05 
AnswerRe: SetFontColor ?? Pin
hairy_hats31-Mar-11 1:58
hairy_hats31-Mar-11 1:58 
GeneralUpdate is coming, I promise Pin
Jamie Nordmeyer1-Jan-03 14:52
Jamie Nordmeyer1-Jan-03 14:52 
GeneralRe: Update is coming, I promise Pin
hewat8-Mar-04 5:55
hewat8-Mar-04 5:55 
GeneralExtractFont -- HEADS UP! Pin
Tim Gard28-Sep-02 22:22
Tim Gard28-Sep-02 22:22 
GeneralRe: ExtractFont -- HEADS UP! Pin
Jamie Nordmeyer30-Sep-02 4:52
Jamie Nordmeyer30-Sep-02 4:52 
GeneralAnother solution ... Pin
cr9710-Jun-03 21:26
cr9710-Jun-03 21:26 
GeneralSmall typo Pin
Anonymous16-Sep-02 4:15
Anonymous16-Sep-02 4:15 
GeneralPoint Size Pin
Jonny27-Mar-02 22:23
Jonny27-Mar-02 22:23 
QuestionPrinting with the selected font? Pin
18-Mar-02 4:29
suss18-Mar-02 4:29 
AnswerRe: Printing with the selected font? Pin
Terence Russell30-May-03 19:56
Terence Russell30-May-03 19:56 
QuestionHow to get Width Pin
laotong1-Jan-02 15:37
laotong1-Jan-02 15:37 
GeneralPoint size Pin
24-Oct-01 0:38
suss24-Oct-01 0:38 
It would be nice to have a function SetSize to set the point size, because it is very difficult to calculate the height and width from the point size
GeneralGood work!!! Pin
KingsGambit1-Aug-01 2:35
KingsGambit1-Aug-01 2:35 
GeneralApply Button Pin
12-Jul-01 23:02
suss12-Jul-01 23:02 
GeneralStill one of my favourite classes Pin
Jason Hattingh7-Aug-00 4:24
sussJason Hattingh7-Aug-00 4:24 
GeneralRe: Still one of my favourite classes Pin
Jamie Nordmeyer8-Sep-00 13:52
Jamie Nordmeyer8-Sep-00 13:52 
GeneralExtension for some standard fonts Pin
Thomas Freudenberg1-Aug-00 0:18
Thomas Freudenberg1-Aug-00 0:18 
GeneralJamie, your email account seems to do not exist anymore... Pin
Thomas Freudenberg1-Aug-00 0:22
Thomas Freudenberg1-Aug-00 0:22 
GeneralRe: Jamie, your email account seems to do not exist anymore... Pin
Jamie Nordmeyer8-Sep-00 13:49
Jamie Nordmeyer8-Sep-00 13:49 
GeneralRe: Extension for some standard fonts Pin
j8-Sep-00 13:51
j8-Sep-00 13:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.