自定义字体 [英] Custom Font
问题描述
目前,我在标签中使用了"Monotype Corsiva,18pt,style = Italic"字体。如果用户没有这种字体,我如何将我的字体嵌入到我的应用程序中,所以不管他们是否有字体,我的字体都会正确地显示给每个人。
Currently I have a font in a label using "Monotype Corsiva, 18pt, style=Italic" If a user does not have this font, how do I embed my font into my app so regardless of rather or not they have the font, my font will display correctly for everyone.
推荐答案
Hello old_School,
Hello old_School,
正如Castorix31所说,你应该将字体嵌入为资源。我不确定你使用的是哪个程序。以下是winform中的步骤。
As Castorix31 said , you should embed the font as resource . I'm not sure which program you are using . The following is steps for example in winform.
1。双击Resources.resx>>选择资源选项卡>>单击添加资源/添加现有文件并选择.ttf文件。(系统ttf文件通常位于C:\ Windows \Fonts下)。
1.Double-click Resources.resx >> choose Resource tab >>click Add Resource/Add Existing File and select your .ttf file.(the system ttf file usually under C:\Windows\Fonts).
< img alt =""src ="https://social.msdn.microsoft.com/Forums/getfile/1128332">
2.right-click .ttf文件(现在在资源文件夹)并转到属性。将Build Action设置为"Embedded Resource"。
2.right-click .ttf file (now in a Resources folder) and go to Properties. Set the Build Action to "Embedded Resource".
3.添加如下代码。
public partial class Form2 : 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 Form2()
{
InitializeComponent();
byte[] fontData = Properties.Resources.MTCORSVA;
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.MTCORSVA.Length);
AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.MTCORSVA.Length, IntPtr.Zero, ref dummy);
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
// label using "Monotype Corsiva, 18pt, style=Italic"
myFont = new Font(fonts.Families[0],18.0F,FontStyle.Italic);
}
并使用你的字体。
private void Form1_Load(object sender, EventArgs e)
{
label1.Font = myFont;
}
结果如下。
此致,
feih_7
这篇关于自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!