如何在 WinForms 应用程序中嵌入我自己的字体? [英] How do I embed my own fonts in a WinForms app?

查看:24
本文介绍了如何在 WinForms 应用程序中嵌入我自己的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 WinForms 应用程序中嵌入字体,这样我就不必担心它们会安装在机器上.我在 MSDN 站点上进行了一些搜索,发现了一些有关使用本机 Windows API 调用的提示,例如由 Scott Hanselman 链接的 Michael Caplan (sp?) 教程.现在,我真的必须经历所有这些麻烦吗?我不能只使用我的应用程序的资源部分吗?

I want to embed fonts in my WinForms application so that I don't have to worry about them being installed on the machine. I've searched a bit on the MSDN site and found a few hints about using native Windows API calls, for instance Michael Caplan's (sp?) tutorial linked to by Scott Hanselman. Now, do I really have to go through all that trouble? Can't I just use the resource part of my app?

如果没有,我可能会走安装路线.在这种情况下,我可以以编程方式执行此操作吗?只需将字体文件复制到 WindowsFonts 文件夹?

If not I'll probably go the installing route. In that case, can I do that programmatically? By just copying the font file to the WindowsFonts folder?

我知道许可问题.

推荐答案

这是在 VS 2013 中对我有用的方法,无需使用不安全块.

This is what worked for me in VS 2013, without having to use an unsafe block.

嵌入资源

  1. 双击 Resources.resx,然后在设计器的工具栏中单击添加资源/添加现有文件并选择您的 .ttf 文件
  2. 在您的解决方案资源管理器中,右键单击您的 .ttf 文件(现在位于资源文件夹中)并转到属性.将构建操作设置为嵌入式资源"

将字体加载到内存中

  1. using System.Drawing.Text;添加到您的Form1.cs文件

  1. Add using System.Drawing.Text; to your Form1.cs file

在默认构造函数上方和内部添加代码以在内存中创建字体(不使用不安全",如其他示例所示).下面是我的整个 Form1.cs:

Add code above and inside your default constructor to create the font in memory (without using "unsafe" as other examples have shown). Below is my entire Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

using System.Drawing.Text;

namespace WindowsFormsApplication1
{
    public partial class Form1 : 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 myFont;

        public Form1()
        {
            InitializeComponent();

            byte[] fontData = Properties.Resources.MyFontName;
            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.MyFontName.Length);
            AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.MyFontName.Length, IntPtr.Zero, ref dummy);
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);

            myFont = new Font(fonts.Families[0], 16.0F);
        }
    }
}

使用你的字体

  1. 给你的主表单添加一个标签,并在Form1.cs中添加一个加载事件来设置字体:

  1. Add a label to your main form, and add a load event to set the font in Form1.cs:

private void Form1_Load(object sender, EventArgs e)
{
    label1.Font = myFont;
}

这篇关于如何在 WinForms 应用程序中嵌入我自己的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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