Hi,
Unfortunately, GDI+ will not recognize the shortcuts and AddFontResource
only adds a font for the current session. It is not a permanently
installed font. To permanently install the font, try the code below (I
have not tested it). The font needs to be permanently installed on the
system for InstalledFontCollection to recognize it. The
InstalledFontCollection is instantiated when the GDI+ application starts.
Any fonts added after the GDI+ application starts will not be detected.
After you have added the font (permantently as described above), you must
restart the application.
// The following code is for demonstration purposes only and will not work
on Win9x. It was written before XP, so you may need to modify it.
//#define _WIN32_WINNT 0x0500
#include <Windows.H>
#include <StdIO.H>
void PrintError( DWORD dwError, LPCSTR lpString )
{
#define MAX_MSG_BUF_SIZE 512
char *msgBuf;
DWORD cMsgLen;
cMsgLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, dwError,
MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR) &msgBuf, MAX_MSG_BUF_SIZE,
NULL);
printf( "%s Error [%d]:: %s\n", lpString, dwError, msgBuf );
LocalFree( msgBuf );
#undef MAX_MSG_BUF_SIZE
}
int main( int argc, char *argv[] )
{
OSVERSIONINFO osv;
int iRet;
LONG lRet;
int iDisposition;
HKEY hKey = NULL;
TCHAR buffer[200];
GLYPHSET gs;
LOCALESIGNATURE ls;
// What version of Windows are we running?
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osv);
// Abort if NT4 or Win2k not being used...
if (!((osv.dwPlatformId == VER_PLATFORM_WIN32_NT) && // NT
(((osv.dwMajorVersion == 4) && (osv.dwMinorVersion == 0)) || // 4.0
((osv.dwMajorVersion == 5) && (osv.dwMinorVersion == 0))))) // 5.0
(Win2k)
{
printf("Requires NT 4.0 or Windows 2000\n");
goto ABORT;
}
// Must specify printer name, font name, font file name...
if (argc != 3)
{
printf("Syntax: %s <font name> <.TTF file name>\n", argv[0]);
goto ABORT;
}
// Add the font to the local session...
SetLastError(0);
iRet = AddFontResource(argv[2]);
if (iRet == 0)
{
wsprintf(buffer, "AddFontResource(%s)", argv[2]);
PrintError(GetLastError(), buffer);
goto ABORT;
}
// Get a registry key for the Fonts information in the registry...
SetLastError(0);
lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion\\Fonts",
0, "REG_SZ", 0, KEY_WRITE, NULL, &hKey, &iDisposition);
if (lRet != ERROR_SUCCESS)
{
PrintError(GetLastError(), "RegCreateKeyEx");
goto ABORT;
}
// Add the font to the registry...
SetLastError(0);
lRet = RegSetValueEx(hKey, argv[1], 0, REG_SZ, argv[2], lstrlen(argv[2]) +
sizeof(TCHAR));
if (lRet != ERROR_SUCCESS)
{
PrintError(GetLastError(), "RegSetValueEx");
goto ABORT;
}
// Done with the registry...
SetLastError(0);
lRet = RegCloseKey(hKey);
if (lRet != ERROR_SUCCESS)
{
PrintError(GetLastError(), "RegCloseKey");
goto ABORT;
}
hKey = NULL;
// Notify other running applications that a font was added...
SendMessageTimeout(HWND_BROADCAST, WM_FONTCHANGE, 0L, 0L, SMTO_NORMAL,
1000,
NULL);
// Notify success...
printf("\n\"%s\" (file: %s) was successfully added to the system.\n",
argv[1],
argv[2]);
/* Cleanup and return... */
ABORT:
if (hKey)
RegCloseKey(hKey);
return 0;
}
Thanks,
-Greg
Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit
http://www.microsoft.com/security for current information on security.
--------------------
Thread-Topic: List of activated fonts does not include shortcut activations
thread-index: AcP9YqoSmJ+t7lfoRpem8QAnbtaVFA==
X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.gener
al
From: "=?Utf-8?B?anVzb3Zza3lAYW5vdGhlcnJldGFyZGVkLmNvbQ==?="
Subject: List of activated fonts does not include shortcut activations
Date: Fri, 27 Feb 2004 10:51:09 -0800
Lines: 6
Message-ID: <
[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.gener
al
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:126336 microsoft.public.dotnet.framework.windowsforms:62770
NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
Our font management tool, Suitcase, activates fonts dynamically by
creating shortcuts in the Windows font folder. Using
System.Drawing.Text.InstalledFontCollection to get the list of font
families from the system yields only the fonts whose files are physically
in the system font folder- not the shortcuts. None of our applications
have any problems using the fonts activated by shortcuts. Can anyone
recommend another way to retrieve the complete list of fonts? Thanks for
any suggestions.
Josh Usovsky
Code:
System.Drawing.Text.InstalledFontCollection installedFonts = new
System.Drawing.Text.InstalledFontCollection();
System.Drawing.FontFamily[] fams = installedFonts .Families;