Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,11 @@ private void DrawPages(Graphics g, Rectangle rect, PreviewPageInfo[] pages, Brus
{
Rectangle box = pageRenderArea[i];
g.DrawRectangle(Pens.Black, box);
using (var brush = ForeColor.GetCachedSolidBrushScope())

// Default page fill is white (paper); an explicitly set ForeColor is still honored,
// as it always has been.
Color pageColor = _isForeColorSet ? ForeColor : Color.White;
using (var brush = pageColor.GetCachedSolidBrushScope())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_isForeColorSet  changes only when  ForeColorChanged  fires, but  Control.ForeColor  raises that event only when the effective color changes. Therefore:

control.ForeColor = SystemColors.ControlText;

does not set the flag because construction already assigned that color. The PR then renders white instead of honoring the explicit foreground color, unlike previous behavior. This can affect existing designer-generated code from when white was the default.

Image

{
g.FillRectangle(brush, box);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System.Drawing;
using System.Drawing.Printing;

namespace System.Windows.Forms.Tests;

Expand Down Expand Up @@ -68,6 +69,57 @@ public void PrintPreviewControl_ForeColorReset_ShouldSerializeReturnsFalse()
Assert.Equal(SystemColors.ControlText.ToArgb(), control.ForeColor.ToArgb());
}

[WinFormsFact]
public void PrintPreviewControl_PageWithNoImage_DefaultForeColor_RendersWhite()
{
// Regression test for #14838: a page with no drawable content (e.g. from an empty
// PrintDocument), with ForeColor left at its default, must render as white paper,
// not as a solid black rectangle.
using PrintPreviewControl control = new()
{
Size = new Size(200, 200)
};

control.CreateControl();

PreviewPageInfo[] pageInfo = [new(image: null, physicalSize: new Size(850, 1100))];
control.TestAccessor.Dynamic._pageInfo = pageInfo;

using Bitmap bitmap = new(control.Width, control.Height);
control.DrawToBitmap(bitmap, new Rectangle(Point.Empty, control.Size));

// The single page fills nearly the whole control, so the center pixel lands well
// inside the page interior, away from its 1px black border.
Color centerPixel = bitmap.GetPixel(bitmap.Width / 2, bitmap.Height / 2);

Assert.Equal(Color.White.ToArgb(), centerPixel.ToArgb());
}

[WinFormsFact]
public void PrintPreviewControl_PageWithNoImage_ExplicitForeColor_RendersForeColor()
{
// ForeColor has driven the page background fill since the original .NET Framework port;
// an explicitly set value must still be honored (see PR #14857 discussion), not overridden
// by the white default that only applies when ForeColor was never set.
using PrintPreviewControl control = new()
{
ForeColor = Color.Red,
Size = new Size(200, 200)
};

control.CreateControl();

PreviewPageInfo[] pageInfo = [new(image: null, physicalSize: new Size(850, 1100))];
control.TestAccessor.Dynamic._pageInfo = pageInfo;

using Bitmap bitmap = new(control.Width, control.Height);
control.DrawToBitmap(bitmap, new Rectangle(Point.Empty, control.Size));

Color centerPixel = bitmap.GetPixel(bitmap.Width / 2, bitmap.Height / 2);

Assert.Equal(Color.Red.ToArgb(), centerPixel.ToArgb());
}

[Fact]
public void ShowPrintPreviewControlHighContrast_BackColorIsCorrect()
{
Expand Down