システム開発・Webサイト構築 プラスラス

2009/3/1 日曜日

【.NET】プライベートフォント(インストールされていないフォント)を使用する

このエントリーを含むはてなブックマーク Yahoo!ブックマークに登録 Google ブックマーク del.icio.us

VB.NETやC#で、プライベートフォントを使用する方法です。

 image

System.Drawing.Text.PrivateFontCollectionクラスを使用すると、プログラム中でシステムにインストールされていないアプリケーション固有のフォントを使えます。

バーコード用のフォントや、独自の記号を入れたフォントなど、自分のプログラムでしか使わないフォントであれば、このような方法で動的に読み込んで用いると良いでしょう。

(インストールすると他でも使えてしまうので、アンインストール時に残すべきなのか考慮するのが面倒ですし。)

以下で、紹介するサンプルは、 Label (名前は label1 )を一つ貼り付けただけのフォームです。

実行時に、プライベートフォントとして動的に読み込んだ ttf ファイル(バーコードフォント)をラベルに設定しています。

なお、このバーコードフォントは、株式会社テクニカルのWebサイト 内のhttp://barcode.technical.jp/handbook1/chapter-7-1.html よりダウンロードしたものを使いました。

このフォントは無償で使用でき、再配布も可能です。

'VB.NETのサンプル

Public Partial Class Form1
    Inherits Form
    Public Sub New()
        InitializeComponent()

        'フォントをセット
        SetPrivateFont()
    End Sub

    'フィールドにPrivateFontCollectionを用意
    Private fontCollection As New System.Drawing.Text.PrivateFontCollection()
    'フィールドにFontを用意
    Private displayFont As Font

    Private Sub SetPrivateFont()
        'フォントのパス
        Dim fontPath As String = "CODE128.ttf"

        'PrivateFontCollectionに追加します
        fontCollection.AddFontFile(fontPath)

        '使いたいフォント名を見つけてFontFamilyオブジェクトを変数にセット
        Dim addedFont As FontFamily = Nothing
        For Each ff As FontFamily In fontCollection.Families
            If ff.Name = "CODE128" Then
                addedFont = ff
                Exit For
            End If
        Next

        If addedFont Is Nothing Then
            Exit Sub
            'なぜかフォントがなかった
        End If

        'Fontオブジェクトを作成します
        '(フォントファミリーとフォントスタイル以外は、
        'フォームに置かれた label1 のプロパティと同じに指定しています。)
        displayFont = New Font(addedFont, _
                       label1.Font.Size, _
                       FontStyle.Regular, _
                       label1.Font.Unit)

        'label1のフォントを指定します
        label1.Font = displayFont

        '値を表示します
        label1.Text = "{123456789}"

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If

        'label1のフォントを既定のフォントにしておく
        If label1.Font = displayFont Then
            label1.Font = Nothing
        End If

        '作成したオブジェクトの後始末をします。
        If displayFont IsNot Nothing Then
            displayFont.Dispose()
            displayFont = Nothing
        End If

        If fontCollection IsNot Nothing Then
            fontCollection.Dispose()
            fontCollection = Nothing
        End If

        MyBase.Dispose(disposing)
    End Sub
End Class

//C#のサンプル

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        //フォントをセット
        SetPrivateFont();
    }

    //フィールドにPrivateFontCollectionを用意
    private System.Drawing.Text.PrivateFontCollection fontCollection
        = new System.Drawing.Text.PrivateFontCollection();
    //フィールドにFontを用意
    private Font displayFont;

    private void SetPrivateFont()
    {
        //フォントのパス
        string fontPath = "CODE128.ttf";

        //PrivateFontCollectionに追加します
        fontCollection.AddFontFile(fontPath);

        //使いたいフォント名を見つけてFontFamilyオブジェクトを変数にセット
        FontFamily addedFont = null;
        foreach (FontFamily ff in fontCollection.Families)
        {
            if (ff.Name == "CODE128")
            {
                addedFont = ff;
                break;
            }
        }

        if(addedFont == null)
        {    //なぜかフォントがなかった
            return;
        }

        //Fontオブジェクトを作成します
        //(フォントファミリーとフォントスタイル以外は、
        //フォームに置かれた label1 のプロパティと同じに指定しています。)
        displayFont = new Font(addedFont,
                       label1.Font.Size,
                       FontStyle.Regular,
                       label1.Font.Unit);

        //label1のフォントを指定します
        label1.Font = displayFont;

        //値を表示します
        label1.Text = "{123456789}";

    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }

        //label1のフォントを既定のフォントにしておく
        if (label1.Font == displayFont)
        {
            label1.Font = null;
        }

        //作成したオブジェクトの後始末をします。
        if (displayFont != null)
        {
            displayFont.Dispose();
            displayFont = null;
        }

        if (fontCollection != null)
        {
            fontCollection.Dispose();
            fontCollection = null;
        }            

        base.Dispose(disposing);
    }
}

関連図書

Filed under: C#,Programming,VB.NET — Nakai @ 16:57:03

コメントはまだありません »

コメントはまだありません。

この投稿へのコメントの RSS フィード。 TrackBack URL

コメントする

HTML convert time: 0.826 sec. Powered by WordPress