diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/GroupBox/GroupBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/GroupBox/GroupBox.cs index c4f606b8446..9a66e23b072 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/GroupBox/GroupBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/GroupBox/GroupBox.cs @@ -425,7 +425,7 @@ protected override void OnPaint(PaintEventArgs e) // specified by the theme. This is a temporary workaround till we find a good solution for the // "default theme color" issue. - if (ShouldSerializeForeColor() || Application.IsDarkModeEnabled || !Enabled) + if (ShouldSerializeForeColor() || Application.IsDarkModeEnabled || !Enabled || ForeColor != DefaultForeColor) { Color textColor = Enabled ? ForeColor : TextRenderer.DisabledTextColor(BackColor); diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/GroupBoxTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/GroupBoxTests.cs index 89012a3a518..04e997873af 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/GroupBoxTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/GroupBoxTests.cs @@ -1958,6 +1958,97 @@ public void GroupBox_OnPaint_VisualStyles_off_NullE_ThrowsNullReferenceException Assert.Throws(() => control.OnPaint(null)); } + [WinFormsFact] + public void GroupBox_OnPaint_VisualStyles_on_AmbientForeColor_FromForm_UsedWhenNotExplicitlySet() + { + if (!Application.RenderWithVisualStyles) + { + return; + } + + using Bitmap image = new(100, 100); + using Graphics graphics = Graphics.FromImage(image); + using PaintEventArgs eventArgs = new(graphics, Rectangle.Empty); + + using Form form = new() + { + ForeColor = Color.Red + }; + using SubGroupBox control = new() + { + Size = new Size(100, 100), + Text = "Test", + Parent = form + }; + + control.OnPaint(eventArgs); + + Assert.False(control.ShouldSerializeForeColor()); + Assert.Equal(Color.Red, control.ForeColor); + } + + [WinFormsFact] + public void GroupBox_OnPaint_VisualStyles_on_AmbientForeColor_FromIntermediatePanel_UsedWhenNotExplicitlySet() + { + if (!Application.RenderWithVisualStyles) + { + return; + } + + using Bitmap image = new(100, 100); + using Graphics graphics = Graphics.FromImage(image); + using PaintEventArgs eventArgs = new(graphics, Rectangle.Empty); + + using Form form = new() + { + ForeColor = SystemColors.ControlText + }; + using Panel panel = new() + { + ForeColor = Color.Lime, + Parent = form + }; + using SubGroupBox control = new() + { + Size = new Size(100, 100), + Text = "Test", + Parent = panel + }; + + control.OnPaint(eventArgs); + Assert.False(control.ShouldSerializeForeColor()); + Assert.Equal(Color.Lime, control.ForeColor); + } + + [WinFormsFact] + public void GroupBox_OnPaint_VisualStyles_on_ExplicitForeColor_TakesPrecedenceOverAmbient() + { + if (!Application.RenderWithVisualStyles) + { + return; + } + + using Bitmap image = new(100, 100); + using Graphics graphics = Graphics.FromImage(image); + using PaintEventArgs eventArgs = new(graphics, Rectangle.Empty); + + using Form form = new() + { + ForeColor = Color.Red + }; + using SubGroupBox control = new() + { + Size = new Size(100, 100), + Text = "Test", + ForeColor = Color.Blue, + Parent = form + }; + + control.OnPaint(eventArgs); + Assert.True(control.ShouldSerializeForeColor()); + Assert.Equal(Color.Blue, control.ForeColor); + } + public static IEnumerable ProcessMnemonic_TestData() { yield return new object[] { null, 'a', false };