自定义.ttf字体在C#windows.Form中使用 [英] Custom .ttf fonts to use in C# windows.Form
问题描述
如何使用我当前的windows.forms应用程序中的自定义.tff字体文件?
我读了一些我在哪里使用它作为嵌入式资源,但是如何将它设置为System.Drawing.Font类型呢?
这篇文章:
一旦文件已经成功地包含在资源中,您需要
来提供一个PrivateFontCollection对象来存储它,还有一个
方法将它加载到集合中。做
的最好的地方可能是表单加载重载或事件处理程序。下面的
列出了这个过程。请注意如何使用AddMemoryFont方法
。它需要一个指向内存的字符
作为字节数组保存的指针。在C#中,我们可以使用不安全的关键字
convienience,但VB必须使用Marshal类
非托管内存处理的功能。后面的选项当然是开放给C#
的程序员,他们不喜欢不安全的关键字。
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);
lockquote
字体可能只有某些可用的样式,
不幸的是,选择不存在的字体样式将抛出
异常。为了克服这个问题,可以询问字体,看看哪些
样式可用,只有字体提供的样式才可以使用。
以下列表演示了如何使用Alpha Dance字体
检查可用的字体样式并显示所有存在的字体样式。
请注意,下划线和删除线样式是由字体呈现引擎构造的伪样式
,实际上并没有以字形的形式提供
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 =新的SolidBrush(Color.Black);
float y = 5;
System.Drawing.Font fn;
foreach(pfc.Families中的FontFamily ff)
{
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();
$ b
图2显示了应用程序的运行情况。 / p>
请参阅Form1_Paint事件处理程序,它具体显示如何设置System.Drawing.Font类型。它基于使用System.Drawing.Text.PrivateFontCollection类。
希望这有助于。
How do I use a custom .tff font file I have with my current windows.forms application?
I read some where that I use it as an embedded resource, but how do I set it the System.Drawing.Font type?
解决方案 This article: How to embed a true type font shows how to do what you ask in .NET.
How to embed a True Type font
Some applications, for reasons of esthetics or a required visual
style, will embed certain uncommon fonts so that they are always there
when needed regardless of whether the font is actually installed on
the destination system.
The secret to this is twofold. First the font needs to be placed in
the resources by adding it to the solution and marking it as an
embedded resource. Secondly, at runtime, the font is loaded via a
stream and stored in a PrivateFontCollection object for later use.
This example uses a font which is unlikely to be installed on your
system. Alpha Dance is a free True Type font that is available from
the Free Fonts Collection. This font was embedded into the application
by adding it to the solution and selecting the "embedded resource"
build action in the properties.
Once the file has been successfully included in the resources you need
to provide a PrivateFontCollection object in which to store it and a
method by which it's loaded into the collection. The best place to do
this is probably the form load override or event handler. The
following listing shows the process. Note how the AddMemoryFont method
is used. It requires a pointer to the memory in which the font is
saved as an array of bytes. In C# we can use the unsafe keyword for
convienience but VB must use the capabilities of the Marshal classes
unmanaged 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 and
unfortunately, selecting a font style that doesn't exist will throw an
exception. To overcome this the font can be interrogated to see which
styles are available and only those provided by the font can be used.
The following listing demonstrates how the Alpha Dance font is used by
checking the available font styles and showing all those that exist.
Note that the underline and strikethrough styles are pseudo styles
constructed by the font rendering engine and are not actually provided
in 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.
See the Form1_Paint event handler, it shows specifically how to set the System.Drawing.Font type. It is based on using the System.Drawing.Text.PrivateFontCollection class.
Hope this helps.
这篇关于自定义.ttf字体在C#windows.Form中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!