简体   繁体   English

如何将字体与.net winforms应用程序捆绑在一起?

[英]How can I bundle a font with my .net winforms application?

I'd like to use a non-standard font for my .net 3.0 Winforms application. 我想为.net 3.0 Winforms应用程序使用非标准字体。

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? 如果是这样,缺少管理员特权会成为问题吗?

You'll have to use an installer to get the font registered on the target machine. 您必须使用安装程序来在目标计算机上注册字体。 But maybe you won't have to, GDI+ supports private fonts . 但也许您不必,GDI +支持专用字体

Here's a blog article I wrote that shows a way to embed fonts as resources in your application (no dll imports necessary :). 这是我写的一篇博客文章,展示了一种将字体作为资源嵌入应用程序中的方法(无需dll导入:)。

Embedding Fonts in your .Net Application 在.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
    }
}

本页详细说明了如何将字体嵌入到winforms项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将VC ++ 2015可再发行的软件包与ClickOnce(.NET)应用程序捆绑在一起? - How can I bundle the VC++ 2015 redistributable with my ClickOnce (.NET) application? 如何将.NET控制台应用程序转换为Winforms或WPF应用程序 - How do I convert a .NET console application to a Winforms or WPF application 如何在WinForms应用程序中使用DOS字体 - How to use DOS font in WinForms application 当焦点对准WinForms应用程序时,如何捕获该键? - How can I capture keys on a WinForms application when the application is in focus? .NET WinForms应用程序中非常奇怪的字体显示问题 - Very strange font display issue in .NET WinForms application 如何让Winforms表单终止应用程序? - How can I make a Winforms form terminate the application? 如何在“paint”winforms 应用程序中将图形 object 保存到 bitmap? - How can I save a graphics object to a bitmap in a "paint" winforms application? 如何为 winforms 应用程序创建桌面快捷方式? - How can I create a desktop shortcut for a winforms application? 给定一个窗口,如何确定它是否属于winforms应用程序? - Given a window, how can I determine if it is part of a winforms application? 如何在Linux上更改WinForms应用程序的WM_CLASS? - How can I change WM_CLASS for a WinForms application on Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM