From 28eeb7822314b891e45a67521fa508bbaf510a76 Mon Sep 17 00:00:00 2001 From: JayashreeSF3546 Date: Tue, 30 Jun 2026 15:16:19 +0530 Subject: [PATCH 1/2] 3583 System font scaling not working on the fly --- .../System/Windows/Forms/Control.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/System/Windows/Forms/Control.cs index 14efc5014ff..1a2f60c67f0 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Control.cs @@ -12261,6 +12261,36 @@ private unsafe void WmWindowPosChanged(ref Message m) } } + /// + /// Called when the system default font has changed (e.g. via Accessibility → Text Size in Windows Settings). + /// Notifies this control and all descendants that use the default font so that they update their font handle + /// and raise , which in turn causes to invoke + /// and resize controls immediately without an app restart. + /// Controls that have an explicitly set font are intentionally skipped. + /// + private void OnSystemFontChanged() + { + // Only notify controls that are using the ambient/default font — i.e., no explicit font was set. + if (!IsFontSet()) + { + // Discard the per-control cached font handle so it is recreated from the updated default font. + DisposeFontHandle(); + + // Raising OnFontChanged will clear _currentAutoScaleDimensions in ContainerControl and trigger + // PerformAutoScale, which recalculates AutoScaleFactor and resizes all child controls. + OnFontChanged(EventArgs.Empty); + } + + // Propagate to all immediate children regardless; each child decides for itself whether to act. + if (ChildControls is { } children) + { + for (int i = 0; i < children.Count; i++) + { + children[i].OnSystemFontChanged(); + } + } + } + /// /// Processes Windows messages. /// @@ -12626,6 +12656,7 @@ protected virtual void WndProc(ref Message m) // s_defaultFont = Application.DefaultFont ?? SystemFonts.MessageBoxFont; // So we need to check both variants - manually set font (Application.DefaultFont is not null) and auto system font + bool fontChanged = false; if (Application.DefaultFont is null) // auto system font { if (s_defaultFont is not null) // we need to check only if s_defaultFont already set @@ -12634,6 +12665,7 @@ protected virtual void WndProc(ref Message m) if (!s_defaultFont.Equals(font)) // the font has changed { s_defaultFont = font; + fontChanged = true; } else { @@ -12643,8 +12675,22 @@ protected virtual void WndProc(ref Message m) } else // manually set font { + Font? oldFont = s_defaultFont; Application.ScaleDefaultFont(); // will update Application.s_defaultFont or Application.s_defaultFontScaled only if needed s_defaultFont = Application.DefaultFont; // Application.s_defaultFontScaled ?? Application.s_defaultFont + fontChanged = oldFont != s_defaultFont; + } + + // When the system default font changes and controls are using it, notify them so that + // AutoScaleMode.Font can immediately re-scale controls and update their sizes without + // requiring an application restart. + if (fontChanged) + { + // Invalidate the cached default font handle so it gets recreated from the new font. + s_defaultFontHandleWrapper?.Dispose(); + s_defaultFontHandleWrapper = null; + + OnSystemFontChanged(); } } } From cf7b839bc3e831502131ee683e2422cd566417f9 Mon Sep 17 00:00:00 2001 From: JayashreeSF3546 Date: Fri, 10 Jul 2026 11:31:58 +0530 Subject: [PATCH 2/2] Review changes --- .../System/Windows/Forms/Control.cs | 47 ++++--------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Control.cs b/src/System.Windows.Forms/System/Windows/Forms/Control.cs index 1a2f60c67f0..1d3144d518a 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Control.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Control.cs @@ -12261,36 +12261,6 @@ private unsafe void WmWindowPosChanged(ref Message m) } } - /// - /// Called when the system default font has changed (e.g. via Accessibility → Text Size in Windows Settings). - /// Notifies this control and all descendants that use the default font so that they update their font handle - /// and raise , which in turn causes to invoke - /// and resize controls immediately without an app restart. - /// Controls that have an explicitly set font are intentionally skipped. - /// - private void OnSystemFontChanged() - { - // Only notify controls that are using the ambient/default font — i.e., no explicit font was set. - if (!IsFontSet()) - { - // Discard the per-control cached font handle so it is recreated from the updated default font. - DisposeFontHandle(); - - // Raising OnFontChanged will clear _currentAutoScaleDimensions in ContainerControl and trigger - // PerformAutoScale, which recalculates AutoScaleFactor and resizes all child controls. - OnFontChanged(EventArgs.Empty); - } - - // Propagate to all immediate children regardless; each child decides for itself whether to act. - if (ChildControls is { } children) - { - for (int i = 0; i < children.Count; i++) - { - children[i].OnSystemFontChanged(); - } - } - } - /// /// Processes Windows messages. /// @@ -12656,7 +12626,7 @@ protected virtual void WndProc(ref Message m) // s_defaultFont = Application.DefaultFont ?? SystemFonts.MessageBoxFont; // So we need to check both variants - manually set font (Application.DefaultFont is not null) and auto system font - bool fontChanged = false; + bool defaultFontChanged = false; if (Application.DefaultFont is null) // auto system font { if (s_defaultFont is not null) // we need to check only if s_defaultFont already set @@ -12665,7 +12635,7 @@ protected virtual void WndProc(ref Message m) if (!s_defaultFont.Equals(font)) // the font has changed { s_defaultFont = font; - fontChanged = true; + defaultFontChanged = true; } else { @@ -12675,22 +12645,25 @@ protected virtual void WndProc(ref Message m) } else // manually set font { - Font? oldFont = s_defaultFont; + Font? previousDefaultFont = s_defaultFont; Application.ScaleDefaultFont(); // will update Application.s_defaultFont or Application.s_defaultFontScaled only if needed s_defaultFont = Application.DefaultFont; // Application.s_defaultFontScaled ?? Application.s_defaultFont - fontChanged = oldFont != s_defaultFont; + defaultFontChanged = !s_defaultFont.Equals(previousDefaultFont); } // When the system default font changes and controls are using it, notify them so that // AutoScaleMode.Font can immediately re-scale controls and update their sizes without // requiring an application restart. - if (fontChanged) + if (defaultFontChanged) { - // Invalidate the cached default font handle so it gets recreated from the new font. s_defaultFontHandleWrapper?.Dispose(); s_defaultFontHandleWrapper = null; + } - OnSystemFontChanged(); + if (defaultFontChanged && !TryGetExplicitlySetFont(out _)) + { + DisposeFontHandle(); + OnFontChanged(EventArgs.Empty); } } }