如何在Essential Studio for WinForms应用程序中使用图标字体?

在WinFrom控件按钮中添加图标字体,其实没那么难,很简单!

图标字体包含符号而不是数字和字母。在Web技术中,与其他图像格式相比,图标字体占主导地位。由于它们是矢量图形,体积小、易于装载,因此用户可以在不降低质量的情况下上下缩放。但唯一的限制就是单个图标只能用一种颜色绘制。

相信每一位小伙伴都会经常都有这样的疑问:可以在桌面应用程序

如何添加和绘制图标字体

系统中通常可用的字体(如Arial、Times New Roman)可能没有我们需要在应用程序中使用的图标,但有许多支持创建图标字体的在线和离线应用程序。Syncfusion就免费提供了一个名为Metro Studio的离线工具。

为了演示,我们创建了一个.ttf文件,其中包含一个名为“WF Fabric”的字体系列。结果图标如下图所示。

Icon-Font-WF-Fabric.ttf-file.png

△ 来自WF Fabric.ttf文件的图标字体

*注意:上图中显示的Unicode(e700,e701等)表示在应用程序中绘制时的相应字形。


在WinForms应用程序中包含WF Fabric.ttf文件,并在属性对话框中将其Build Action标记为Embedded Resource。

WF-Fabric.ttf-file-marked-as-embedded-resource.png

△ WF Fabric.ttf文件标记为嵌入式资源

在表单初始化期间,包括在系统内存中注册图标字体的代码。与其他字体(Arial、Times New Roman等)一样,WF Fabric也将在控制面板所有控制面板项字体的系统内存中注册。

public Form1()<font></font>{<font></font>    InitializeComponent();<font></font>    this.Paint += Form1_Paint;<font></font>}<font></font><font></font>private void Form1_Paint(object sender, PaintEventArgs e)<font></font>{<font></font>    PrivateFontCollection pfc = new PrivateFontCollection();<font></font>    //Extracting icon fonts from the WF Fabric.ttf file and adding into system memory.<font></font>    Stream fontAsStream = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApp1.WF Fabric.ttf");<font></font>    byte[] fontAsByte = new byte[fontAsStream.Length];<font></font>    fontAsStream.Read(fontAsByte, 0, (int)fontAsStream.Length);<font></font>    fontAsStream.Close();<font></font>    IntPtr memPointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(typeof(byte)) * fontAsByte.Length);<font></font>    System.Runtime.InteropServices.Marshal.Copy(fontAsByte, 0, memPointer, fontAsByte.Length);<font></font>    pfc.AddMemoryFont(memPointer, fontAsByte.Length);<font></font>}<font></font>

着至关重要的作用。pfc对象中的Families属性将保存已保存的字体系列名称。如上所述,我们创建了WF Fabric.ttf,其字体系列名称为WF Fabric。

现在创建一个枚举,其中包含具有相应名称的所有图标字体,并为其Unicode指定前缀0x。因此,无论您使用图标字体绘制何处,Unicode都将转换为字符串并作为参数传递给DrawString方法。

public partial class Form1 : Form<font></font>{<font></font>    public Form1()<font></font>    {<font></font>        InitializeComponent();<font></font>        this.Paint += Form1_Paint;<font></font>    }<font></font><font></font>    private void Form1_Paint(object sender, PaintEventArgs e)<font></font>    {<font></font>        PrivateFontCollection pfc = new PrivateFontCollection();<font></font>        //Extracting icon fonts from the WF Fabric.ttf file and inserting into system memory.<font></font>        Stream fontAsStream = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApp1.WF Fabric.ttf");<font></font>        byte[] fontAsByte = new byte[fontAsStream.Length];<font></font>        fontAsStream.Read(fontAsByte, 0, (int)fontAsStream.Length);<font></font>        fontAsStream.Close();<font></font>        IntPtr memPointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(typeof(byte)) * fontAsByte.Length);<font></font>        System.Runtime.InteropServices.Marshal.Copy(fontAsByte, 0, memPointer, fontAsByte.Length);<font></font>        pfc.AddMemoryFont(memPointer, fontAsByte.Length);<font></font><font></font>        //Icon font's unicode "0xe700" is converted to string and drawn using e.Graphics with WF Fabric set as font family. <font></font>        string iconChar = char.ConvertFromUtf32(IconFont.LastArrow);<font></font>        Font iconFont = new Font(pfc.Families[0], 18.5f, FontStyle.Bold);<font></font>        e.Graphics.DrawString(iconChar, iconFont, new SolidBrush(Color.Orange), new PointF(10, 10));<font></font><font></font>        //Icon font's unicode "0xe710" is converted to string and drawn using e.Graphics with WF Fabric set as font family.<font></font>        iconChar = char.ConvertFromUtf32(IconFont.Plus);<font></font>        e.Graphics.DrawString(iconChar, iconFont, new SolidBrush(Color.Red), new PointF(40, 40));<font></font><font></font>        //Icon font's unicode "0xe720" is converted to string and drawn using e.Graphics with WF Fabric set as font family.<font></font>        iconChar = char.ConvertFromUtf32(IconFont.Paint);<font></font>        e.Graphics.DrawString(iconChar, iconFont, new SolidBrush(Color.Green), new PointF(70, 70));<font></font>    }<font></font>}<font></font><font></font>public static class IconFont<font></font>{<font></font>    //0xe700, 0xe710, 0xe720 - are icon font's unicode from the WF Fabric.ttf file.<font></font>    public static int LastArrow = 0xe700;<font></font>    public static int Plus = 0xe710;<font></font>    public static int Paint = 0xe720;<font></font>}<font></font>

现在是在实际场景中应用图标字体的时候了。我们准备了一个简单的演示,它显示了在自定义按钮控件上绘制的第一页、最后一页、上一页和下一页的导航图标。

参考示例:使用图标字体在WinForms按钮控件上呈现图标

Custom-button-control-with-icon-font-and-text-drawn-inside.png

△ 自定义按钮控制与图标字体和文本绘制在里面

使用图标字体的优点是:

  1. 改进了所有字体大小的渲染质量;

  2. 通过改变颜色为不同的主题和皮肤重用单个图标字体;

  3. 通过不必相对于DPI缩放维持不同的图像大小来减小应用程序大小;

  4. 添加多个图标字体以及用字母、数字和符号连接图标字体。



想要了解Essential Studio for Windows Forms更多信息或资源的朋友,请点这里

想要购买Essential Studio for Windows Forms正版授权的朋友,请点这里。

1565166511625_12201A8E-ADC2-4723-A4C7-A25BEBC0575B(1).png

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年7月5日
下一篇 2019年7月5日

相关推荐

发表回复

登录后才能评论