[. NET source learning] to instantiate a font, encountering a situation where fonts do not exist.

Source: Internet
Author: User

  When you instantiate a font class, the Windows system replaces the font with a Microsoft Sans serif font when the passed-in parameter is a nonexistent or not installed typeface.

  Msdn:

" How to: construct Font families and fonts. ">for more information on how to construct fonts, see  how to:construct Font families and Fonts. windows Forms applications support TrueType fonts and has limited support for OpenType Fonts. if you attempt to use a font which is not supported, or the" the "and" the font is not installed on the machine that's running the APPL Ication, the Microsoft Sans Serif font would be substituted.

  

  Scenario :

Using the New font ("Font",.....), when the font doesn't exist, I thought I'd replace it with the system's default input method, and then I looked at MSDN and found that it didn't seem to be the case. Decide to look up the source code.

  

  Source :

System.Drawing.Font class

1 Private voidInitialize (stringFamilyname,floatEmSize, FontStyle style, GraphicsUnit unit,bytegdiCharSet,BOOLGdiverticalfont)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 class

1 Internal FontFamily (stringbool  createdefaultonfail)2{3      this. Createdefaultonfail = createdefaultonfail; 4     this. createfontfamily NULL ); 5 }
1 Private voidCreatefontfamily (stringname, fontcollection fontcollection)2 {3IntPtr IntPtr =IntPtr.Zero;4INTPTR handle = (Fontcollection = =NULL) ?IntPtr.Zero:fontCollection.nativeFontCollection;5     intnum = SafeNativeMethods.Gdip.GdipCreateFontFamilyFromName (name,NewHandleRef (fontcollection, handle), outintPtr);6     if(num! =0)7     {8         if( This. Createdefaultonfail)9         {TenINTPTR =FontFamily. Getgdipgenericsansserif (); One         } A         Else -         { -             if(num = = -) the             { -                 Throw NewArgumentException (SR. GetString ("Gdiplusfontfamilynotfound",New Object[] -                 { - name +                 })); -             } +             if(num = = -) A             { at                 Throw NewArgumentException (SR. GetString ("Gdiplusnottruetypefont",New Object[] -                 { - name -                 })); -             } -             ThrowSafeNativeMethods.Gdip.StatusException (num); in         } -     } to      This. Setnativefamily (INTPTR); +}
1 Private StaticIntPtr Getgdipgenericsansserif ()2 {3IntPtr zero =IntPtr.Zero;4     intnum = Safenativemethods.gdip.Gdipgetgenericfontfamilysansserif( outzero);5     if(num! =0)6     {7         ThrowSafeNativeMethods.Gdip.StatusException (num);8     }9     returnZero;Ten}
Conclusion:
"SafeNativeMethods.Gdip.GdipGetGenericFontFamilySansSerif" method, the Microsoft Sans Serif font is returned under Windows system.

This shows that when the instantiated font does not exist, Windows pinned back to the Microsoft Sans Serif font, regardless of the system default font.
        

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.