Skip to content
Merged
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
44 changes: 44 additions & 0 deletions src/Common/src/Interop/Comdlg32/Interop.PD.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

internal partial class Interop
{
internal partial class Comdlg32
{
[Flags]
public enum PD : uint
{
ALLPAGES = 0x00000000,
SELECTION = 0x00000001,
PAGENUMS = 0x00000002,
NOSELECTION = 0x00000004,
NOPAGENUMS = 0x00000008,
COLLATE = 0x00000010,
PRINTTOFILE = 0x00000020,
PRINTSETUP = 0x00000040,
NOWARNING = 0x00000080,
RETURNDC = 0x00000100,
RETURNIC = 0x00000200,
RETURNDEFAULT = 0x00000400,
SHOWHELP = 0x00000800,
ENABLEPRINTHOOK = 0x00001000,
ENABLESETUPHOOK = 0x00002000,
ENABLEPRINTTEMPLATE = 0x00004000,
ENABLESETUPTEMPLATE = 0x00008000,
ENABLEPRINTTEMPLATEHANDLE = 0x00010000,
ENABLESETUPTEMPLATEHANDLE = 0x00020000,
USEDEVMODECOPIES = 0x00040000,
USEDEVMODECOPIESANDCOLLATE = 0x00040000,
DISABLEPRINTTOFILE = 0x00080000,
HIDEPRINTTOFILE = 0x00100000,
NONETWORKBUTTON = 0x00200000,
CURRENTPAGE = 0x00400000,
NOCURRENTPAGE = 0x00800000,
EXCLUSIONFLAGS = 0x01000000,
USELARGETEMPLATE = 0x10000000
}
}
}
16 changes: 16 additions & 0 deletions src/Common/src/Interop/Comdlg32/Interop.PD_RESULT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

internal partial class Interop
{
internal partial class Comdlg32
{
public enum PD_RESULT
{
CANCEL = 0,
PRINT = 1,
APPLY = 2
}
}
}
50 changes: 6 additions & 44 deletions src/Common/src/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,35 +374,7 @@ public static uint MAKELCID(uint lgid, uint sort)
NM_CUSTOMDRAW = ((0 - 0) - 12),
NM_RELEASEDCAPTURE = ((0 - 0) - 16);

public const int PD_ALLPAGES = 0x00000000,
PD_SELECTION = 0x00000001,
PD_PAGENUMS = 0x00000002,
PD_NOSELECTION = 0x00000004,
PD_NOPAGENUMS = 0x00000008,
PD_COLLATE = 0x00000010,
PD_PRINTTOFILE = 0x00000020,
PD_PRINTSETUP = 0x00000040,
PD_NOWARNING = 0x00000080,
PD_RETURNDC = 0x00000100,
PD_RETURNIC = 0x00000200,
PD_RETURNDEFAULT = 0x00000400,
PD_SHOWHELP = 0x00000800,
PD_ENABLEPRINTHOOK = 0x00001000,
PD_ENABLESETUPHOOK = 0x00002000,
PD_ENABLEPRINTTEMPLATE = 0x00004000,
PD_ENABLESETUPTEMPLATE = 0x00008000,
PD_ENABLEPRINTTEMPLATEHANDLE = 0x00010000,
PD_ENABLESETUPTEMPLATEHANDLE = 0x00020000,
PD_USEDEVMODECOPIES = 0x00040000,
PD_USEDEVMODECOPIESANDCOLLATE = 0x00040000,
PD_DISABLEPRINTTOFILE = 0x00080000,
PD_HIDEPRINTTOFILE = 0x00100000,
PD_NONETWORKBUTTON = 0x00200000,
PD_CURRENTPAGE = 0x00400000,
PD_NOCURRENTPAGE = 0x00800000,
PD_EXCLUSIONFLAGS = 0x01000000,
PD_USELARGETEMPLATE = 0x10000000,
PRF_CHECKVISIBLE = 0x00000001,
public const int PRF_CHECKVISIBLE = 0x00000001,
PRF_NONCLIENT = 0x00000002,
PRF_CLIENT = 0x00000004,
PRF_ERASEBKGND = 0x00000008,
Expand Down Expand Up @@ -565,11 +537,6 @@ public const int

public static int START_PAGE_GENERAL = unchecked((int)0xffffffff);

// Result action ids for PrintDlgEx.
public const int PD_RESULT_CANCEL = 0;
public const int PD_RESULT_PRINT = 1;
public const int PD_RESULT_APPLY = 2;

private static uint wmMouseEnterMessage = uint.MaxValue;

public static User32.WindowMessage WM_MOUSEENTER
Expand Down Expand Up @@ -724,7 +691,7 @@ public interface PRINTDLG
IntPtr hDevNames { get; set; }
IntPtr hDC { get; set; }

int Flags { get; set; }
Comdlg32.PD Flags { get; set; }

short nFromPage { get; set; }
short nToPage { get; set; }
Expand Down Expand Up @@ -757,8 +724,6 @@ public class PRINTDLG_32 : PRINTDLG
IntPtr m_hDevNames;
IntPtr m_hDC;

int m_Flags;
Copy link
Contributor

Choose a reason for hiding this comment

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

fields cannot be moved around


short m_nFromPage;
short m_nToPage;
short m_nMinPage;
Expand All @@ -784,7 +749,7 @@ public class PRINTDLG_32 : PRINTDLG
public IntPtr hDevNames { get { return m_hDevNames; } set { m_hDevNames = value; } }
public IntPtr hDC { get { return m_hDC; } set { m_hDC = value; } }

public int Flags { get { return m_Flags; } set { m_Flags = value; } }
Copy link
Contributor

@weltkante weltkante Feb 13, 2020

Choose a reason for hiding this comment

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

automatic properties must be avoided in interop since you don't control the binary layout anymore

[edit] made the comment on the wrong side of the diff, this line got replaced by an automatic property public int Flags { get; set; } which is what I was talking about above.

public Comdlg32.PD Flags { get; set; }

public short nFromPage { get { return m_nFromPage; } set { m_nFromPage = value; } }
public short nToPage { get { return m_nToPage; } set { m_nToPage = value; } }
Expand Down Expand Up @@ -817,8 +782,6 @@ public class PRINTDLG_64 : PRINTDLG
IntPtr m_hDevNames;
IntPtr m_hDC;

int m_Flags;
Copy link
Contributor

Choose a reason for hiding this comment

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

fields cannot be moved around

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, should have picked that! 🤦‍♂


short m_nFromPage;
short m_nToPage;
short m_nMinPage;
Expand All @@ -844,7 +807,7 @@ public class PRINTDLG_64 : PRINTDLG
public IntPtr hDevNames { get { return m_hDevNames; } set { m_hDevNames = value; } }
public IntPtr hDC { get { return m_hDC; } set { m_hDC = value; } }

public int Flags { get { return m_Flags; } set { m_Flags = value; } }
Copy link
Contributor

@weltkante weltkante Feb 13, 2020

Choose a reason for hiding this comment

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

don't use automatic properties since you cannot control the binary layout

[edit] made the comment on the wrong side of the diff, this line got replaced by an automatic property public int Flags { get; set; } which is what I was talking about above.

public Comdlg32.PD Flags { get; set; }

public short nFromPage { get { return m_nFromPage; } set { m_nFromPage = value; } }
public short nToPage { get { return m_nToPage; } set { m_nToPage = value; } }
Expand Down Expand Up @@ -875,7 +838,7 @@ public class PRINTDLGEX
public IntPtr hDevNames;
public IntPtr hDC;

public int Flags;
public Comdlg32.PD Flags;
public int Flags2;

public int ExclusionFlags;
Expand All @@ -900,8 +863,7 @@ public class PRINTDLGEX
public IntPtr lphPropertyPages;

public int nStartPage;
public int dwResultAction;

public Comdlg32.PD_RESULT dwResultAction;
}

// x86 requires EXPLICIT packing of 1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.TVHT.cs" Link="Interop\ComCtl32\Interop.TVHT.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.TVIS.cs" Link="Interop\ComCtl32\Interop.TVIS.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.TVS_EX.cs" Link="Interop\ComCtl32\Interop.TVS_EX.cs" />
<Compile Include="..\..\Common\src\Interop\Comdlg32\Interop.CF.cs" Link="Interop\Comdlg32\Interop.CF.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.BitBlt.cs" Link="Interop\Gdi32\Interop.BitBlt.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.CreatePen.cs" Link="Interop\Gdi32\Interop.CreatePen.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.MCGIF.cs" Link="Interop\ComCtl32\Interop.MCGIF.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.MCGIP.cs" Link="Interop\ComCtl32\Interop.MCGIP.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.MCGRIDINFO.cs" Link="Interop\ComCtl32\Interop.MCGRIDINFO.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.MCHITTESTINFO.cs" Link="Interop\ComCtl32\Interop.MCHITTESTINFO.cs" />
<Compile Include="..\..\Common\src\Interop\Comdlg32\Interop.CF.cs" Link="Interop\Comdlg32\Interop.CF.cs" />
<Compile Include="..\..\Common\src\Interop\Comdlg32\Interop.PD.cs" Link="Interop\Comdlg32\Interop.PD.cs" />
<Compile Include="..\..\Common\src\Interop\Comdlg32\Interop.PD_RESULT.cs" Link="Interop\Comdlg32\Interop.PD_RESULT.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.BitBlt.cs" Link="Interop\Gdi32\Interop.BitBlt.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.CreatePen.cs" Link="Interop\Gdi32\Interop.CreatePen.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.CreateRectRgn.cs" Link="Interop\Gdi32\Interop.CreateRectRgn.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.CreateSolidBrush.cs" Link="Interop\Gdi32\Interop.CreateSolidBrush.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.DeleteObject.cs" Link="Interop\Gdi32\Interop.DeleteObject.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using static Interop;
using static Interop.Comdlg32;

namespace System.Windows.Forms
{
Expand All @@ -19,8 +20,7 @@ namespace System.Windows.Forms
// The only event this dialog has is HelpRequested, which isn't very useful
public sealed class PrintDialog : CommonDialog
{
private const int printRangeMask = (int)(PrintRange.AllPages | PrintRange.SomePages
| PrintRange.Selection | PrintRange.CurrentPage);
private const PD printRangeMask = PD.ALLPAGES | PD.PAGENUMS | PD.SELECTION | PD.CURRENTPAGE;

// If PrintDocument != null, settings == printDocument.PrinterSettings
private PrinterSettings settings = null;
Expand Down Expand Up @@ -223,51 +223,51 @@ public bool ShowNetwork
[SRDescription(nameof(SR.PDuseEXDialog))]
public bool UseEXDialog { get; set; }

private int GetFlags()
private PD GetFlags()
{
int flags = 0;
PD flags = PD.ALLPAGES;

// Only set this flag when using PRINTDLG and PrintDlg,
// and not when using PrintDlgEx and PRINTDLGEX.
if (!UseEXDialog)
{
flags |= NativeMethods.PD_ENABLEPRINTHOOK;
flags |= PD.ENABLEPRINTHOOK;
}

if (!allowCurrentPage)
{
flags |= NativeMethods.PD_NOCURRENTPAGE;
flags |= PD.NOCURRENTPAGE;
}
if (!allowPages)
{
flags |= NativeMethods.PD_NOPAGENUMS;
flags |= PD.NOPAGENUMS;
}
if (!allowPrintToFile)
{
flags |= NativeMethods.PD_DISABLEPRINTTOFILE;
flags |= PD.DISABLEPRINTTOFILE;
}
if (!allowSelection)
{
flags |= NativeMethods.PD_NOSELECTION;
flags |= PD.NOSELECTION;
}

flags |= (int)PrinterSettings.PrintRange;
flags |= (PD)PrinterSettings.PrintRange;

if (printToFile)
{
flags |= NativeMethods.PD_PRINTTOFILE;
flags |= PD.PRINTTOFILE;
}
if (showHelp)
{
flags |= NativeMethods.PD_SHOWHELP;
flags |= PD.SHOWHELP;
}
if (!showNetwork)
{
flags |= NativeMethods.PD_NONETWORKBUTTON;
flags |= PD.NONETWORKBUTTON;
}
if (PrinterSettings.Collate)
{
flags |= NativeMethods.PD_COLLATE;
flags |= PD.COLLATE;
}

return flags;
Expand Down Expand Up @@ -428,7 +428,7 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.WndProc hookProcPtr

UpdatePrinterSettings(data.hDevMode, data.hDevNames, data.nCopies, data.Flags, settings, PageSettings);

PrintToFile = ((data.Flags & NativeMethods.PD_PRINTTOFILE) != 0);
PrintToFile = (data.Flags & PD.PRINTTOFILE) != 0;
PrinterSettings.PrintToFile = PrintToFile;

if (AllowSomePages)
Expand All @@ -441,10 +441,10 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.WndProc hookProcPtr
// PRINTDLG.nCopies or PRINTDLG.nCopies indicates the number of copies the user wants
// to print, and the PD_COLLATE flag in the Flags member indicates
// whether the user wants to print them collated.
if ((data.Flags & NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE) == 0)
if ((data.Flags & PD.USEDEVMODECOPIESANDCOLLATE) == 0)
{
PrinterSettings.Copies = data.nCopies;
PrinterSettings.Collate = ((data.Flags & NativeMethods.PD_COLLATE) == NativeMethods.PD_COLLATE);
PrinterSettings.Collate = (data.Flags & PD.COLLATE) == PD.COLLATE;
}

return true;
Expand Down Expand Up @@ -522,17 +522,17 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data)
//
// The flags NativeMethods.PD_SHOWHELP and NativeMethods.PD_NONETWORKBUTTON don't work with
// PrintDlgEx. So we have to strip them out.
data.Flags &= ~(NativeMethods.PD_SHOWHELP | NativeMethods.PD_NONETWORKBUTTON);
data.Flags &= ~(PD.SHOWHELP | PD.NONETWORKBUTTON);

int hr = UnsafeNativeMethods.PrintDlgEx(data);
if (NativeMethods.Failed(hr) || data.dwResultAction == NativeMethods.PD_RESULT_CANCEL)
if (NativeMethods.Failed(hr) || data.dwResultAction == PD_RESULT.CANCEL)
{
return false;
}

UpdatePrinterSettings(data.hDevMode, data.hDevNames, (short)data.nCopies, data.Flags, PrinterSettings, PageSettings);

PrintToFile = ((data.Flags & NativeMethods.PD_PRINTTOFILE) != 0);
PrintToFile = (data.Flags & PD.PRINTTOFILE) != 0;
PrinterSettings.PrintToFile = PrintToFile;
if (AllowSomePages)
{
Expand All @@ -549,14 +549,14 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data)
// PRINTDLG.nCopies or PRINTDLG.nCopies indicates the number of copies the user wants
// to print, and the PD_COLLATE flag in the Flags member indicates
// whether the user wants to print them collated.
if ((data.Flags & NativeMethods.PD_USEDEVMODECOPIESANDCOLLATE) == 0)
if ((data.Flags & PD.USEDEVMODECOPIESANDCOLLATE) == 0)
{
PrinterSettings.Copies = (short)(data.nCopies);
PrinterSettings.Collate = ((data.Flags & NativeMethods.PD_COLLATE) == NativeMethods.PD_COLLATE);
PrinterSettings.Collate = (data.Flags & PD.COLLATE) == PD.COLLATE;
}

// We should return true only if the user pressed the "Print" button while dismissing the dialog.
return (data.dwResultAction == NativeMethods.PD_RESULT_PRINT);
return data.dwResultAction == PD_RESULT.PRINT;
}
finally
{
Expand All @@ -580,7 +580,7 @@ private bool ShowPrintDialog(IntPtr hwndOwner, NativeMethods.PRINTDLGEX data)
// Due to the nature of PRINTDLGEX vs PRINTDLG, separate but similar methods
// are required for updating the settings from the structure utilized by the dialog.
// Take information from print dialog and put in PrinterSettings
private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, int flags, PrinterSettings settings, PageSettings pageSettings)
private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, PD flags, PrinterSettings settings, PageSettings pageSettings)
{
// Mode
settings.SetHdevmode(hDevMode);
Expand Down