如何快速,轻松地嵌入在WinForms应用程序的字体在C# [英] How to quickly and easily embed fonts in winforms app in C#
问题描述
这可能是小白领土,但到底是什么:安装
This is probably noob territory but what the heck:
我要嵌入字体在我的WinForms应用程序,这样我就不必担心他们在机器上。我搜索在MSDN网站了一下,发现有关使用本地的Windows API调用一些提示,比如迈克尔Caplans(藻?)教程由Scott Hanselman的链接。现在,我真的必须去克服这些困难?不能我只是用我的应用程序的资源部分?
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 Caplans (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?
如果不是我可能会去安装路径。在这种情况下,我能做到这一点编程?只需复制字体文件到文件夹Windows\Fonts
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 Windows\Fonts folder?
编辑:?我知道的许可问题,感谢尽管关注
I am aware of licencing issues, thanks for the concern though.
推荐答案
这是我在VS 2013什么工作,而不必使用不安全的块。
This is what worked for me in VS 2013, without having to use an unsafe block.
嵌入资源
- 双击Resources.resx和工具栏为设计师单击添加资源/添加现有文件,并选择您的.ttf文件
- 在Solution Explorer中,(在资源文件夹现在)右击.TTF文件,并转到属性。设置生成操作为嵌入的资源
加载字体到内存
-
添加
使用System.Drawing.Text;
您Form1.cs的文件
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);
}
}
}
使用您的字体
-
标签添加到您的主要形式,并添加加载事件设置在Form1.cs的字体:
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应用程序的字体在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!