diff --git a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs index 5f29bc722df..b9ee2e15019 100644 --- a/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs +++ b/src/System.Windows.Forms/src/System/Windows/Forms/Printing/PrintPreviewControl.cs @@ -592,7 +592,13 @@ private void CalculatePageInfo() private void DrawMessage(Graphics g, Rectangle rect, bool isExceptionPrinting) { - using var brush = ForeColor.GetCachedSolidBrushScope(); + Color brushColor = SystemColors.ControlText; + if (SystemInformation.HighContrast && Parent is Control parent) + { + brushColor = parent.BackColor; + } + + using var brush = brushColor.GetCachedSolidBrushScope(); using StringFormat format = new() { @@ -737,7 +743,7 @@ private void PaintFocus(PaintEventArgs e, bool isHighContrast) /// private Color GetBackColor(bool isHighContract) { - return (isHighContract && !ShouldSerializeBackColor()) ? SystemColors.ControlDark : BackColor; + return (isHighContract && !ShouldSerializeBackColor()) ? SystemColors.ControlDarkDark : BackColor; } private static int PixelsToPhysical(int pixels, int dpi) => (int)(pixels * 100.0 / dpi); diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Printing/PrintPreviewControlTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Printing/PrintPreviewControlTests.cs index da64c99b68c..9511c7459a4 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Printing/PrintPreviewControlTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Printing/PrintPreviewControlTests.cs @@ -8,34 +8,36 @@ namespace System.Windows.Forms.Tests; // NB: doesn't require thread affinity public class PrintPreviewControlTests { - private const int EmptyColorArgb = 0; - private const int BlueColorArgb = -16776961; - private const int GreenColorArgb = -16744448; - private const int ControlDarkColorArgb = -6250336; - private const int AppWorkSpaceNoHcColorArgb = -5526613; - private const int AppWorkSpaceHcColorArgb = -1; - - [Theory] - [InlineData(EmptyColorArgb, false, AppWorkSpaceNoHcColorArgb)] - [InlineData(EmptyColorArgb, true, ControlDarkColorArgb)] - [InlineData(BlueColorArgb, false, BlueColorArgb)] - [InlineData(GreenColorArgb, true, GreenColorArgb)] - public void ShowPrintPreviewControl_BackColorIsCorrect(int customBackColorArgb, bool isHighContrast, int expectedBackColorArgb) + [Fact] + public void ShowPrintPreviewControl_BackColorIsCorrect() { PrintPreviewControl control = new(); - if (customBackColorArgb != EmptyColorArgb) - { - control.BackColor = Color.FromArgb(customBackColorArgb); - } + int actualBackColorArgb = control.TestAccessor().Dynamic.GetBackColor(false).ToArgb(); + Assert.Equal(SystemColors.AppWorkspace.ToArgb(), actualBackColorArgb); - int actualBackColorArgb = control.TestAccessor().Dynamic.GetBackColor(isHighContrast).ToArgb(); - Assert.Equal(expectedBackColorArgb, actualBackColorArgb); + control.BackColor = Color.Green; + actualBackColorArgb = control.TestAccessor().Dynamic.GetBackColor(false).ToArgb(); + Assert.Equal(Color.Green.ToArgb(), actualBackColorArgb); + } + + [Fact] + public void ShowPrintPreviewControlHighContrast_BackColorIsCorrect() + { + PrintPreviewControl control = new(); + + int actualBackColorArgb = control.TestAccessor().Dynamic.GetBackColor(true).ToArgb(); + + Assert.Equal(SystemColors.ControlDarkDark.ToArgb(), actualBackColorArgb); // Default AppWorkSpace color in HC theme does not allow to follow HC standards. - if (isHighContrast) - { - Assert.True(!AppWorkSpaceHcColorArgb.Equals(actualBackColorArgb)); - } + Assert.False(SystemColors.AppWorkspace.ToArgb().Equals(actualBackColorArgb)); + + control.BackColor = Color.Green; + + actualBackColorArgb = control.TestAccessor().Dynamic.GetBackColor(true).ToArgb(); + + Assert.Equal(Color.Green.ToArgb(), actualBackColorArgb); + Assert.False(SystemColors.AppWorkspace.ToArgb().Equals(actualBackColorArgb)); } }