本文介绍了如何为自定义控件设置自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我通过覆盖控件创建了自定义控件。我已经实现了一些自定义来序列化和反序列化字体设置。但是,当我使用AddMemoryFont()方法从资源设置字体时,字体不会更新为控件。甚至我已经重写了Font属性和FontChanged事件并调用了基本函数。下面是我设置字体的代码部分,I have created a custom control by overriding from control. I have implemented some customization to serialize and deserialize the font settings. However, when I set the font from resources by using AddMemoryFont() method, the font is not getting updated to the control. Even I have overridden the Font property and FontChanged event and called the base functions. Below are my code part to set the font,Font font = InitializeResourceFont(Properties.Resources.Axel_Regular);public Font InitializeResourceFont(byte[] resourceFont) { byte[] fontData = resourceFont; IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length); Marshal.Copy(fontData, 0, fontPtr, fontData.Length); uint dummy = 0; fonts.AddMemoryFont(fontPtr, resourceFont.Length); AddFontMemResourceEx(fontPtr, (uint)resourceFont.Length, IntPtr.Zero, ref dummy); Marshal.FreeCoTaskMem(fontPtr); return new Font(fonts.Families[0], 12.00F); } 我是否缺少更新字体来自资源?如果是,请分享任何更新字体的建议。Is there something I'm missing to update the font from resources? If yes, share any suggestions to update the font. 示例链接: 简单样本 谢谢, ArulpriyaArulpriya ArulpriyaArulpriya推荐答案 为什么不定义 字体 直接为CustomControl输入type属性 CustomFontInfo ?Why don't you define aFont type attribute directly for CustomControl instead ofCustomFontInfo? CustomFontInfo.cs:CustomFontInfo.cs: public class CustomControl : Control { public CustomControl() { } private CustomFontInfo _fontInfo; public CustomFontInfo FontInfo { get { return _fontInfo; } set { _fontInfo = value; } } public Font Font { get; set; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //FontInfo = new CustomFontInfo(new Font("Calibri",10,FontStyle.Italic)); //var font = InitializeResourceFont(Properties.Resources.Axel_Regular); //FontInfo = new CustomFontInfo(font); //e.Graphics.DrawString("Hello World", FontInfo.GdipFont, Brushes.Red, 10, 10); e.Graphics.DrawString("Hello World", Font, Brushes.Red, 10, 10); } } Form1.cs: public partial class Form1 : Form { [DllImport("gdi32.dll")] private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts); private PrivateFontCollection fonts = new PrivateFontCollection(); private Font axelFont; public Form1() { InitializeComponent(); Font font = InitializeResourceFont(Properties.Resources.Axel_Regular); //this.customControl1.FontInfo = new CustomFontInfo(font);// new Font(font.FontFamily, 12f)); this.customControl1.Font = font; this.button1.Font = font;//new Font(font.FontFamily, 12f); } public Font InitializeResourceFont(byte[] resourceFont) { byte[] fontData = resourceFont; IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length); Marshal.Copy(fontData, 0, fontPtr, fontData.Length); uint dummy = 0; fonts.AddMemoryFont(fontPtr, resourceFont.Length); AddFontMemResourceEx(fontPtr, (uint)resourceFont.Length, IntPtr.Zero, ref dummy); Marshal.FreeCoTaskMem(fontPtr); return new Font(fonts.Families[0], 12.00F); } }问候, Frankie 这篇关于如何为自定义控件设置自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 阿里云证书,YYDS!
05-23 15:21