Tech — Category

May
10
2009

Using the standard Windows font in .Net

0

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!

Jan
7
2009

And then, Apple spoke

7:58pm · Tech · · ·
3
Jan
4
2009

Buying Music, Online

9:23pm · Tech
4
Dec
22
2008

WordPress upgrade

9:59am · My Blog
2
Oct
27
2008

We don’t need no stinking buttons

3:38pm · Tech · ·
3
Oct
8
2008

Games I’ve played recently

7:09pm · Gaming
0
Sep
4
2008

Blog upgrade and theme refresh

8:53pm · My Blog · ·
0
Sep
3
2008

Google Chrome brings the future - today!

8:04pm · Tech · · · ·
0
Aug
23
2008

Vodafone has a rethink

2:16pm · Tech · · ·
2
Jul
27
2008

Mesmerizing feast of eye candy

2:20pm · Tech · ·
0