Click here to Skip to main content
Click here to Skip to main content

Automatic Font Handling Class

, 28 Dec 1999
Rate this:
Please Sign up or sign in to vote.
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jamie Nordmeyer
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.
Follow on   Twitter

Comments and Discussions

 
QuestionPrinting with the selected font? PinmemberMarcus Fries18-Mar-02 4:29 
AnswerRe: Printing with the selected font? PinmemberTerence Russell30-May-03 19:56 
QuestionHow to get Width Pinmemberlaotong1-Jan-02 15:37 
GeneralPoint size PinmemberAnonymous24-Oct-01 0:38 
GeneralGood work!!! PinmemberRejeesh.T.S1-Aug-01 2:35 
GeneralApply Button PinmemberNB12-Jul-01 23:02 
GeneralStill one of my favourite classes PinsussJason Hattingh7-Aug-00 4:24 
GeneralRe: Still one of my favourite classes PinsussJamie Nordmeyer8-Sep-00 13:52 
GeneralExtension for some standard fonts PinmemberThomas Freudenberg1-Aug-00 0:18 
GeneralJamie, your email account seems to do not exist anymore... PinsussThomas Freudenberg1-Aug-00 0:22 
GeneralRe: Jamie, your email account seems to do not exist anymore... PinsussJamie Nordmeyer8-Sep-00 13:49 
GeneralRe: Extension for some standard fonts PinsussJ8-Sep-00 13:51 
GeneralRe: Extension for some standard fonts PinsussJamie Nordmeyer8-Sep-00 13:51 
QuestionResource leak? PinsussSteve Kowald16-Jun-00 8:14 
AnswerRe: Resource leak? PinsussJamie Nordmeyer8-Sep-00 13:48 
AnswerRe: Resource leak? PinmemberRick York10-Jun-03 8:39 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 29 Dec 1999
Article Copyright 1999 by Jamie Nordmeyer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid