gpt4 book ai didi

c# - 在 C# windows.Form 中使用的自定义 .ttf 字体

转载 作者:可可西里 更新时间:2023-11-01 08:39:12 24 4
gpt4 key购买 nike

如何使用我当前的 windows.forms 应用程序中的自定义 .tff 字体文件?我读了一些将其用作嵌入式资源的地方,但如何将其设置为 System.Drawing.Font 类型?

最佳答案

本文:How to embed a true type font展示了如何在 .NET 中执行您所要求的操作。

如何嵌入 True Type 字体

Some applications, for reasons of esthetics or a required visualstyle, will embed certain uncommon fonts so that they are always therewhen needed regardless of whether the font is actually installed onthe destination system.

The secret to this is twofold. First the font needs to be placed inthe resources by adding it to the solution and marking it as anembedded resource. Secondly, at runtime, the font is loaded via astream and stored in a PrivateFontCollection object for later use.

This example uses a font which is unlikely to be installed on yoursystem. Alpha Dance is a free True Type font that is available fromthe Free Fonts Collection. This font was embedded into the applicationby adding it to the solution and selecting the "embedded resource"build action in the properties.

Figure 1. The Alpha Dance font file embedded in the solution.

Once the file has been successfully included in the resources you needto provide a PrivateFontCollection object in which to store it and amethod by which it's loaded into the collection. The best place to dothis is probably the form load override or event handler. Thefollowing listing shows the process. Note how the AddMemoryFont methodis used. It requires a pointer to the memory in which the font issaved as an array of bytes. In C# we can use the unsafe keyword forconvienience but VB must use the capabilities of the Marshal classesunmanaged memory handling. The latter option is of course open to C#programmers who just don't like the unsafe keyword.PrivateFontCollection pfc = new PrivateFontCollection();

private void Form1_Load(object sender, System.EventArgs e)
{
Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("embedfont.Alphd___.ttf");

byte[] fontdata = new byte[fontStream.Length];
fontStream.Read(fontdata,0,(int)fontStream.Length);
fontStream.Close();
unsafe
{
fixed(byte * pFontData = fontdata)
{
pfc.AddMemoryFont((System.IntPtr)pFontData,fontdata.Length);
}
}
}

Fonts may have only certain styles which are available andunfortunately, selecting a font style that doesn't exist will throw anexception. To overcome this the font can be interrogated to see whichstyles are available and only those provided by the font can be used.The following listing demonstrates how the Alpha Dance font is used bychecking the available font styles and showing all those that exist.Note that the underline and strikethrough styles are pseudo stylesconstructed by the font rendering engine and are not actually providedin glyph form.

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
bool bold=false;
bool regular=false;
bool italic=false;

e.Graphics.PageUnit=GraphicsUnit.Point;
SolidBrush b = new SolidBrush(Color.Black);

float y=5;

System.Drawing.Font fn;

foreach(FontFamily ff in pfc.Families)
{
if(ff.IsStyleAvailable(FontStyle.Regular))
{
regular=true;
fn=new Font(ff,18,FontStyle.Regular);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Bold))
{
bold=true;
fn=new Font(ff,18,FontStyle.Bold);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(ff.IsStyleAvailable(FontStyle.Italic))
{
italic=true;
fn=new Font(ff,18,FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
if(bold && italic)
{
fn=new Font(ff,18,FontStyle.Bold | FontStyle.Italic);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
}
fn=new Font(ff,18,FontStyle.Underline);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
y+=20;
fn=new Font(ff,18,FontStyle.Strikeout);
e.Graphics.DrawString(fn.Name,fn,b,5,y,StringFormat.GenericTypographic);
fn.Dispose();
}

b.Dispose();
}

Figure 2 shows the application in action.

Figure 2. The embedded Alpha Dance font.

参见Form1_Paint 事件处理程序,它具体说明了如何设置System.Drawing.Font 类型。它基于使用 System.Drawing.Text.PrivateFontCollection 类。

希望这对您有所帮助。

关于c# - 在 C# windows.Form 中使用的自定义 .ttf 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/544590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com