Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC

Finding a Font file from a Font name

Rate me:
Please Sign up or sign in to vote.
4.56/5 (20 votes)
10 Jul 2001CPOL3 min read 183.4K   6.4K   35  
How to find the name of a font file, given the display name of a font
// GetFontFile.cpp
//
// Copyright (C) 2001 Hans Dietrich
//
// This software is released into the public domain.  
// You are free to use it in any way you like.
//
// This software is provided "as is" with no expressed 
// or implied warranty.  I accept no liability for any 
// damage or loss of business that this software may cause. 
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GetFontFile.h"
#include "GetNameValue.h"
#include "GetWinVer.h"

///////////////////////////////////////////////////////////////////////////////
// GetFontFile
//
// Note:  This is *not* a foolproof method for finding the name of a font file.
//        If a font has been installed in a normal manner, and if it is in
//        the Windows "Font" directory, then this method will probably work.
//        It will probably work for most screen fonts and TrueType fonts.
//        However, this method might not work for fonts that are created 
//        or installed dynamically, or that are specific to a particular
//        device, or that are not installed into the font directory.

BOOL GetFontFile(LPCTSTR lpszFontName, CString& strDisplayName, CString& strFontFile)
{
	ASSERT(lpszFontName && lpszFontName[0] != 0);

	_TCHAR szName[2 * MAX_PATH];
	_TCHAR szData[2 * MAX_PATH];

	int nVersion;
	CString strVersion;
	GetWinVer(strVersion, &nVersion);
	TRACE(_T("strVersion=%s\n"), strVersion);

	CString strFont;
	if ((nVersion >= WNTFIRST) && (nVersion <= WNTLAST)) 
		strFont = _T("Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts");
	else
		strFont = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Fonts");

	strFontFile.Empty();

	BOOL bResult = FALSE;

	while (GetNextNameValue(HKEY_LOCAL_MACHINE, strFont, szName, szData) == ERROR_SUCCESS)
	{
		if (strnicmp(lpszFontName, szName, strlen(lpszFontName)) == 0)
		{
			TRACE(_T("found font\n"));
			strDisplayName = szName;
			strFontFile = szData;
			bResult = TRUE;
			break;
		}

		strFont.Empty();	// this will get next value, same key
	}

	GetNextNameValue(HKEY_LOCAL_MACHINE, NULL, NULL, NULL);	// close the registry key

	return bResult;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions