RE: Install font from ClickOnce application



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\Microsoft\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.dll")]
private static extern int AddFontResource(string lpszFilename);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
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)]
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;
}
}




.