After a bit of reflection (reflection?), I think I can explain how it works.
Both FontFamily.GenericSansSerif
and FontFamily.GenericSerif
use an internal constructor that scans the default font in the system by the value of IntPtr
. In both cases, it passes IntPtr.Zero
, which effectively allows GDI + to make a choice (I decided not to go down this particular rabbit hole).
In principle, the FontFamily class is also sealed with pointers, so I would not try to override these properties. Instead, you can write your own method that mimics the fallback behavior that you see in CSS:
public FontFamily DefaultFont(params string[] fonts) { // Try to return the first matching font foreach (var font in fonts) { try { return new FontFamily(font); } catch (ArgumentException) { } } // Resort to system default return new FontFamily(new GenericFontFamilies()); }
source share