Click here to Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionSetFontColor ??
ChrisRibe
9:05 16 Mar '07  
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
GeneralUpdate is coming, I promise
Jamie Nordmeyer
15:52 1 Jan '03  
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 promise
hewat
6:55 8 Mar '04  
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!
Tim Gard
23:22 28 Sep '02  
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!
Jamie Nordmeyer
5:52 30 Sep '02  
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 ...
cr97
22:26 10 Jun '03  
.. is to do at first

CString strWork(str);
and then use strWork with GetToken() ...

cr97
GeneralSmall typo
Anonymous
5:15 16 Sep '02  
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 Size
Jonny
23: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

GeneralPrinting with the selected font?
Marcus Fries
5:29 18 Mar '02  
How to handle the printing in any MFC MDI Application using the Autofont in OnDraw

if I select a font and size, go to a print preview the font ist realy small.

Do anyone have any idea's how to get the real font size for printing?

Thanks Marcus
PS: By the way it's a realy nice class.
GeneralRe: Printing with the selected font?
Terence Russell
20:56 30 May '03  
This snippet will compute a font height for the printer that has the same proportions of the font as it appears on screen. (However, I believe this is only true if the printer has the same values for both LOGPIXELSY and LOGPIXELSX.)


if ( pDC->IsPrinting )
{
CAutoFont myFont;

int nPrintHeight = MulDiv(myFont.GetHeight(), pDC->GetDeviceCaps(LOGPIXELSY),96);

myFont.SetHeight( nHeightDPI );

// ... do some rendering here

}
GeneralHow to get Width
laotong
16:37 1 Jan '02  
lf.lfWidth always equals to 0...How to get its real value? Frown
GeneralPoint size
Anonymous
1:38 24 Oct '01  
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!!!
Rejeesh.T.S
3:35 1 Aug '01  
The CAutoFont class is a very useful class, which makes the font-handling quite easy.

Smile
GeneralApply Button
NB
0:02 13 Jul '01  
Can anyone help me to extract Color from color combo in CFontDialog box when apply button is pressed.When I tried to use CFHookProc but was not successful.Some code piece will be of great use.Cry
GeneralStill one of my favourite classes
Jason Hattingh
5:24 7 Aug '00  
... thanks jami
GeneralRe: Still one of my favourite classes
Jamie Nordmeyer
14:52 8 Sep '00  
Thanks for the encouraging word, Jason! :
GeneralExtension for some standard fonts
Thomas Freudenberg
1:18 1 Aug '00  
Hi Jamie,

I have extended your class--which BTW is very helpful--by some functions to get Windows standard fonts, such as menu font and so on:
BOOL CAutoFont::CreateMenuFont()
{
NONCLIENTMETRICS ncm = {0};
ncm.cbSize = sizeof( NONCLIENTMETRICS );
if (!SystemParametersInfo( SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0 ))
return FALSE;

DeleteObject();
return CreateFontIndirect( &ncm.lfMenuFont );
}

BOOL CAutoFont::CreateMessageFont()
{
NONCLIENTMETRICS ncm = {0};
ncm.cbSize = sizeof( NONCLIENTMETRICS );
if (!SystemParametersInfo( SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0 ))
return FALSE;

DeleteObject();
return CreateFontIndirect( &ncm.lfMessageFont );
}

BOOL CAutoFont::CreateStatusFont()
{
NONCLIENTMETRICS ncm = {0};
ncm.cbSize = sizeof( NONCLIENTMETRICS );
if (!SystemParametersInfo( SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0 ))
return FALSE;

DeleteObject();
return CreateFontIndirect( &ncm.lfStatusFont );
}

GeneralJamie, your email account seems to do not exist anymore...
Thomas Freudenberg
1:22 1 Aug '00  
> This message was created automatically by mail delivery software.
>
> A message that you sent could not be delivered to all of its recipients. The
> following address(es) failed:
>
> nordyj@gte.net:
> SMTP error from remote mailer after RCPT TO:
> :
> host mtapop3pub.gte.net [206.46.170.36]:
> 550 Invalid recipient:
>
GeneralRe: Jamie, your email account seems to do not exist anymore...
Jamie Nordmeyer
14:49 8 Sep '00  
I'm trying to get that fixed right now, but unfortunately, I can't change my records for some reason.

The correct address to be mailing to is:
jamie.nordmeyer@mcgnw.com
GeneralRe: Extension for some standard fonts
J
14:51 8 Sep '00  
Cool! I am dire need of updating the control (been busy on other projects), so I'll include this in the next release (with proper representation, of course)
GeneralRe: Extension for some standard fonts
Jamie Nordmeyer
14:51 8 Sep '00  
Cool! I am dire need of updating the control (been busy on other projects), so I'll include this in the next release (with proper representation, of course)
GeneralResource leak?
Steve Kowald
9:14 16 Jun '00  
If you create a font using CreateFontIndirect() you must call DeleteObject() in the destructor of your class.

CAutoFont::~CAutoFont()
{
// SSK - must delete the object
DeleteObject();
}
GeneralRe: Resource leak?
Jamie Nordmeyer
14:48 8 Sep '00  
Ah, good point. Thanks for reminding me
GeneralRe: Resource leak?
Rick York
9:39 10 Jun '03  
This is not necessary in VC++ v6.0. If you use the CreateFontIndirect method of CFont that is. CFont is derived from CGdiObject and its destructor calls DeleteObject so it is already taken care of.


The Ten Commandments For C Programmers


Last Updated 29 Dec 1999 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010