Skip to content

Commit 45fbdc1

Browse files
[release/9.0] Fix Color value in PageSettings (#13389)
Backport of #13385 to release/9.0 /cc @JeremyKuhne ## Customer Impact Reading `PageSettings.Color` gives the wrong value, there is no workaround. ## Testing Regression unit test. ## Risk Low, this was a manual mistake in converting to source generated interop. ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/winforms/pull/13389)
2 parents fe592d7 + a1cdb5a commit 45fbdc1

File tree

4 files changed

+63
-31
lines changed

4 files changed

+63
-31
lines changed

src/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Rectangle Bounds
5353
public bool Color
5454
{
5555
get => _color.IsDefault
56-
? _printerSettings.GetModeField(ModeField.Color, (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME) == (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME
56+
? _printerSettings.GetModeField(ModeField.Color, (short)DEVMODE_COLOR.DMCOLOR_MONOCHROME) == (short)DEVMODE_COLOR.DMCOLOR_COLOR
5757
: (bool)_color;
5858
set => _color = value;
5959
}

src/System.Drawing.Common/tests/Helpers.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Drawing.Printing;
56
using System.Text;
67
using Windows.Win32;
@@ -21,6 +22,47 @@ public unsafe static class Helpers
2122

2223
public static bool AreAnyPrintersInstalled() => s_anyInstalledPrinters;
2324

25+
private const string PrintToPdfPrinterName = "Microsoft Print to PDF";
26+
27+
/// <summary>
28+
/// Checks if PDF printing is supported by verifying installed printers.
29+
/// </summary>
30+
/// <returns><see langword="true"/> if a PDF printer is installed; otherwise, <see langword="false"/>.</returns>
31+
public static bool CanPrintToPdf()
32+
{
33+
foreach (string name in InstalledPrinters)
34+
{
35+
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
36+
{
37+
return true;
38+
}
39+
}
40+
41+
return false;
42+
}
43+
44+
/// <summary>
45+
/// Attempts to get the name of the PDF printer installed on the system.
46+
/// </summary>
47+
/// <param name="printerName">
48+
/// When this method returns, contains the name of the PDF printer if found; otherwise, <see langword="null"/>.
49+
/// </param>
50+
/// <returns><see langword="true"/> if a PDF printer is found; otherwise, <see langword="false"/>.</returns>
51+
public static bool TryGetPdfPrinterName([NotNullWhen(true)] out string? printerName)
52+
{
53+
foreach (string name in InstalledPrinters)
54+
{
55+
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
56+
{
57+
printerName = name;
58+
return true;
59+
}
60+
}
61+
62+
printerName = null;
63+
return false;
64+
}
65+
2466
public static string GetTestBitmapPath(string fileName) => GetTestPath("bitmaps", fileName);
2567
public static string GetTestFontPath(string fileName) => GetTestPath("fonts", fileName);
2668
public static string GetTestColorProfilePath(string fileName) => GetTestPath("colorProfiles", fileName);

src/System.Drawing.Common/tests/System/Drawing/Printing/PageSettingsTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,21 @@ public void Clone_Success()
6363
Assert.Equal(ps.PaperSource.Kind, clone.PaperSource.Kind);
6464
Assert.Equal(ps.PaperSource.SourceName, clone.PaperSource.SourceName);
6565
}
66+
67+
[ConditionalFact(Helpers.AnyInstalledPrinters)]
68+
public void PrintToPDF_DefaultPageSettings_IsColor()
69+
{
70+
// Regression test for https://github.com/dotnet/winforms/issues/13367
71+
if (!Helpers.TryGetPdfPrinterName(out string? printerName))
72+
{
73+
return;
74+
}
75+
76+
PrinterSettings printerSettings = new()
77+
{
78+
PrinterName = printerName
79+
};
80+
81+
printerSettings.DefaultPageSettings.Color.Should().BeTrue("PDF printer should support color printing.");
82+
}
6683
}

src/System.Drawing.Common/tests/System/Drawing/Printing/PrintDocumentTests.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ public void EndPrint_SetValue_ReturnsExpected()
160160
Assert.False(flag);
161161
}
162162

163-
[ConditionalFact(nameof(CanPrintToPdf))]
163+
[ConditionalFact(typeof(Helpers), nameof(Helpers.CanPrintToPdf))]
164164
public void Print_DefaultPrintController_Success()
165165
{
166-
if (!CanPrintToPdf())
166+
if (!Helpers.TryGetPdfPrinterName(out string? printerName))
167167
{
168168
return;
169169
}
@@ -172,7 +172,7 @@ public void Print_DefaultPrintController_Success()
172172
PrintEventHandler endPrintHandler = new((sender, e) => endPrintCalled = true);
173173
using (PrintDocument document = new())
174174
{
175-
document.PrinterSettings.PrinterName = GetPdfPrinterName();
175+
document.PrinterSettings.PrinterName = printerName;
176176
document.PrinterSettings.PrintFileName = GetTestFilePath();
177177
document.PrinterSettings.PrintToFile = true;
178178
document.EndPrint += endPrintHandler;
@@ -247,33 +247,6 @@ private void AssertDefaultPageSettings(PageSettings pageSettings)
247247
Assert.True(pageSettings.PrinterSettings.IsDefaultPrinter);
248248
}
249249

250-
private const string PrintToPdfPrinterName = "Microsoft Print to PDF";
251-
private static bool CanPrintToPdf()
252-
{
253-
foreach (string name in Helpers.InstalledPrinters)
254-
{
255-
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
256-
{
257-
return true;
258-
}
259-
}
260-
261-
return false;
262-
}
263-
264-
private static string GetPdfPrinterName()
265-
{
266-
foreach (string name in Helpers.InstalledPrinters)
267-
{
268-
if (name.StartsWith(PrintToPdfPrinterName, StringComparison.Ordinal))
269-
{
270-
return name;
271-
}
272-
}
273-
274-
throw new InvalidOperationException("No PDF printer installed");
275-
}
276-
277250
private class TestPrintController : PrintController
278251
{
279252
public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)

0 commit comments

Comments
 (0)