我怎样才能捆绑的字体与我的.NET WinForms应用程序? [英] How can I bundle a font with my .net winforms application?

查看:169
本文介绍了我怎样才能捆绑的字体与我的.NET WinForms应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用非标准字体为我的。NET 3.0 WinForms应用程序。

I'd like to use a non-standard font for my .net 3.0 Winforms application.

这个字体可能会安装在我的一些用户的计算机,但它显然缺少对一些人。

This font might be installed on some of my user's computer, but it will clearly be missing on some others.

我怎么能随我的程序的字体?我是否需要安装字体?如果是这样,就是缺乏管理员权限将是一个问题?

How can I ship the font with my program ? Do I need to install the font ? If so, is the lack of administrator privileges going to be an issue?

推荐答案

下面是一个博客我写的文章,显示了一种嵌入字体作为资源在应用程序中(无DLL进口必要的:)。

Here's a blog article I wrote that shows a way to embed fonts as resources in your application (no dll imports necessary :).

在.NET应用程序中嵌入字体

下面是我创建,所有的魔术发生的类。博客文章包括指令和使用它的一个例子。

Here's the class I create where all the magic happens. The blog article includes instructions and an example of using it.

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
    }
}

这篇关于我怎样才能捆绑的字体与我的.NET WinForms应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆