Easy Way to Set the LOGFONT Structure

Environment: Windows 95/98/NT, VC4.2+

To set the new font for a window or control a CFont object is often used. In order to use this object a LOGFONT structure must be passed to it to create the font object in memory. Filling the members of the LOGFONT structure can be time consuming and prone to errors so I created a short project which allows a programmer to select a desired font, which creates a string containing values for all the members of the LOGFONT structure. Next, the programmer uses the following functions to create a new CFont object.

static const TCHAR* pszL = NULL;
CString _S(int i)
CString sTemp; AfxExtractSubString(sTemp, pszL, i, TCHAR(‘,’));
return sTemp;

#ifdef _UNICODE
#define _L(i) _wtol(_S(i))
#define _L(i) atol(_S(i))

#define _B(i) (BYTE)_L(i)
#define StartConversion(s) pszL = s
void FillLogFont(LOGFONT& logFont, LPCTSTR lpszFontString )
logFont.lfHeight = _L(0);
logFont.lfWidth = _L(1);
logFont.lfEscapement = _L(2);
logFont.lfOrientation = _L(3);
logFont.lfWeight = _L(4);
logFont.lfItalic = _B(5);
logFont.lfUnderline = _B(6);
logFont.lfStrikeOut = _B(7);
logFont.lfCharSet = _B(8);
logFont.lfOutPrecision = _B(9);
logFont.lfClipPrecision = _B(10);
logFont.lfQuality = _B(11);
logFont.lfPitchAndFamily = _B(12);
memcpy(logFont.lfFaceName, _S(13), LF_FACESIZE);

How to generate the font string:

Run the GetF executable, select a font, and then copy the string created at the bottom of the dialog into the clipboard. Paste this string into your code as the second parameter of the FillLogFont(…) function.

Download source – 16KB

More by Author

Must Read