diff --git a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs index cd5315b5a44..342ff10af83 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs @@ -49,6 +49,7 @@ public partial class PrintPreviewControl : Control private double _zoom = DefaultZoom; private bool _pageInfoCalcPending; private bool _exceptionPrinting; + private bool _isForeColorSet; /// /// Initializes a new instance of the class. @@ -57,6 +58,7 @@ public PrintPreviewControl() { ResetBackColor(); ResetForeColor(); + ForeColorChanged += (_, _) => _isForeColorSet = true; Size = new Size(100, 100); SetStyle(ControlStyles.ResizeRedraw, false); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true); @@ -295,9 +297,13 @@ public override string Text internal override bool ShouldSerializeBackColor() => !BackColor.Equals(SystemColors.AppWorkspace); [EditorBrowsable(EditorBrowsableState.Never)] - public override void ResetForeColor() => ForeColor = Color.White; + public override void ResetForeColor() + { + ForeColor = SystemColors.ControlText; + _isForeColorSet = false; + } - internal override bool ShouldSerializeForeColor() => !ForeColor.Equals(Color.White); + internal override bool ShouldSerializeForeColor() => _isForeColorSet; internal override bool SupportsUiaProviders => true; @@ -592,7 +598,7 @@ private void CalculatePageInfo() private void DrawMessage(Graphics g, Rectangle rect, bool isExceptionPrinting) { - Color brushColor = ShouldSerializeForeColor() ? ForeColor : SystemColors.ControlText; + Color brushColor = _isForeColorSet ? ForeColor : SystemColors.ControlText; if (SystemInformation.HighContrast && Parent is Control parent) { brushColor = parent.BackColor; diff --git a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs index ab5c1f19a24..eaaf19a51fc 100644 --- a/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs +++ b/src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs @@ -24,6 +24,50 @@ public void ShowPrintPreviewControl_BackColorIsCorrect() Assert.Equal(Color.Green.ToArgb(), actualBackColorArgb); } + [Fact] + public void PrintPreviewControl_ForeColor_DefaultIsControlText() + { + PrintPreviewControl control = new(); + + Assert.Equal(SystemColors.ControlText.ToArgb(), control.ForeColor.ToArgb()); + } + + [Fact] + public void PrintPreviewControl_ForeColorNotSet_ShouldSerializeReturnsFalse() + { + PrintPreviewControl control = new(); + + // ForeColor not explicitly set: ShouldSerializeForeColor must return false. + Assert.False(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + } + + [Fact] + public void PrintPreviewControl_ForeColorWhiteExplicitlySet_ShouldSerializeReturnsTrue() + { + // Regression: #14420 - explicitly setting White must be distinguishable from the default. + PrintPreviewControl control = new() { ForeColor = Color.White }; + + Assert.True(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + } + + [Fact] + public void PrintPreviewControl_ForeColorNonDefault_ShouldSerializeReturnsTrue() + { + PrintPreviewControl control = new() { ForeColor = Color.Red }; + + Assert.True(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + } + + [Fact] + public void PrintPreviewControl_ForeColorReset_ShouldSerializeReturnsFalse() + { + PrintPreviewControl control = new() { ForeColor = Color.Red }; + control.ResetForeColor(); + + Assert.False(control.TestAccessor.Dynamic.ShouldSerializeForeColor()); + Assert.Equal(SystemColors.ControlText.ToArgb(), control.ForeColor.ToArgb()); + } + [Fact] public void ShowPrintPreviewControlHighContrast_BackColorIsCorrect() {