43

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?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
wasker
  • 1,959
  • 2
  • 19
  • 32
  • 2
    This is a very old question but I wonder if the answers still apply today. Let's say one works on a WinForms application on Windows 10;. Has the default font issue been fixed or not? – dpant Feb 22 '20 at 08:34

11 Answers11

50

The accepted answer doesn't really answer the question; it just explains why this behavior is occurring.

Some of the other answers propose solid workarounds, but I've found that the best solution really is to create a base form that all of the forms in your application inherit from and set this base form's Font property to SystemFonts.MessageBoxFont in the constructor. This not only ensures that your application picks up the correct font at run-time, based on the user's environment (heading off the potential problem posed by Hans Passant—an XP without Office 2007 will resort to Microsoft Sans Serif in the absence of Segoe UI), but also gives you design-time support for your current Windows font. Using the correct font at design time solves the problem Josuegomes points out, because any container control that is created on the form will pick up the font used by the form at design-time.

Besides the above advantages, this frees you from having to remember to modify the constructor for each form that you create and ensures consistency across all of the forms in your application, as well as giving you a place to put other common functionality. I use this in a couple of different ways such as p/invoking, etc. to fix bugs in the WinForms implementation.

The only problem that remains with this approach is if you want to set a font style for a particular control, such as bold. The best place to do this is still in that form's constructor, starting with the form's font as a base and modifying the style from it:

myControl.Font = New Font(Me.Font, FontStyle.Bold)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
36

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

this.Font = SystemFonts.MessageBoxFont;

This appear to work with Windows XP and Windows Vista.

CS.
  • 1,845
  • 1
  • 19
  • 38
15

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).

Robert Wagner
  • 17,515
  • 9
  • 56
  • 72
13

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.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • "MS Sans Serif" is not the same as "Microsoft Sans Serif". The former is an old bitmap font, the latter still sees widespread use in Win10 (control panels, file properties, basically all dialog-type windows of built-in apps, like [non-main-window] dialogs in regedit.exe or gpedit.msc). DEFAULT_GUI_FONT is "MS Shell Dlg" at 8pt specifically. And "MS Shell Dlg" is "Microsoft Sans Serif" on WinXP through Win10, but not universally. It's a different font on Chinese or Japanese systems for example. And it's widespread because native dialog resources default to it, including in Windows own programs. – dialer Feb 23 '21 at 16:51
6

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.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Olia Gavrysh
  • 61
  • 1
  • 1
3

The controls inside the group box are indeed not affected by the form's Font property. The reason is that controls in container controls are treated as children of the container controls like groupbox, but not children of the main form. In order for all controls including those in groupboxes to scale properly, you can use code similar to below:

        foreach (Control ctr in this.Controls)
        {
            ctr.Font = SystemFonts.IconTitleFont;

            // controls in groupboxes are not child of main form
            if (ctr.HasChildren)
            {
                foreach (Control childControl in ctr.Controls)
                {
                    childControl.Font = SystemFonts.IconTitleFont;
                }
            }        
        }
David
  • 31
  • 1
1

Setting the form's Font property to SystemFonts.DialogFont doesn't work if you have group boxes with associated controls. The controls inside the group box are not affected by the form's Font property. I "solved" this by setting the Font property to SystemFonts.DialogFont for each and every group box.

josuegomes
  • 451
  • 4
  • 15
1

Try this, Click a Form and change font size for example I changed a font size of Form to 12pt and then test by drag text box to the Form. You'll see, The textbox size is changed to 12pt as well. I've just got this solution by accident.

P D
  • 11
  • 1
  • did you noticed that a question was asked in 2008? It is quite late answer – m.cekiera Jul 08 '15 at 18:50
  • 2
    Yes, I see, but in 2015 I still need the answer of this question and I can solved this problem by accident as I told. So, I think I should told this solution for others people who will probably have the same problem like me – P D Jul 10 '15 at 00:04
1

The Control.DefaultFont is ReadOnly; one hacky was to overwrite it is to use reflection.

Type settingsType = typeof(Control);
var defaultFontField = settingsType.GetField("defaultFont", BindingFlags.Static | BindingFlags.NonPublic);
defaultFontField.SetValue(null, new Font("Segoe UI", 8.25F));

Be sure to have a UT keeping an eye on this code, there is no API contract to protect you if the Framework implementation changes.

Also be aware of forms designer which most of the time will insert the font verbatim in .designer classes.

Adrian Rus
  • 329
  • 1
  • 4
  • 9
  • This won't work properly with Remote Desktop in some cases. If the server and the client have a different default font (e.g. 8.25 pt Microsoft Sans Serif on English Windows vs. 9 pt SimSun on Chinese Windows), then starting or ending an RDP session will cause many controls, including Label, to start rendering with the real default font. When this happens the Font property will continue to return your overridden value. – Billy Dec 11 '19 at 00:01
0

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.

Kirsan
  • 254
  • 3
  • 5
0

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.