【问题标题】:How can I bundle a font with my .net winforms application?如何将字体与我的 .net winforms 应用程序捆绑在一起?
【发布时间】:2010-02-18 11:36:39
【问题描述】:

我想为我的 .net 3.0 Winforms 应用程序使用非标准字体。

此字体可能安装在我用户的某些计算机上,但在其他一些计算机上显然会丢失。

如何将字体与我的程序一起发送?我需要安装字体吗?如果是这样,缺乏管理员权限是否会成为问题?

【问题讨论】:

  • 您的程序是如何“交付”的?压缩文件?安装程序?两者都有?
  • 我们支持这两种方法,具体取决于客户的喜好。话虽如此,我们可以很容易地在客户端启动时直接执行字体注册代码(当然它不需要管理权限)

标签: .net winforms fonts


【解决方案1】:

您必须使用安装程序来获取在目标机器上注册的字体。但也许你不必这样做,GDI+ 支持private fonts

【讨论】:

    【解决方案2】:

    这是我写的一篇博客文章,展示了一种将字体作为资源嵌入到应用程序中的方法(无需导入 dll :)。

    Embedding Fonts in your .Net Application

    这是我创建的所有魔法发生的类。博客文章包含说明和使用示例。

    using System.Drawing;
    using System.Drawing.Text;
    using System.Runtime.InteropServices;
    namespace EmbeddedFontsExample.Fonts
    {
        public class ResFonts
        {
            private static PrivateFontCollection sFonts;
            static ResFonts()
            {
                sFonts = new PrivateFontCollection();
                // The order the fonts are added to the collection 
                // should be the same as the order they are added
                // to the ResFontFamily enum.
                AddFont(MyFonts.Consolas);
            }
            private static void AddFont(byte[] font)
            {
                var buffer = Marshal.AllocCoTaskMem(font.Length);
                Marshal.Copy(font, 0, buffer, font.Length);
                sFonts.AddMemoryFont(buffer, font.Length);
            }
            public static Font Create(
                ResFontFamily family, 
                float emSize, 
                FontStyle style = FontStyle.Regular, 
                GraphicsUnit unit = GraphicsUnit.Pixel)
            {
                var fam = sFonts.Families[(int)family];
                return new Font(fam, emSize, style, unit);
            }
        }
        public enum ResFontFamily
        {
            /// <summary>Consolas</summary>
            Consolas = 0
        }
    }
    

    【讨论】:

    • 假设您已正常将字体添加为资源,我唯一要更改的是 AddFont(MyFonts.Consolas) 应该是 AddFont(Resources.Consolas)
    • 链接失效了!
    • 当然是:)。它现在在 BizArk。该类名为 FontUtil,这是它的 wiki 页面:github.com/BizArk/BizArk3/wiki/BizArk.Core.Util.FontUtil 注意,v3 仍处于测试阶段,但似乎相当稳定。
    【解决方案3】:

    This page详细解释了如何将字体嵌入到winforms项目中。

    【讨论】:

    • 链接失效
    猜你喜欢
    • 1970-01-01
    • 2013-03-16
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2017-02-27
    • 2015-07-09
    相关资源
    最近更新 更多