首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在WinForms应用程序中嵌入我自己的字体?

如何在WinForms应用程序中嵌入我自己的字体?
EN

Stack Overflow用户
提问于 2009-02-17 09:59:41
回答 5查看 42.2K关注 0票数 38

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

如果不是,我可能会选择安装路线。在这种情况下,我可以通过编程来实现吗?只需将字体文件复制到Windows\Fonts文件夹?

我知道许可问题。

EN

回答 5

Stack Overflow用户

发布于 2014-05-07 21:40:59

这就是我在VS2013中的工作,而不需要使用不安全的代码块。

嵌入资源

  1. 双击Resources.resx,在设计器的工具栏中单击“添加资源”/“添加现有文件”,在解决方案资源管理器中选择.ttf文件
  2. ,右键单击.ttf文件(现在位于“资源”文件夹中),然后转到“属性”。将生成操作设置为"Embedded Resource"

将字体加载到内存

  1. using System.Drawing.Text;添加到Form1.cs文件中
  2. 在默认构造函数的上面和内部添加代码,以便在内存中创建字体(不像其他示例那样使用"unsafe“)。下面是我的整个Form1.cs:

使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Data;使用System.Linq;使用System.Text;使用System.Threading.Tasks;使用System.Windows.Forms;使用System.Reflection;使用System.Drawing.Text;命名空间WindowsFormsApplication1 { public partial Form1 : Form {public private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont,uint cbFont,IntPtr pdv,ref uint );私有虚拟字体= System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);System.Runtime.InteropServices.Marshal.Copy(fontData,();PrivateFontCollection PrivateFontCollection();myFont Form1() { InitializeComponent();byte[] fontData = Properties.Resources.MyFontName;虚拟字体= IntPtr fontPtr 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);fonts.AddMemoryFont myFont =新字体(fonts.Families,16.0F);}

使用您的字体

  1. 将标签添加到主窗体,并添加load事件以设置Form1.cs中的字体:

private void Form1_Load(对象发送者,EventArgs e) { label1.Font = myFont;}

票数 58
EN

Stack Overflow用户

发布于 2011-05-26 17:29:40

代码语言:javascript
运行
复制
// specify embedded resource name
string resource = "embedded_font.PAGAP___.TTF";

// receive resource stream
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);

// create an unsafe memory block for the font data
System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);

// create a buffer to read in to
byte[] fontdata = new byte[fontStream.Length];

// read the font data from the resource
fontStream.Read(fontdata, 0, (int)fontStream.Length);

// copy the bytes to the unsafe memory block
Marshal.Copy(fontdata, 0, data, (int)fontStream.Length);

// pass the font to the font collection
private_fonts.AddMemoryFont(data, (int)fontStream.Length);

// close the resource stream
fontStream.Close();

// free up the unsafe memory
Marshal.FreeCoTaskMem(data);
票数 8
EN

Stack Overflow用户

发布于 2014-07-21 20:12:12

我将一个randomly downloaded font拖放到我的资源中,这起作用了。但是使用了一个文件。我猜你也可以用它来安装字体吧?

代码语言:javascript
运行
复制
public Form1()
{
    string filename = @"C:\lady.gaga";
    File.WriteAllBytes(filename, Resources.KBREINDEERGAMES);
    PrivateFontCollection pfc = new PrivateFontCollection();
    pfc.AddFontFile(filename);

    Label label = new Label();
    label.AutoSize = true;
    label.Font = new Font(pfc.Families[0], 16);
    label.Text = "hello world";
    Controls.Add(label);
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/556147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档