经过大量的挖掘,并对此一无所知(包括对这个问题的驾车投票,这仍然让我感到困惑),我找到了解决这个问题的方法,似乎很多人都面临过并且也遇到过空手而归,仅将字体作为文件安装在用户系统上。正如我所想,这根本不是必需的,您可以以简单的方式嵌入字体,使其可以用于任何控件,包括 TextBox、ComboBox 等。无论您想要什么。 p>
我将把它写成一步一步的指南,介绍如何将字体嵌入到您的项目中,并让它在您想要的任何控件上正确显示。
第 1 步 - 选择受害者(字体选择)
出于演示目的(和大众需求),我将使用 FontAwesome 字体(在撰写本文时为 4.1 版)
- 在您的项目中,转到
Project > <your project name> Properties
- 点击
Resources(应该在左侧)
- 点击
Add Resource按钮右侧的下拉箭头,然后选择Add Existing File。
- 确保从下拉列表中选择
All Files (*.*),然后浏览到要嵌入字体文件的位置,选择它,然后单击[打开] 按钮。
您现在应该看到如下内容:
第 2 步 - 深入研究代码
复制以下代码并将其保存为“MemoryFonts.cs”,然后将其添加到您的项目中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Drawing.Text;
using System.Drawing;
public static class MemoryFonts {
[DllImport( "gdi32.dll" )]
private static extern IntPtr AddFontMemResourceEx( IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts );
private static PrivateFontCollection pfc { get; set; }
static MemoryFonts() {
if ( pfc==null ) { pfc=new PrivateFontCollection(); }
}
public static void AddMemoryFont(byte[] fontResource) {
IntPtr p;
uint c = 0;
p=Marshal.AllocCoTaskMem( fontResource.Length );
Marshal.Copy( fontResource, 0, p, fontResource.Length );
AddFontMemResourceEx( p, (uint)fontResource.Length, IntPtr.Zero, ref c );
pfc.AddMemoryFont( p, fontResource.Length );
Marshal.FreeCoTaskMem( p );
p = IntPtr.Zero;
}
public static Font GetFont( int fontIndex, float fontSize = 20, FontStyle fontStyle = FontStyle.Regular ) {
return new Font(pfc.Families[fontIndex], fontSize, fontStyle);
}
// Useful method for passing a 4 digit hex string to return the unicode character
// Some fonts like FontAwesome require this conversion in order to access the characters
public static string UnicodeToChar( string hex ) {
int code=int.Parse( hex, System.Globalization.NumberStyles.HexNumber );
string unicodeString=char.ConvertFromUtf32( code );
return unicodeString;
}
}
第 3 步 - 让它漂亮(使用字体)
在主窗体上,从您的工具箱中添加一个 TextBox 控件
-
在form_Load 事件中,输入以下代码(在我的情况下,资源是fontawesome_webfont。将其更改为您的字体资源名称)
private void Form1_Load( object sender, EventArgs e ) {
MemoryFonts.AddMemoryFont( Properties.Resources.fontawesome_webfont );
textBox1.Font = MemoryFonts.GetFont(
// using 0 since this is the first font in the collection
0,
// this is the size of the font
20,
// the font style if any. Bold / Italic / etc
FontStyle.Regular
);
// since I am using FontAwesome, I would like to display one of the icons
// the icon I chose is the Automobile (fa-automobile). Looking up the unicode
// value using the cheat sheet https://fortawesome.github.io/Font-Awesome/cheatsheet/
// shows : fa-automobile (alias) []
// so I pass 'f1b9' to my UnicodeToChar method which returns the Automobile icon
textBox1.Text = MemoryFonts.UnicodeToChar( "f1b9" );
}
最终结果
您可能注意到,也可能没有注意到我使用此方法时请记住,您可能希望添加多种嵌入字体。在这种情况下,您只需为要添加的每个字体调用AddMemoryFont(),然后在使用GetFont()时使用适当的索引值(从零开始)
与微软关于PrivateFontCollection.AddMemoryFont() 的文档相反,您根本不需要使用UseCompatibleTextRendering 或SetCompatibleTextRenderingDefault。我上面概述的方法允许在对象/控件中自然呈现字体。