Click here to Skip to main content
Licence 
First Posted 19 Nov 1999
Views 99,082
Bookmarked 22 times

Automatic Font Handling Class

By | 28 Dec 1999 | Article
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

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionLicense PinmemberMember 896106523:05 28 May '12  
QuestionSetFontColor ?? PinmemberChrisRibe8:05 16 Mar '07  
AnswerRe: SetFontColor ?? Pinmemberviaducting1:58 31 Mar '11  
GeneralUpdate is coming, I promise PinmemberJamie Nordmeyer14:52 1 Jan '03  
GeneralRe: Update is coming, I promise Pinmemberhewat5:55 8 Mar '04  
GeneralExtractFont -- HEADS UP! PinmemberTim Gard22:22 28 Sep '02  
GeneralRe: ExtractFont -- HEADS UP! PinmemberJamie Nordmeyer4:52 30 Sep '02  
GeneralAnother solution ... Pinmembercr9721:26 10 Jun '03  
GeneralSmall typo PinsussAnonymous4:15 16 Sep '02  
GeneralPoint Size PinmemberJonny22:23 27 Mar '02  
Hello.
 
This class is simple, and useful. But The point size of a font is useful too. If you need it, create a member function with the following contain :
return (-MulDiv (Font.GetHeight(), 72, GetDeviceCaps (GetDC()->GetSafeHdc(), LOGPIXELSY))) ;
It returns the height of a font int point size.
 
Have a good day. Smile | :)
 
Jonny

QuestionPrinting with the selected font? PinmemberMarcus Fries4:29 18 Mar '02  
AnswerRe: Printing with the selected font? PinmemberTerence Russell19:56 30 May '03  
QuestionHow to get Width Pinmemberlaotong15:37 1 Jan '02  
GeneralPoint size PinmemberAnonymous0:38 24 Oct '01  
GeneralGood work!!! PinmemberRejeesh.T.S2:35 1 Aug '01  
GeneralApply Button PinmemberNB23:02 12 Jul '01  
GeneralStill one of my favourite classes PinsussJason Hattingh4:24 7 Aug '00  
GeneralRe: Still one of my favourite classes PinsussJamie Nordmeyer13:52 8 Sep '00  
GeneralExtension for some standard fonts PinmemberThomas Freudenberg0:18 1 Aug '00  
GeneralJamie, your email account seems to do not exist anymore... PinsussThomas Freudenberg0:22 1 Aug '00  
GeneralRe: Jamie, your email account seems to do not exist anymore... PinsussJamie Nordmeyer13:49 8 Sep '00  
GeneralRe: Extension for some standard fonts PinsussJ13:51 8 Sep '00  
GeneralRe: Extension for some standard fonts PinsussJamie Nordmeyer13:51 8 Sep '00  
QuestionResource leak? PinsussSteve Kowald8:14 16 Jun '00  
AnswerRe: Resource leak? PinsussJamie Nordmeyer13:48 8 Sep '00  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 29 Dec 1999
Article Copyright 1999 by Jamie Nordmeyer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid