HighTechTalks DotNet Forums  

Install font from ClickOnce application

Dotnet Framework microsoft.public.dotnet.framework


Discuss Install font from ClickOnce application in the Dotnet Framework forum.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Thomas Taylor
 
Posts: n/a

Default Install font from ClickOnce application - 09-20-2006 , 07:47 PM






How can I distribute a TrueType font with my Windows Forms application using
ClickOnce?

Is there a direct/supported way to install via ClickOnce?

I tried including the ttf files as resources and copying to the fonts folder
on startup. This worked fine, but I can't manage to get them 'registered'
properly (in spite of making registry entries and api calls for
AddFontResource, CreateScalableFontResource, and SendMessage). These calls
appear to work, but I still can't use the font. So, if there is no direct
way to install with ClickOnce, any advice on doing the work from C# would be
appreciated.



Reply With Quote
  #2  
Old   
Jeffrey Tan[MSFT]
 
Posts: n/a

Default RE: Install font from ClickOnce application - 09-21-2006 , 07:56 AM






Hi Thomas,

Based on my understanding, your ClickOnce application copies TTF file to
the system fonts folder and p/invokes win32 AddFontResource API to register
the font. However, you find that your application still can not use the new
registered font yet.

Yes, I can reproduce this behavior in a normal .Net Winform application by
using the code snippet below:

<DllImport("gdi32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
_
Friend Shared Function AddFontResourceW(ByVal filename As String) As
Integer
End Function

<DllImport("gdi32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
_
Friend Shared Function RemoveFontResourceW(ByVal filename As String) As
Boolean
End Function

Public sFontFile As String = "C:\cookiehollow.ttf"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
System.IO.File.Copy(sFontFile, "C:\windows\fonts\cookiehollow.ttf")
Dim ret As Integer =
AddFontResourceW("C:\windows\fonts\cookiehollow.tt f")
If ret = 0 Then
MessageBox.Show(Marshal.GetLastWin32Error())
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdRefresh2.Click
Me.Button1.Font = New Font("CookieHollow", 10, FontStyle.Regular)
End Sub

However, after closing the application and running it again, I find that we
can use the cookiehollow.ttf now. So it seems that AddFontResource
registration does not take effect untile we restart the application. This
leads me to mention another known bug of .Net Winform:
InstalledFontCollection.GetFamilies does not detect fonts installed (or
removed) after GDI+ was started. This is actually not a .Net bug, but the
GDI+ bug, which is filed in our internal bug database. So I think the
AddFontResource GDI API may have the similar problem.

To workaround the problem, we may use .Net PrivateFontCollection class. For
example, the first time you are registering the TTF font, since the
application will not see the font yet. You may add the font into
PrivateFontCollection class, this will make the font usable to the current
running application:

Public oPFC As New PrivateFontCollection
Public sFontFile As String = "C:\cookiehollow.ttf"
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
oPFC.AddFontFile(sFontFile)
Me.Button1.Font = New Font(oPFC.Families(0), 10)
End Sub

Then in next running, you may use the already installed font without any
problem now. This works well on my side.

Hope this helps.

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.



Reply With Quote
  #3  
Old   
Thomas Taylor
 
Posts: n/a

Default RE: Install font from ClickOnce application - 09-21-2006 , 10:17 AM



Thanks for the reply, Jeffrey. I don't think I can get by with making it a
private font since the application can write files for use by other
applications (e.g., Excel), and the font will be need by the other
applications. So, I really need to to a 'regular' install of the font in
Windows.

I got the following prototype code working. Well, at least it appears to
work on a clean Win XP machine; it still does not work on a Win 2003 machine
(even under an admin account), but maybe that's a problem on that specific
machine.

internal static void InstallFont()
{
string fontsPath = GetFontsPath();
string ttfFile = System.IO.Path.Combine(fontsPath, "MyFont.TTF");;
string fotFile = System.IO.Path.Combine(fontsPath, "MyFont.FOT");;
int ret;

if (!System.IO.File.Exists(ttfFile))
{
//Write file from embedded resource
System.IO.File.WriteAllBytes(ttfFile, Properties.Resources.MyFont);
//Write resource file
ret = CreateScalableFontResource(0, fotFile, ttfFile, String.Empty);
//Add font resource
ret = AddFontResource(fotFile);
//Add registry entry so the font is also available next session
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Mi crosoft\Windows
NT\CurrentVersion\Fonts", "My Font (TrueType)", "MyFont.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.dl l")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
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.d ll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);

So, if anyone can point out something I'm doing wrong here, that would be
great.

I'm also still thinking that ClickOnce should support font installs; this
seems like a common task. Is there no way to get ClickOnce to do this for me?

Reply With Quote
  #4  
Old   
Jeffrey Tan[MSFT]
 
Posts: n/a

Default RE: Install font from ClickOnce application - 09-22-2006 , 04:24 AM



Hi Thomas,

Thanks for the feedback.

Actually, ClickOnce is only a deployment technology, it does not offer any
support for font installation currently, since this operation requires
extra coding. We have to code the font installation logic in the
application. So this question is not a ClickOnce specific.

Based on my test using the code snippet below, after I called InstallFont,
the Word application can use the font without any problem, so I think it
should work for Excel application. But without restarting the application,
the .Net winform application still can not use it. , do you get different
result? My testing machine is Win2003 SP1.

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");
System.IO.File.Copy(@"C:\cookiehollow.ttf", ttfFile);
int ret;

if (System.IO.File.Exists(ttfFile))
{
//Add font resource
ret = AddFontResource(ttfFile);
//Add registry entry so the font is also available next session
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Mi crosoft\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.dl l")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
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.d ll", CharSet
=System.Runtime.InteropServices.CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);

private void button2_Click(object sender, EventArgs e)
{
this.button2.Font = new Font("CookieHollow", 10, FontStyle.Regular);
}

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.


Reply With Quote
  #5  
Old   
Thomas Taylor
 
Posts: n/a

Default RE: Install font from ClickOnce application - 09-22-2006 , 02:17 PM



Jeffrey,

Yes, I am able to use the font, at least on my development machine (Win
2003) and a test machine (Win XP). I think the difference is that you have
to create the fot file (with CreateScalableFontResource) for GDI to be able
to use the font. At least that's what I understand from the API docs. I
still feel like I don't fully understand what I'm doing.

Thanks again for the response.
Thomas

""Jeffrey Tan[MSFT]"" wrote:

Quote:
Hi Thomas,

Thanks for the feedback.

Actually, ClickOnce is only a deployment technology, it does not offer any
support for font installation currently, since this operation requires
extra coding. We have to code the font installation logic in the
application. So this question is not a ClickOnce specific.

Based on my test using the code snippet below, after I called InstallFont,
the Word application can use the font without any problem, so I think it
should work for Excel application. But without restarting the application,
the .Net winform application still can not use it. , do you get different
result? My testing machine is Win2003 SP1.

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");
System.IO.File.Copy(@"C:\cookiehollow.ttf", ttfFile);
int ret;

if (System.IO.File.Exists(ttfFile))
{
//Add font resource
ret = AddFontResource(ttfFile);
//Add registry entry so the font is also available next session
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Mi crosoft\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.dl l")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
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.d ll", CharSet
=System.Runtime.InteropServices.CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr lParam);

private void button2_Click(object sender, EventArgs e)
{
this.button2.Font = new Font("CookieHollow", 10, FontStyle.Regular);
}

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.



Reply With Quote
  #6  
Old   
Jeffrey Tan[MSFT]
 
Posts: n/a

Default RE: Install font from ClickOnce application - 09-25-2006 , 04:31 AM



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\Mi crosoft\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.dl l")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
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.d ll", 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.


Reply With Quote
  #7  
Old   
sdc@kampusonline.com
 
Posts: n/a

Default Re: Install font from ClickOnce application - 10-05-2006 , 09:20 AM



This is not working with .PFM or .PFB font file so please help me how can i install .pfm or .pfb font.
I am working with .Net Framwork 1.1 with C#.
---
Posted via www.DotNetSlackers.com

Reply With Quote
  #8  
Old   
Thomas Taylor
 
Posts: n/a

Default RE: Install font from ClickOnce application - 11-08-2006 , 03:08 PM



Jeffrey,

Sorry for the extremely slow response. It may be moot at this point, but I
wanted to respond in case it's useful to someone else.

To answer your question, yes, I can use the font after running the code from
my previous post (I did not try your code, but it looks pretty much the same).

To be specific, I can create a new font:
Font myFont = new Font("MyFont", 32f);

And then I can use it to draw on a graphics object:

g.DrawString("Hello", mFont, Brushes.Black, new PointF(10, 10));

Thanks again for the responses.

I still think lack of support for font installs in ClickOnce is a big hole.
ClickOnce rocks, but installing a font seems like a REALLY common task.

Reply With Quote
  #9  
Old   
Jeffrey Tan[MSFT]
 
Posts: n/a

Default RE: Install font from ClickOnce application - 11-09-2006 , 02:10 AM



Hi Thomas,

Thanks very much for your feedback.

Since the test result on my side reveals that it still can be used in the
current winform application without restarting, I assume you mean that you
can use the font in *other* applications after its installation with code,
yes?

Yes, I see your concern, installing a new font may be a common requirement
in ClickOnce application, so including such a feature should be useful.

The reason the ClickOnce did not include this feature is that the ClickOnce
deloyment normally does not touch the system directories. This is an
important difference with MSI deployment technology. In the table of "Key
Differences" section of the article below, you will see that, the ClickOnce
deployment does not conver:
"Install to GAC"(GAC is another system directory)
"Write to Registry"
"Install for All Users"
etc... While these can all be done with MSI:
"Choosing Between ClickOnce and Windows Installer"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/clickoncevsinstaller.asp

So we have to code ourselves for the font installation.

Hope this makes sense to you. 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.


Reply With Quote
  #10  
Old   
Thomas Taylor
 
Posts: n/a

Default RE: Install font from ClickOnce application - 11-09-2006 , 10:01 PM






Jeffrey,

I was using the newly installed font to draw pages for printing via
PrintDocument. This does seem to work fine without restarting the
application. Specifically, I can print to "Microsoft Office Document Image
Writer", and the new font works fine.

However, if I try to set the font for a label on a form or draw directly on
the form, the font does not work. I think this confirms the behavior that
described seeing.

Below is the code for a simple form that I used to test (it's in two parts:
form and code-behind).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace FontTest
{
public partial class FontInstallTestForm : Form
{
public FontInstallTestForm()
{
InitializeComponent();
}

private void installButton_Click(object sender, EventArgs e)
{
string fontsPath = GetFontsPath();
string destFile;
string fotFile;
int ret;

destFile = System.IO.Path.Combine(fontsPath, "FREE3OF9.TTF");
if (!System.IO.File.Exists(destFile))
{
string sourceFile = @"c:\FREE3OF9.TTF";
System.IO.File.Copy(sourceFile, destFile);
fotFile = System.IO.Path.Combine(fontsPath, "FREE3OF9.FOT");
ret = CreateScalableFontResource(0, fotFile, destFile, String.Empty);
ret = AddFontResource(fotFile);
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Mi crosoft\Windows
NT\CurrentVersion\Fonts", "Free 3 of 9 Regular (TrueType)", "FREE3OF9.TTF",
RegistryValueKind.String);
ret = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, new IntPtr(0), new
IntPtr(0));
}
}

private void setFontButton_Click(object sender, EventArgs e)
{
Font font = new Font("Free 3 of 9", 32f);

label.Font = font;
label.Text = textBox.Text;

Graphics g = this.CreateGraphics();
g.DrawString(textBox.Text, font, Brushes.Black, 12, 200);

}

#region 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;
public static string GetFontsPath()
{
StringBuilder sb = new StringBuilder(MAX_PATH);
SHGetFolderPath(IntPtr.Zero, CSIDL_FONTS, IntPtr.Zero, 0, sb);
return sb.ToString();
}
#endregion

#region PInvoke to 'register' fonts and broadcast addition
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dl l")]
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.d ll", CharSet =
System.Runtime.InteropServices.CharSet.Auto)]
static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr
lParam);
#endregion

}
}


namespace FontTest
{
partial class FontInstallTestForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed;
otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.installButton = new System.Windows.Forms.Button();
this.textBox = new System.Windows.Forms.TextBox();
this.label = new System.Windows.Forms.Label();
this.setFontButton = new System.Windows.Forms.Button();
this.textBoxLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// installButton
//
this.installButton.Location = new System.Drawing.Point(12, 12);
this.installButton.Name = "installButton";
this.installButton.Size = new System.Drawing.Size(119, 23);
this.installButton.TabIndex = 0;
this.installButton.Text = "Install Font";
this.installButton.UseVisualStyleBackColor = true;
this.installButton.Click += new System.EventHandler(this.installButton_Click);
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(93, 64);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(200, 20);
this.textBox.TabIndex = 1;
this.textBox.Text = "*123ABC*";
//
// label
//
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(12, 151);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(41, 13);
this.label.TabIndex = 2;
this.label.Text = "<label>";
//
// setFontButton
//
this.setFontButton.Location = new System.Drawing.Point(12, 110);
this.setFontButton.Name = "setFontButton";
this.setFontButton.Size = new System.Drawing.Size(119, 23);
this.setFontButton.TabIndex = 3;
this.setFontButton.Text = "Set Font and Text";
this.setFontButton.UseVisualStyleBackColor = true;
this.setFontButton.Click += new System.EventHandler(this.setFontButton_Click);
//
// textBoxLabel
//
this.textBoxLabel.AutoSize = true;
this.textBoxLabel.Location = new System.Drawing.Point(12, 67);
this.textBoxLabel.Name = "textBoxLabel";
this.textBoxLabel.Size = new System.Drawing.Size(75, 13);
this.textBoxLabel.TabIndex = 4;
this.textBoxLabel.Text = "Text for Label:";
//
// FontInstallTestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(447, 285);
this.Controls.Add(this.textBoxLabel);
this.Controls.Add(this.setFontButton);
this.Controls.Add(this.label);
this.Controls.Add(this.textBox);
this.Controls.Add(this.installButton);
this.Name = "FontInstallTestForm";
this.Text = "Font Install Test";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button installButton;
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Label label;
private System.Windows.Forms.Button setFontButton;
private System.Windows.Forms.Label textBoxLabel;
}
}





Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.