【问题标题】:How can i use ttf font in winform designer without installation?如何在没有安装的情况下在 winform Designer 中使用 ttf 字体?
【发布时间】:2026-02-07 02:35:01
【问题描述】:

我想在 winform 设计器中使用自定义 ttf 字体。 我试图搜索,但只有以编程方式添加。 但是程序化的方式并没有体现在设计师身上。 wpf 项目可以使用 blend for visual studio 来做到这一点,但 winform 无法设置我用设计师添加项目的 ttf 字体。 我该怎么办?

【问题讨论】:

  • 如果没有安装,设计者怎么知道它的存在才能使用呢?设计师从哪里来使用它?使用一些常识。您的代码可以在您的应用程序启动时加载它,这使其可用于您的应用程序。如果没有安装,设计师从哪里加载它?它怎么知道它的存在?
  • 我不知道这是否有帮助。您应该在发布之前先搜索。 *.com/questions/1297264/…
  • @Ken White:我写这个项目的时候有一个字体。但是我在发布项目时发现了字体问题。所以我删除了我使用这个项目的 ttf 字体。
  • @chopperfield 我已经使用了 PrivateFontCollection。但是ttf字体在设计器中没有显示。
  • 那么您的问题是我如何在设计器中使用我删除的字体? 现在?这是没有意义的。您不能使用设计器中不存在的字体。我不确定你为什么不理解。如果设计者不知道字体,就不能使用该字体。如果你删了,不仅设计师不能用,你的app也不能用了。

标签: c# winforms


【解决方案1】:

试试这个

// 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);

How to quickly and easily embed fonts in winforms app in C#

【讨论】:

    最近更新 更多