在Winforms应用程序中嵌入多个truetype字体的正确方法是什么?

时间:2017-05-05 20:12:36

标签: c# winforms fonts

此代码来自最流行的答案:How to quickly and easily embed fonts in winforms app in C#

此代码工作正常,但我需要嵌入多个字体。这是我当前的代码:

   public partial class UnitInfoBox : Form
{

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
        IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);

    private PrivateFontCollection fonts = new PrivateFontCollection();

    Font Klablasto22, Amaltea14, BauderieScript18, Rudyard22;

    public UnitInfoBox()
    {
        InitializeComponent();

        byte[] fontData = Properties.Resources.klabasto;
        IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
        System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
        uint dummy = 0;
        fonts.AddMemoryFont(fontPtr, Properties.Resources.klabasto.Length);
        AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.klabasto.Length, IntPtr.Zero, ref dummy);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
        Klablasto22 = new Font(fonts.Families[0], 24.0F);

        fontData = Properties.Resources.amaltea;
        fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
        System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
        dummy = 0;
        fonts.AddMemoryFont(fontPtr, Properties.Resources.amaltea.Length);
        AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.amaltea.Length, IntPtr.Zero, ref dummy);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
        Amaltea14 = new Font(fonts.Families[0], 14.0F);

        fontData = Properties.Resources.BAUDS_;
        fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
        System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
        fonts.AddMemoryFont(fontPtr, Properties.Resources.BAUDS_.Length);
        AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.BAUDS_.Length, IntPtr.Zero, ref dummy);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
        BauderieScript18 = new Font(fonts.Families[1], 18.0F);

        fontData = Properties.Resources.rudyard;
        fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
        System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
        fonts.AddMemoryFont(fontPtr, Properties.Resources.rudyard.Length);
        AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.rudyard.Length, IntPtr.Zero, ref dummy);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
        Rudyard22 = new Font(fonts.Families[3], 22.0F);

请注意,第一种字体的分配方式如下:

            Klablasto22 = new Font(fonts.Families[0], 24.0F);

但是,第二种字体也是如此!

 Amaltea14 = new Font(fonts.Families[0], 14.0F);

逻辑上,这应该是:

Amaltea14 = new Font(fonts.Families[1], 14.0F);

但是,如果我这样做,它会拉入第一个字体!然后下一个字体分配如下:

            BauderieScript18 = new Font(fonts.Families[1], 18.0F);

下一个字体跳过两个:

            Rudyard22 = new Font(fonts.Families[3], 22.0F);

这是我为了加载正确的字体而必须做的事情。

我真的更愿意知道如何做到这一点'正确的方式'。有人知道发生了什么吗?谢谢!

0 个答案:

没有答案