Easy Way to Set the LOGFONT Structure | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 8, 2000
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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))
#else
	#define _L(i) atol(_S(i))
#endif
#define _B(i) (BYTE)_L(i)
#define StartConversion(s) pszL = s
void FillLogFont(LOGFONT& logFont, LPCTSTR lpszFontString )
{
	StartConversion(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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.