Default font for Windows Forms application

Viewed 57223

Every time that I create a new form in my application, it uses the "Microsoft Sans Serif, 8.25pt" font by default. I'm not changing it because I know that in this case my form should pick up whatever the default font is for the system. However, when I run my application, the font that is used is still anything but Segoe UI (my default system font in my Windows Vista OS).

Why does this happen? How do I make sure that my application looks like a normal Windows application?

11 Answers

You can add before InitializeComponent() in the Form constructor(s):

this.Font = SystemFonts.MessageBoxFont;

This appear to work with Windows XP and Windows Vista.

Check out this blog entry talking about the default font in Forms which leads to the problem you are experiencing and this Connect Bug with Microsoft's response. In short it just seems that Forms does not get the (correct) default windows font (which you have changed).

Yes, it uses the font returned by GetStockObject(DEFAULT_GUI_FONT). Which is MS Sans Serif. An old font, long gone from most machines. The font mapper translate it to, no surprise, Microsoft Sans Serif.

There is no documented procedure I know of to change that default font, the SDK docs mention MS Sans Serif explicitly. If you want Segoe, you'll have to ask for it. Which isn't that safe to do, there are still a lot of XP machines out there without Office 2007. The font mapper will translate it on a machine that doesn't have Segoe available. Not sure what pops out, I don't have such a machine left anymore.

In .NET Core we (.NET team) updated the default font to Segoe UI and in .NET 6 we have added an API to change your default font all over your application.

In project file inside <PropertyGroup> add:

<ApplicationDefaultFont>Curlz MT, 18pt</ApplicationDefaultFont>

This will update every control that is using the default font (everything that was not explicitly overwritten by you) in the designer and your running application.

You can also call Application.SetDefaultFont() directly in you Main() like in the example below:

static void Main()
{
    ApplicationConfiguration.Initialize();

    Application.SetDefaultFont(new Font(new FontFamily("Curlz MT"), 18f));

    Application.Run(new AirQualityForm());
}

this will update the font in your running application but not in the designer.

I've tried sample app that targets both net472/net48 and netcoreapp3.1. While .net app Control.DefaultFont always returns Microsoft Sans Serif and not scaled. But .net core 3.1 app Control.DefaultFont returns exactly the system font on win7/10 and scaled well. So, I think they fixed this in core at last.

I have a very easy suggestion for this one. Just select the form and choose your required font from the properties panel. And viola, every control you make will have your selected font as the default font.

Related