[.NET源码学习]实例化Font,遭遇字体不存在的情况。

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

  Msdn:

    "For more information about how to construct fonts, see How to: Construct Font Families and FontsWindows 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         else
13         {
14             if (num == 14)
15             {
16                 throw new ArgumentException(SR.GetString("GdiplusFontFamilyNotFound", new object[]
17                 {
18                     name
19                 }));
20             }
21             if (num == 16)
22             {
23                 throw new ArgumentException(SR.GetString("GdiplusNotTrueTypeFont", new object[]
24                 {
25                     name
26                 }));
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值