博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[.NET源码学习]实例化Font,遭遇字体不存在的情况。
阅读量:4448 次
发布时间:2019-06-07

本文共 2701 字,大约阅读时间需要 9 分钟。

  实例化Font类时,当传入参数为不存在未安装的字体时,Windows系统会用Microsoft Sans Serif字体替代该字体。

  Msdn:

    "For more information about how to construct fonts, see . Windows Forms applications support TrueType fonts and have limited support for OpenType fonts. If you attempt to use a font that is not supported, or the font is not installed on the machine that is running the application, the Microsoft Sans Serif font will be substituted."

  

  情景

    利用 New Font("字体",.....) ,当该字体不存在时,我本以为会用系统默认的输入法代替之,后来查看了MSDN,发现事实似乎并非如此。决定查找一下源代码。

  

  源码:  

   System.Drawing.Font类

1 private void Initialize(string familyName, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)2 {3     this.originalFontName = familyName;4     this.SetFontFamily(new FontFamily(Font.StripVerticalName(familyName), true));5     this.Initialize(this.fontFamily, emSize, style, unit, gdiCharSet, gdiVerticalFont);6 }

     System.Drawing.FontFamily类

1 internal FontFamily(string name, bool createDefaultOnFail)2 {3     this.createDefaultOnFail = createDefaultOnFail;4     this.CreateFontFamily(name, null);5 }
1 private void CreateFontFamily(string name, FontCollection fontCollection) 2 { 3     IntPtr intPtr = IntPtr.Zero; 4     IntPtr handle = (fontCollection == null) ? IntPtr.Zero : fontCollection.nativeFontCollection; 5     int num = SafeNativeMethods.Gdip.GdipCreateFontFamilyFromName(name, new HandleRef(fontCollection, handle), out intPtr); 6     if (num != 0) 7     { 8         if (this.createDefaultOnFail) 9         {10             intPtr = FontFamily.GetGdipGenericSansSerif();11         }12         else13         {14             if (num == 14)15             {16                 throw new ArgumentException(SR.GetString("GdiplusFontFamilyNotFound", new object[]17                 {18                     name19                 }));20             }21             if (num == 16)22             {23                 throw new ArgumentException(SR.GetString("GdiplusNotTrueTypeFont", new object[]24                 {25                     name26                 }));27             }28             throw SafeNativeMethods.Gdip.StatusException(num);29         }30     }31     this.SetNativeFamily(intPtr);32 }
1 private static IntPtr GetGdipGenericSansSerif() 2 { 3     IntPtr zero = IntPtr.Zero; 4     int num = SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif(out zero); 5     if (num != 0) 6     { 7         throw SafeNativeMethods.Gdip.StatusException(num); 8     } 9     return zero;10 }
结论: 【SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif】方法,Windows系统下返回Microsoft Sans Serif字体。 由此可见,当实例化字体不存在时,Windows下固定返回Microsoft Sans Serif字体,与系统默认字体无关。
    

 

转载于:https://www.cnblogs.com/huiyin/p/3896552.html

你可能感兴趣的文章
arm裸机驱动错误总结
查看>>
C# 程序性能提升篇-1、装箱和拆箱,枚举的ToString浅析
查看>>
lfs(systemv版本)学习笔记-第3页
查看>>
Postman-简单使用(1)
查看>>
[BJOI2014] 大融合
查看>>
最简单的一个java驱动jdbc链接mysql数据库
查看>>
Laravel 加载第三方类库的方法
查看>>
Binary Tree Level Order Traversal
查看>>
wcf契约版本处理与异常处理(随记)
查看>>
hlg1201Zombie’s Treasure Chest---数学问题
查看>>
1.基础数据类型的初识 字符串 bool 整型 if else elif
查看>>
Mybatis源码分析: MapperMethod功能讲解(1)
查看>>
Error: Cannot retrieve repository metadata (repomd.xml) for repository: addons.
查看>>
jqGrid的subGrid子表格
查看>>
插件库
查看>>
桶排序
查看>>
the least common multiplier
查看>>
Metro 风格的浏览和无插件的 HTML5
查看>>
LifecycleControl.cs
查看>>
函数式思维: 利用 Either 和 Option 进行函数式错误处理 类型安全的函数式异常...
查看>>