Skip to content

Allow set font application-wide #3001

Closed

Description

@RussKie

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

  1. added
    api-suggestion
    on Mar 21, 2020
  2. added this to the 5.0 milestone on Mar 27, 2020
  3. added
    api-ready-for-review
    on Mar 27, 2020
  4. merriemcgaw commented on Mar 27, 2020

    @merriemcgaw
    Contributor

    We should totally do this for .NET 5, I think this is a great idea.

  5. terrajobst commented on Mar 27, 2020

    @terrajobst
    Contributor
    • This method must be "run only once" kind, i.e. a user may not be allowed to invoke it once an app has started.

    Are you going to throw an exception when the SetDefaultFont is called after Run? If so, I'd throw InvalidOperationException.

  6. RussKie commented on Mar 27, 2020

    @RussKie
    ContributorAuthor
    • This method must be "run only once" kind, i.e. a user may not be allowed to invoke it once an app has started.

    Are you going to throw an exception when the SetDefaultFont is called after Run? If so, I'd throw InvalidOperationException.

    I propose we follow the same pattern set by SetCompatibleTextRenderingDefault() method and throw if SetDefaultFont() is called after the application has started:

    public static void SetCompatibleTextRenderingDefault(bool defaultValue)
    {
    if (NativeWindow.AnyHandleCreated)
    {
    throw new InvalidOperationException(SR.Win32WindowAlreadyCreated);
    }
    Control.UseCompatibleTextRenderingDefault = defaultValue;
    }

    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.

  7. bartonjs commented on Mar 27, 2020

    @bartonjs
    Member

    Is there already a way to get the default font? (Get/set symmetry)

  8. weltkante commented on Mar 27, 2020

    @weltkante
    Contributor

    probably Control.DefaultFont ?

  9. terrajobst commented on Mar 27, 2020

    @terrajobst
    Contributor

    Might be worthwhile to add a corresponding GetDefaultFont() to Application so that folks can just tweak parts, like:

    [STAThread]
    static void Main()
    {
        Application.SetHighDpiMode(HighDpiMode.SystemAware);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
    
        // Set default to 10pt
        var font = Application.GetDefaultFont();
        font = new Font(font.FontFamily, 10f);
        Application.SetDefaultFont(font);
    
        Application.Run(new Form1());
    }

    It would be odd if users had to call Control.DefaultFont to do this.

  10. weltkante commented on Mar 27, 2020

    @weltkante
  11. terrajobst commented on Mar 27, 2020

    @terrajobst
  12. RussKie commented on Mar 30, 2020

    @RussKie
    ContributorAuthor

    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 FontFamily and FontSize explicitly, 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?

  13. RussKie commented on Apr 17, 2020

    @RussKie
    Author
  14. terrajobst commented on Apr 17, 2020

    @terrajobst
    Contributor

    No objections, you're the domain expert. We just generally strive to avoid asymmetries like set-only APIs.

  15. 31 remaining items

  16. RussKie commented on May 16, 2021

    @RussKie
    ContributorAuthor

    With Windows 10 21H2 changing the System font to Segoe UI Variable with its Small, Text, and Display OpticalSize variants - will WinForms be ready to handle that, and will there be suitable fallbacks for Windows 8, and older Windows 10 versions?

    Thanks for the link. We get the default font from .NET runtime, and take it for granted:

    public static Font DefaultFont
    {
    get
    {
    if (s_defaultFont is null)
    {
    s_defaultFont = SystemFonts.MessageBoxFont;
    Debug.Assert(s_defaultFont is not null, "defaultFont wasn't set!");
    }
    return s_defaultFont;
    }
    }

    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?

  17. RussKie commented on May 16, 2021

    @RussKie
    Author
  18. kirsan31 commented on May 16, 2021

    @kirsan31
  19. ghost removed
    🚧 work in progress
    on May 18, 2021
  20. added 2 commits that reference this issue on May 18, 2021
    9d476b6
    8ba5135
  21. Zheng-Li01 commented on May 27, 2021

    @Zheng-Li01
    Member

    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

  22. imba-tjd commented on Jun 19, 2021

    @imba-tjd

    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.

  23. weltkante commented on Jun 19, 2021

    @weltkante
    Contributor

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

  24. ghost locked as resolved and limited conversation to collaborators on Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedapi-suggestion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions