Perhaps one of the subtle differences between Windows Vista and Windows XP is the new font that Microsoft decided to use for the user interface. Tahoma 8.25pt has been used since Windows 2000 (but was available before then) and it has been replaced with Segoe UI 9pt, which is a much nicer looking font, with its tighter and rounder letters, but larger spacing. The bigger point size also helps increase the readability.
But how do you deal with different user interface fonts in .Net? Especially when the default for Windows Forms is the horrible MS Sans Serif (why?). Well, one method is to use the Environment class to detect which version of Windows the user is using and set the font,
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version.Major >= 6) {
// Vista+
this.Font = new Font("Segoe UI", 8.0f);
} else if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version.Major < 6) {
// XP and below
this.Font = new Font("Tahoma", 8.0f);
} else ... (Unix/Mac code if you want to go that far)
But what if the user has custom fonts selected, or is using the Mono framework, or Microsoft decide to change the default font again in the future? The fonts are probably going to be messed up.
Fortunately, .Net has the SystemFonts class, which is a small collection of the default fonts used by the system. But, the names aren’t actually what is advertised. DefaultFont is not actually the Windows default font, but the .Net default font… MS Sans Serif, ahhh!
The only one that seems to return the real system default font is MessageBoxFont. So, we can turn that whole if statement into…
this.Font = SystemFonts.MessageBoxFont;
You can also use this font when dealing with larger sizes…
HeadingLabel.Font = new Font(SystemFonts.MessageBoxFont.FontFamily, 15f, FontStyle.Regular);
Pretty fonts for all occasion, that even follow the users preferences. Yay!
Get Updates
Search