Skip to content

Commit 66f365a

Browse files
author
Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)
committed
Handles feedback
1 parent bd5bec6 commit 66f365a

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControl.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,10 @@ private void DrawPages(Graphics g, Rectangle rect, PreviewPageInfo[] pages, Brus
691691
Rectangle box = pageRenderArea[i];
692692
g.DrawRectangle(Pens.Black, box);
693693

694-
// Page background is fixed white; ForeColor is unrelated (it colors message text only).
695-
using (var brush = Color.White.GetCachedSolidBrushScope())
694+
// Default page fill is white (paper); an explicitly set ForeColor is still honored,
695+
// as it always has been.
696+
Color pageColor = _isForeColorSet ? ForeColor : Color.White;
697+
using (var brush = pageColor.GetCachedSolidBrushScope())
696698
{
697699
g.FillRectangle(brush, box);
698700
}

src/test/unit/System.Windows.Forms/System/Windows/Forms/Printing/PrintPreviewControlTests.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ public void PrintPreviewControl_ForeColorReset_ShouldSerializeReturnsFalse()
7070
}
7171

7272
[WinFormsFact]
73-
public void PrintPreviewControl_PageWithNoImage_RendersWhiteNotForeColor()
73+
public void PrintPreviewControl_PageWithNoImage_DefaultForeColor_RendersWhite()
7474
{
7575
// Regression test for #14838: a page with no drawable content (e.g. from an empty
76-
// PrintDocument) must render as white paper, not as a solid ForeColor rectangle.
76+
// PrintDocument), with ForeColor left at its default, must render as white paper,
77+
// not as a solid black rectangle.
7778
using PrintPreviewControl control = new()
7879
{
79-
ForeColor = Color.Black,
8080
Size = new Size(200, 200)
8181
};
8282

@@ -95,6 +95,31 @@ public void PrintPreviewControl_PageWithNoImage_RendersWhiteNotForeColor()
9595
Assert.Equal(Color.White.ToArgb(), centerPixel.ToArgb());
9696
}
9797

98+
[WinFormsFact]
99+
public void PrintPreviewControl_PageWithNoImage_ExplicitForeColor_RendersForeColor()
100+
{
101+
// ForeColor has driven the page background fill since the original .NET Framework port;
102+
// an explicitly set value must still be honored (see PR #14857 discussion), not overridden
103+
// by the white default that only applies when ForeColor was never set.
104+
using PrintPreviewControl control = new()
105+
{
106+
ForeColor = Color.Red,
107+
Size = new Size(200, 200)
108+
};
109+
110+
control.CreateControl();
111+
112+
PreviewPageInfo[] pageInfo = [new(image: null, physicalSize: new Size(850, 1100))];
113+
control.TestAccessor.Dynamic._pageInfo = pageInfo;
114+
115+
using Bitmap bitmap = new(control.Width, control.Height);
116+
control.DrawToBitmap(bitmap, new Rectangle(Point.Empty, control.Size));
117+
118+
Color centerPixel = bitmap.GetPixel(bitmap.Width / 2, bitmap.Height / 2);
119+
120+
Assert.Equal(Color.Red.ToArgb(), centerPixel.ToArgb());
121+
}
122+
98123
[Fact]
99124
public void ShowPrintPreviewControlHighContrast_BackColorIsCorrect()
100125
{

0 commit comments

Comments
 (0)