在Essential Studio for WinForms应用程序中使用图标字体
admin
2023-02-21 01:40:08
0

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

相信每一位小伙伴都会经常都有这样的疑问:可以在桌面应用程序Essential Studio for Windows Forms中使用图标字体吗?答案是肯定的!本文就为你讲解具体的操作方法。

首先,请记住,在DPI的情况下,我们只需要改变字体大小,不需要为不同的DPI缩放维护不同大小的图像。

如何添加和绘制图标字体

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

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

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

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

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

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

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

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

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

public Form1()
{
    InitializeComponent();
    this.Paint += Form1_Paint;
}

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

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

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

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Paint += Form1_Paint;
    }

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

        //Icon font's unicode "0xe700" is converted to string and drawn using e.Graphics with WF Fabric set as font family. 
        string iconChar = char.ConvertFromUtf32(IconFont.LastArrow);
        Font iconFont = new Font(pfc.Families[0], 18.5f, FontStyle.Bold);
        e.Graphics.DrawString(iconChar, iconFont, new SolidBrush(Color.Orange), new PointF(10, 10));

        //Icon font's unicode "0xe710" is converted to string and drawn using e.Graphics with WF Fabric set as font family.
        iconChar = char.ConvertFromUtf32(IconFont.Plus);
        e.Graphics.DrawString(iconChar, iconFont, new SolidBrush(Color.Red), new PointF(40, 40));

        //Icon font's unicode "0xe720" is converted to string and drawn using e.Graphics with WF Fabric set as font family.
        iconChar = char.ConvertFromUtf32(IconFont.Paint);
        e.Graphics.DrawString(iconChar, iconFont, new SolidBrush(Color.Green), new PointF(70, 70));
    }
}

public static class IconFont
{
    //0xe700, 0xe710, 0xe720 - are icon font's unicode from the WF Fabric.ttf file.
    public static int LastArrow = 0xe700;
    public static int Plus = 0xe710;
    public static int Paint = 0xe720;
}

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

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

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

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

使用图标字体的优点是:

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

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

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

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


相关内容

热门资讯

刚刚,Claude Opus ... 昨晚,科技圈刚上演了一场开源 AI 大团建。英伟达创始人黄仁勋在社交媒体发表公开信,拉上 Meta、...
瑞松科技获得发明专利授权:“一... 证券之星消息,根据天眼查APP数据显示瑞松科技(688090)新获得一项发明专利授权,专利名为“一种...
中信科智联申请数据关联方法专利... 国家知识产权局信息显示,中信科智联科技有限公司申请一项名为“一种数据关联方法及相关装置”的专利,公开...
福州AI产业发展跑出“加速度” 7月17日至20日,2026世界人工智能大会暨人工智能全球治理高级别会议在上海举行,吸引了全球目光。...
乌民众再度走上街头:不恢复他防... 虽然乌军总司令瑟尔斯基被解除了职位,但是被解职的费多罗夫并未恢复防长职务,乌克兰民众7月24日再度走...
姗姗来迟!万斯晒新生儿照片 美国副总统万斯夫妇日前迎来第4个孩子,他们7月24日终于晒出了新生儿的照片。据美国有线电视新闻网(C...
洗衣机正常洗,水出不出去什么原... 这种情况原因有很多种,1、电机故障造成洗衣机不能脱水,洗衣机电动机有嗡嗡声响而不转动,多数是电动机匝...
半自动洗衣机排不出水是什么原因 1、排水管扭曲,保持排水管的畅通才能让洗衣机正常排水。2、排水缓慢时,有可能是因排水口过高,排水阀拉...
西门子洗衣机经常故障 西门子是全球著名的家电品牌,其洗衣机在国内市场也拥有广泛的用户群体。然而,近年来,一些西门子洗衣机用...
西门子洗衣机平衡自检故障 西门子洗衣机平衡自检故障是一种非常常见的问题,它通常是由于洗衣机内部出现平衡不良的问题导致的。当洗衣...