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

Automatic Font Handling Class

By , 28 Dec 1999
 

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
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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionLicensememberMember 896106528 May '12 - 23:05 
The article does not include any specified license, so, can you tell what license should apply?
Is it The Code Project Open License (CPOL)?
 
Thanks
Cornelia
QuestionSetFontColor ??memberChrisRibe16 Mar '07 - 8:05 
Hi !
Great class simplifies things a bit.
 
How do you use the SetFontColor method you seem to need to set the current DC ?
Whats that all about?
 
Chris
AnswerRe: SetFontColor ??memberviaducting31 Mar '11 - 1:58 
Colour is handled separately. You need the DC to set the colour.
GeneralUpdate is coming, I promisememberJamie Nordmeyer1 Jan '03 - 14:52 
OK, I'm currently working on the update to this class, and have implemented most of what you all have suggested. I'm extremely busy of late (thus the long delay in doing all this), but I wanted to assure you that an update IS coming. Thanks for the patience! Smile | :)
 
Jamie Nordmeyer
Portland, Oregon, USA
GeneralRe: Update is coming, I promisememberhewat8 Mar '04 - 5:55 
Hi Jamie,
Thanks for your interesting article. Have you finished/posted your update somewhere on the net ? (this article on codeproject hasn't changed for some time...)
Peter
GeneralExtractFont -- HEADS UP!memberTim Gard28 Sep '02 - 22:22 
First off, this is a great class, really useful.
 
I had a problem, though, that vexed me for a while: I was storing the string generated by ContractFont in a document, but was losing the string if I made any other document changes and re-saved.
 
Here's the issue: ExtractFont passes the string by reference. It was eating up my string when I called it!
 
So I removed the reference operators (&) from the function definition and implementation and now all is fine.
 
I suppose that if somebody didn't want to monkey with the class, they could always copy their string to a dummy string and pass *that* to the function.
 
Thanks for the class. Smile | :)
 
Tim
GeneralRe: ExtractFont -- HEADS UP!memberJamie Nordmeyer30 Sep '02 - 4:52 
Thanks for the info, Tim. One of these eons (hopefully soon) I need to update this thing and repost it. I'll probably have time in a couple weeks (I have my black belt test this weekend, so I'll try for next).
 
Jamie Nordmeyer
Portland, Oregon, USA
GeneralAnother solution ...membercr9710 Jun '03 - 21:26 
.. is to do at first
 
CString strWork(str);
 
and then use strWork with GetToken() ...
 
cr97
GeneralSmall typosussAnonymous16 Sep '02 - 4:15 
Great code, but you need to change GetOrientation so that it returns lfOrientation and not lfEscapement.
 
In other words, it should look like:
 
LONG CAutoFont::GetOrientation()
{
return lf.lfOrientation;
}
GeneralPoint SizememberJonny27 Mar '02 - 22:23 
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

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

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