In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Icon fonts contain symbols instead of numbers and letters. In Web technology, compared with other image formats, icon fonts play a dominant role. Because they are vector graphics, small and easy to load, you can scale up and down without reducing the quality. But the only limitation is that a single icon can only be drawn in one color.
I believe that every little friend will often have the question: can you use icon fonts in the desktop application Essential Studio for Windows Forms? The answer is yes! This article will explain the specific operation methods for you.
First of all, remember that in the case of DPI, we only need to change the font size, and we don't need to maintain different sizes of images for different DPI zooms.
How to add and draw icon fonts
The fonts commonly available in the system (such as Arial, Times New Roman) may not have the icons we need to use in applications, but there are many online and offline applications that support the creation of icon fonts. Syncfusion provides an offline tool called Metro Studio for free.
For demonstration purposes, we created a .ttf file that contains a font family called "WF Fabric". The resulting icon is shown in the following figure.
△ icon fonts from WF Fabric.ttf files
* Note: the Unicode shown in the above figure (e700.e701, etc.) represents the corresponding glyph when drawn in the application.
Include the WF Fabric.ttf file in the WinForms application and mark its Build Action as Embedded Resource in the properties dialog box.
△ WF Fabric.ttf files are marked as embedded resources
Includes code to register icon fonts in system memory during form initialization. Like other fonts (Arial, Times New Roman, etc.), WF Fabric will be registered in the system memory of the control panel\ all control panel items\ fonts.
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 (typeof (byte)) * fontAsByte.Length); System.Runtime.InteropServices.Marshal.Copy (fontAsByte, 0, memPointer, fontAsByte.Length) Pfc.AddMemoryFont (memPointer, fontAsByte.Length);}
Plays a vital role. The Families property in the pfc object saves the saved font family name. As mentioned above, we created WF Fabric.ttf, whose font family name is WF Fabric. Download the latest version of Essential Studio for Windows Forms
Now create an enumeration that contains all icon fonts with corresponding names and specify the prefix 0x for their Unicode. Therefore, no matter where you draw with the icon font, Unicode is converted to a string and passed as an argument to the DrawString method.
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, (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;}
It is time to apply icon fonts in real-world scenarios. We have prepared a simple demonstration that shows the navigation icons for the first, last, previous, and next pages drawn on the custom button control.
Reference example: rendering icons on WinForms button controls using icon fonts
△ custom button control with icon fonts and text drawn inside
The advantages of using icon fonts are:
Improved rendering quality for all font sizes
Reuse a single icon font by changing colors for different themes and skins
Reduce the application size by not having to maintain different image sizes relative to DPI scaling
Add multiple icon fonts and connect icon fonts with letters, numbers, and symbols.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.