Hi Thomas,
Thanks for your feedback!
Oh, I missed this CreateScalableFontResource API, sorry for this.
Yes, to install a TrueType font, a scalable font resource file needs to be
created. This scalable font resource file stores the name of a TrueType
font file so that GDI knows where to find the file. To create a scalable
font resource file, call the GDI function CreateScalableFontResource with
an integer flag, the name of the font resource file to be generated, an
existing TrueType font file name, and the path to the files if they do not
contain a complete path.
Thank you for pointing this out.
Do you mean you can use the font after installation in other
Applications(such as Excel) or you can use it in your ClickOnce
installation application? Based on my testing(full code listed below),
after adding CreateScalableFontResource calling, I still can not use the
installed font in the .Net winform application without restarting the
application. Do you get the same behavior as me? Thank you for confirming
this.
private void button1_Click(object sender, EventArgs e)
{
InstallFont();
}
internal static void InstallFont()
{
string fontsPath = GetFontsPath();
string ttfFile = System.IO.Path.Combine(fontsPath, "cookiehollow.ttf");
string fotFile = System.IO.Path.Combine(fontsPath, "cookiehollow.fot");
System.IO.File.Copy(@"C:\cookiehollow.ttf", ttfFile);
int ret;
if (System.IO.File.Exists(ttfFile))
{
ret = CreateScalableFontResource(0, fotFile, ttfFile, null);
if (ret==0)
{
MessageBox.Show("CreateScalableFontResource failed with error"
+ Marshal.GetLastWin32Error().ToString());
return;
}
//Add font resource
ret = AddFontResource(ttfFile);
if (ret==0)
{
MessageBox.Show("AddFontResource failed with error" +
Marshal.GetLastWin32Error().ToString());
return;
}
//Add registry entry so the font is also available next session
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Fonts",
"Cookie Hollow Regular (TrueType)", "cookiehollow.ttf",
RegistryValueKind.String);
//Broadcast to let all top-level windows know about change
ret = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, new IntPtr(0), new
IntPtr(0));
}
}
// PInvoke to look up fonts path
[System.Runtime.InteropServices.DllImport("shfolder.dll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
IntPtr hToken, int dwFlags, StringBuilder lpszPath);
private const int CSIDL_FONTS = 0x0014;
private const int MAX_PATH = 260;
private static string GetFontsPath()
{
StringBuilder sb = new StringBuilder(MAX_PATH);
SHGetFolderPath(IntPtr.Zero, CSIDL_FONTS, IntPtr.Zero, 0, sb);
return sb.ToString();
}
// PInvoke to 'register' fonts and broadcast addition
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern int CreateScalableFontResource(uint fdwHidden, string
lpszFontRes, string lpszFontFile, string lpszCurrentPath);
private static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
private const uint WM_FONTCHANGE = 0x001D;
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet
=System.Runtime.InteropServices.CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);
Thanks.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.