I was working on XBList the other night when I realized something - the font used in its dialogs and the friends list wasn’t Segoe UI. Segoe UI is the very pretty, ClearType-optimized new default dialog font in Windows Vista. In Windows XP and 2000, it’s Tahoma, and in earlier editions it was Microsoft Sans Serif. You can see the subtle differences between them:
In .NET and Windows Forms, the default font for controls is actually Microsoft Sans Serif, not the operating system’s default dialog font! Kevin Dente explains this on his blog. This is not the only time Microsoft’s dropped the ball on this - if you go through some dialogs in Vista you’ll see that many of them use Tahoma or even Microsoft Sans Serif instead of Segoe UI. This is pretty funny, especially when Rule #1 of the Top Rules for the Windows Vista User Experience is “Use the Aero Theme and System Font (Segoe UI)”. Mitch Kaplan offers up a pretty good explanation for why getting it all right is very hard, but having a mix of old and new fonts still looks shoddy.
I don’t want my apps to look shoddy. Embarrassingly enough, I’ve been hardcoding Tahoma in all my apps to get the more “modern” XP look. Now that Vista’s on the scene, it’s clear that I want to select the correct default font for whichever OS my app is running on. As Kevin points out,
Control.DefaultFont is no help here - it’s what’s driving Windows Forms’ default choice of Microsoft Sans Serif in the first place. After some digging I found this Visual Studio feedback ticket (sign in required), where the Visual Studio guys explain that, while they couldn’t fix the default, they did create a SystemFonts class to help out. They recommend putting this code in your Form’s constructor:
this.Font = SystemFonts.DialogFont;
Unfortunately, this doesn’t work in a number of ways. The first is that on Vista,
SystemFonts.DialogFont is… Tahoma! Closer, but not quite right yet. If you pop open SystemFonts in Reflector, you’ll see that the DialogFont property just does some simple platform-detection, and then just hardcodes Tahoma. This worked when it was just 2000/XP vs. 9x, but Vista throws it for a total loop. Fortunately the fix is easy - use
SystemFonts.MessageBoxFont instead. This one seems to always return the correct default dialog font.
However, I ran into one more problem. If I set the default font on the Form, like the code above does, I get weird, bloated controls:
Fortunately I’ve got a solution for that one too. Instead of setting the font on the Form and letting it inherit, just loop through the
Controls property, and individually set the right font on each control:
// Set the default dialog font on each child control
foreach (Control c in Controls)
c.Font = SystemFonts.MessageBoxFont;
// Use a larger, bold version of the default dialog font for one control
this.label1.Font = new Font(SystemFonts.MessageBoxFont.Name, 12f, FontStyle.Bold, GraphicsUnit.Point);
Now I get a more familiar-looking dialog:
I could always make a subclass of Form to do this for me, but I’m OK with copying it into each new form. With this code, all my controls come up with the pretty new Segoe UI font in Windows Vista, and Tahoma in XP.