经过大量的挖掘,并且对此一无所知(包括对这个问题的驾车投票,这仍然让我感到困惑),我找到了解决这个问题的方法,似乎很多人都面临过这个问题并且也空无一物 -只能求助于将字体作为文件安装在用户系统上。正如我所想,这根本不是必需的,您可以以一种简单的方式嵌入字体,使其可以用于任何控件,包括 TextBox、ComboBox 等。无论您想要什么。
我将把它写成一步一步的指南,介绍如何将字体嵌入到你的项目中,并让它在你想要的任何控件上正确显示。
第 1 步 - 选择你的受害者(字体选择)
出于演示目的(和大众需求),我将使用 FontAwesome 字体(在撰写本文时为 v 4.1)
- 在您的项目中,转到
Project > <your project name> Properties
- 点击
Resources
(应该在左侧)
- 单击按钮右侧的下拉箭头
Add Resource
,然后选择Add Existing File
。
![在此处输入图像描述](https://i.stack.imgur.com/p39E1.png)
- 确保
All Files (*.*)
从下拉列表中选择 ,然后浏览到要嵌入字体文件的位置,选择它,然后单击 [打开] 按钮。
![在此处输入图像描述](https://i.stack.imgur.com/v7u1x.png)
您现在应该看到如下内容:
![在此处输入图像描述](https://i.stack.imgur.com/c3Ie5.png)
第 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 控件
![在此处输入图像描述](https://i.stack.imgur.com/P4pkd.png)
在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" );
}
最终结果
![在此处输入图像描述](https://i.stack.imgur.com/CZL3r.png)
您可能注意到也可能没有注意到我使用这种方法,记住您可能希望添加多个嵌入字体。在这种情况下,您只需调用AddMemoryFont()
要添加的每种字体,然后在使用时使用适当的索引值(从零开始)GetFont()
与 Microsoft 提供的有关PrivateFontCollection.AddMemoryFont()的文档相反,您不需要使用UseCompatibleTextRendering
或SetCompatibleTextRenderingDefault
根本不需要使用。我上面概述的方法允许在对象/控件中自然呈现字体。