Repository navigation
Description
Is your feature request related to a problem? Please describe.
The default font has been updated in .NET Core 3.0 (#656) and documented. However for some users there is still an element of surprise when they migrate their apps to .NET Core.
We've received several questions regarding different sizes of forms (e.g. #1122, #1827, etc.).
Whilst the new default font is here to stay, some users may wish to retain the original font (e.g. due to a design of their app). However for an application with more than a handful of forms, setting the original font may be tedious and cumbersome exercise.
Describe the solution you'd like
Add the ability to set an application-wide font, similar to SetHighDpiMode() or SetCompatibleTextRenderingDefault() methods.
- This method must be "run only once" kind, i.e. a user may not be allowed to invoke it once an app has started.
- Any form that doesn't explicitly specify its own font, must inherit the application default font.
API Proposal
namespace System.Windows.Forms
{
public partial class Application
{
public void SetDefaultFont(Font font);
}
}API Usage
class Program
{
[STAThread]
static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8f));
Application.Run(new Form1());
}
}Will this feature affect UI controls?
No
Activity
merriemcgaw commentedon Mar 27, 2020
We should totally do this for .NET 5, I think this is a great idea.
terrajobst commentedon Mar 27, 2020
Are you going to throw an exception when the
SetDefaultFontis called afterRun? If so, I'd throwInvalidOperationException.RussKie commentedon Mar 27, 2020
I propose we follow the same pattern set by
SetCompatibleTextRenderingDefault()method and throw ifSetDefaultFont()is called after the application has started:winforms/src/System.Windows.Forms/src/System/Windows/Forms/Application.cs
Lines 1208 to 1215 in 18f1c81
The rationale behind it, if a form has been created, it may have been laid out and rendered using one font, the font may have been cached etc., and setting a new font could be detrimental from both performance and UX.
bartonjs commentedon Mar 27, 2020
Is there already a way to get the default font? (Get/set symmetry)
weltkante commentedon Mar 27, 2020
probably
Control.DefaultFont?terrajobst commentedon Mar 27, 2020
Might be worthwhile to add a corresponding
GetDefaultFont()toApplicationso that folks can just tweak parts, like:It would be odd if users had to call
Control.DefaultFontto do this.weltkante commentedon Mar 27, 2020
terrajobst commentedon Mar 27, 2020
RussKie commentedon Mar 30, 2020
The rationale behind this FR is to allow users to set the original font, that may have been used to design the app (i.e. Font-scaling pixel-perfect design scenario), to help with a migration from .NET Framework to .NET Core/.NET.
To this end, users are expected to set both
FontFamilyandFontSizeexplicitly, and not leverage the current default font.I don't feel that leveraging the default font to change its properties fits the above scenario. And, to be honest, I'm struggling to think of a use-case that would require this.
Do you have a use-case that would necessitate this?
RussKie commentedon Apr 17, 2020
terrajobst commentedon Apr 17, 2020
No objections, you're the domain expert. We just generally strive to avoid asymmetries like set-only APIs.
31 remaining items
RussKie commentedon May 16, 2021
Thanks for the link. We get the default font from .NET runtime, and take it for granted:
winforms/src/System.Windows.Forms/src/System/Windows/Forms/Control.cs
Lines 1807 to 1819 in c93d8c8
If you're running that build already you could probably run a test yourself and see:
https://github.com/dotnet/runtime/blob/64303750a9198a49f596bcc3aa13de804e421579/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs#L85-L99
I think it may just work out of the box... @safern any thoughts?
RussKie commentedon May 16, 2021
kirsan31 commentedon May 16, 2021
Allow set application-wide default font (#4911)
Allow set application-wide default font (#4911)
Zheng-Li01 commentedon May 27, 2021
Verified the issue with latest 6.0.0-preview.5.21275.7, the main functions have been fully merged/implemented, found a new issue too and filed it as #4978
imba-tjd commentedon Jun 19, 2021
Question: Can WPF set default font? rather than set FontFamily in every xaml. I had googled it but got no answer.
Edit:
I managed to set it by
typeof(SystemFonts).GetField("_messageFontFamily", BindingFlags.Static | BindingFlags.NonPublic).SetValue(null, new System.Windows.Media.FontFamily("xxx"));It's the first time I monkey patch in C#, interesting. Previously I only did this kind of things in Python.
weltkante commentedon Jun 19, 2021
@imba-tjd you can try setting the default value of the FontFamily and related properties in the metadata before creating any window or control. In general this is the wrong repo to ask in though, you'll find more people knowing about WPF in the WPF repo.