PageSettings
Specifies settings that apply to a single, printed page.
using System.Runtime.CompilerServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
namespace System.Drawing.Printing
{
[NullableContext(1)]
[Nullable(0)]
public class PageSettings : ICloneable
{
private PrinterSettings _printerSettings;
private TriState _color = TriState.Default;
[Nullable(2)]
private PaperSize _paperSize;
[Nullable(2)]
private PaperSource _paperSource;
[Nullable(2)]
private PrinterResolution _printerResolution;
private TriState _landscape = TriState.Default;
private Margins _margins = new Margins();
public Rectangle Bounds {
get {
HGLOBAL hGLOBAL = (HGLOBAL)_printerSettings.GetHdevmode();
Rectangle bounds = GetBounds(hGLOBAL);
PInvokeCore.GlobalFree(hGLOBAL);
return bounds;
}
}
public bool Color {
get {
if (!_color.IsDefault)
return (bool)_color;
return _printerSettings.GetModeField(ModeField.Color, 1) == 1;
}
set {
_color = value;
}
}
public float HardMarginX {
get {
CreateDcScope scope = _printerSettings.CreateDeviceContext(this);
try {
int deviceCaps = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.LOGPIXELSX);
return (float)(PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETX) * 100 / deviceCaps);
} finally {
scope.Dispose();
}
}
}
public float HardMarginY {
get {
CreateDcScope scope = _printerSettings.CreateDeviceContext(this);
try {
int deviceCaps = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.LOGPIXELSY);
return (float)(PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETY) * 100 / deviceCaps);
} finally {
scope.Dispose();
}
}
}
public bool Landscape {
get {
if (!_landscape.IsDefault)
return (bool)_landscape;
return (long)_printerSettings.GetModeField(ModeField.Orientation, 1) == 2;
}
set {
_landscape = value;
}
}
public Margins Margins {
get {
return _margins;
}
set {
_margins = value;
}
}
public PaperSize PaperSize {
get {
return GetPaperSize(HGLOBAL.Null);
}
set {
_paperSize = value;
}
}
public unsafe PaperSource PaperSource {
get {
if (_paperSource != null)
return _paperSource;
HGLOBAL hMem = (HGLOBAL)_printerSettings.GetHdevmode();
DEVMODEW* devmode = (DEVMODEW*)PInvokeCore.GlobalLock(hMem);
PaperSource result = PaperSourceFromMode(devmode);
PInvokeCore.GlobalUnlock(hMem);
PInvokeCore.GlobalFree(hMem);
return result;
}
set {
_paperSource = value;
}
}
public RectangleF PrintableArea {
get {
RectangleF result = default(RectangleF);
CreateDcScope scope = _printerSettings.CreateInformationContext(this);
try {
int deviceCaps = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.LOGPIXELSX);
int deviceCaps2 = PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.LOGPIXELSY);
if (!Landscape) {
result.X = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETX) * 100 / (float)deviceCaps;
result.Y = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETY) * 100 / (float)deviceCaps2;
result.Width = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.HORZRES) * 100 / (float)deviceCaps;
result.Height = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.VERTRES) * 100 / (float)deviceCaps2;
} else {
result.Y = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETX) * 100 / (float)deviceCaps;
result.X = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.PHYSICALOFFSETY) * 100 / (float)deviceCaps2;
result.Height = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.HORZRES) * 100 / (float)deviceCaps;
result.Width = (float)PInvokeCore.GetDeviceCaps((HDC)ref scope, GET_DEVICE_CAPS_INDEX.VERTRES) * 100 / (float)deviceCaps2;
}
return result;
} finally {
scope.Dispose();
}
}
}
public unsafe PrinterResolution PrinterResolution {
get {
if (_printerResolution != null)
return _printerResolution;
HGLOBAL hMem = (HGLOBAL)_printerSettings.GetHdevmode();
DEVMODEW* devmode = (DEVMODEW*)PInvokeCore.GlobalLock(hMem);
PrinterResolution result = PrinterResolutionFromMode(devmode);
PInvokeCore.GlobalUnlock(hMem);
PInvokeCore.GlobalFree(hMem);
return result;
}
set {
_printerResolution = value;
}
}
public PrinterSettings PrinterSettings {
get {
return _printerSettings;
}
set {
_printerSettings = (value ?? new PrinterSettings());
}
}
private unsafe short ExtraBytes {
get {
HGLOBAL hdevmodeInternal = _printerSettings.GetHdevmodeInternal();
DEVMODEW* ptr = (DEVMODEW*)PInvokeCore.GlobalLock(hdevmodeInternal);
int result = (ptr != null) ? ((short)ptr->dmDriverExtra) : 0;
PInvokeCore.GlobalUnlock(hdevmodeInternal);
PInvokeCore.GlobalFree(hdevmodeInternal);
return (short)result;
}
}
public PageSettings()
: this(new PrinterSettings())
{
}
public PageSettings(PrinterSettings printerSettings)
{
_printerSettings = printerSettings;
}
public object Clone()
{
PageSettings obj = (PageSettings)MemberwiseClone();
obj._margins = (Margins)_margins.Clone();
return obj;
}
public unsafe void CopyToHdevmode(IntPtr hdevmode)
{
if (hdevmode == (IntPtr)0)
throw new ArgumentNullException("hdevmode");
DEVMODEW* ptr = (DEVMODEW*)PInvokeCore.GlobalLock((HGLOBAL)hdevmode);
if (_color.IsNotDefault && ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_COLOR))
ptr->dmColor = ((!_color.IsTrue) ? DEVMODE_COLOR.DMCOLOR_MONOCHROME : DEVMODE_COLOR.DMCOLOR_COLOR);
if (_landscape.IsNotDefault && ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_ORIENTATION))
ptr->dmOrientation = (short)((!_landscape.IsTrue) ? 1 : 2);
if (_paperSize != null) {
if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PAPERSIZE))
ptr->dmPaperSize = (short)_paperSize.RawKind;
bool flag = false;
bool flag2 = false;
if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PAPERLENGTH)) {
int num = PrinterUnitConvert.Convert(_paperSize.Height, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
ptr->dmPaperLength = (short)num;
flag2 = true;
}
if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PAPERWIDTH)) {
int num2 = PrinterUnitConvert.Convert(_paperSize.Width, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
ptr->dmPaperWidth = (short)num2;
flag = true;
}
if (_paperSize.Kind == PaperKind.Custom) {
if (!flag2) {
ptr->dmFields |= DEVMODE_FIELD_FLAGS.DM_PAPERLENGTH;
int num3 = PrinterUnitConvert.Convert(_paperSize.Height, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
ptr->dmPaperLength = (short)num3;
}
if (!flag) {
ptr->dmFields |= DEVMODE_FIELD_FLAGS.DM_PAPERWIDTH;
int num4 = PrinterUnitConvert.Convert(_paperSize.Width, PrinterUnit.Display, PrinterUnit.TenthsOfAMillimeter);
ptr->dmPaperWidth = (short)num4;
}
}
}
if (_paperSource != null && ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_DEFAULTSOURCE))
ptr->dmDefaultSource = (short)_paperSource.RawKind;
if (_printerResolution != null) {
if (_printerResolution.Kind == PrinterResolutionKind.Custom) {
if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PRINTQUALITY))
ptr->dmPrintQuality = (short)_printerResolution.X;
if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_YRESOLUTION))
ptr->dmYResolution = (short)_printerResolution.Y;
} else if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PRINTQUALITY)) {
ptr->dmPrintQuality = (short)_printerResolution.Kind;
}
}
if (ptr->dmDriverExtra >= ExtraBytes) {
string printerName = _printerSettings.PrinterName;
IntPtr intPtr;
if (printerName == null)
intPtr = (IntPtr)(void*)null;
else {
ref reference = ref printerName.GetPinnableReference();
intPtr = (IntPtr)(&reference);
}
char* value = (char*)(long)intPtr;
if (PInvoke.DocumentProperties(HWND.Null, HANDLE.Null, value, ptr, ptr, 10) < 0)
PInvokeCore.GlobalFree((HGLOBAL)hdevmode);
ref reference = ref *(char*)null;
}
PInvokeCore.GlobalUnlock((HGLOBAL)hdevmode);
}
internal Rectangle GetBounds(HGLOBAL modeHandle)
{
PaperSize paperSize = GetPaperSize(modeHandle);
if (!<GetBounds>g__IsLandscape|42_0(modeHandle))
return new Rectangle(0, 0, paperSize.Width, paperSize.Height);
return new Rectangle(0, 0, paperSize.Height, paperSize.Width);
}
private unsafe PaperSize GetPaperSize(HGLOBAL modeHandle)
{
if (_paperSize != null)
return _paperSize;
bool flag = false;
if (modeHandle.IsNull) {
modeHandle = (HGLOBAL)_printerSettings.GetHdevmode();
flag = true;
}
DEVMODEW* devmode = (DEVMODEW*)PInvokeCore.GlobalLock(modeHandle);
PaperSize result = PaperSizeFromMode(devmode);
PInvokeCore.GlobalUnlock(modeHandle);
if (flag)
PInvokeCore.GlobalFree(modeHandle);
return result;
}
[NullableContext(0)]
[return: Nullable(1)]
private unsafe PaperSize PaperSizeFromMode(DEVMODEW* devmode)
{
PaperSize[] paperSizes = _printerSettings.Get_PaperSizes();
if (devmode->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PAPERSIZE)) {
for (int i = 0; i < paperSizes.Length; i++) {
if (paperSizes[i].RawKind == devmode->Anonymous1.Anonymous1.dmPaperSize)
return paperSizes[i];
}
}
return new PaperSize(PaperKind.Custom, "custom", PrinterUnitConvert.Convert(devmode->Anonymous1.Anonymous1.dmPaperWidth, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display), PrinterUnitConvert.Convert(devmode->Anonymous1.Anonymous1.dmPaperLength, PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display));
}
[NullableContext(0)]
[return: Nullable(1)]
private unsafe PaperSource PaperSourceFromMode(DEVMODEW* devmode)
{
PaperSource[] paperSources = _printerSettings.Get_PaperSources();
if (devmode->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_DEFAULTSOURCE)) {
for (int i = 0; i < paperSources.Length; i++) {
if ((short)paperSources[i].RawKind == devmode->dmDefaultSource)
return paperSources[i];
}
}
return new PaperSource((PaperSourceKind)devmode->dmDefaultSource, "unknown");
}
[NullableContext(0)]
[return: Nullable(1)]
private unsafe PrinterResolution PrinterResolutionFromMode(DEVMODEW* devmode)
{
PrinterResolution[] printerResolutions = _printerSettings.Get_PrinterResolutions();
for (int i = 0; i < printerResolutions.Length; i++) {
if (devmode->dmPrintQuality >= 0 && devmode->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PRINTQUALITY) && devmode->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_YRESOLUTION)) {
if (printerResolutions[i].X == devmode->dmPrintQuality && printerResolutions[i].Y == devmode->dmYResolution)
return printerResolutions[i];
} else if (devmode->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_PRINTQUALITY) && printerResolutions[i].Kind == (PrinterResolutionKind)devmode->dmPrintQuality) {
return printerResolutions[i];
}
}
return new PrinterResolution(PrinterResolutionKind.Custom, devmode->dmPrintQuality, devmode->dmYResolution);
}
public unsafe void SetHdevmode(IntPtr hdevmode)
{
if (hdevmode == (IntPtr)0)
throw new ArgumentException(System.SR.Format(System.SR.InvalidPrinterHandle, hdevmode));
DEVMODEW* ptr = (DEVMODEW*)PInvokeCore.GlobalLock((HGLOBAL)hdevmode);
if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_COLOR))
_color = (ptr->dmColor == DEVMODE_COLOR.DMCOLOR_COLOR);
if (ptr->dmFields.HasFlag(DEVMODE_FIELD_FLAGS.DM_ORIENTATION))
_landscape = ((long)ptr->dmOrientation == 2);
_paperSize = PaperSizeFromMode(ptr);
_paperSource = PaperSourceFromMode(ptr);
_printerResolution = PrinterResolutionFromMode(ptr);
PInvokeCore.GlobalUnlock((HGLOBAL)hdevmode);
}
public override string ToString()
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(78, 7);
defaultInterpolatedStringHandler.AppendLiteral("[");
defaultInterpolatedStringHandler.AppendFormatted("PageSettings");
defaultInterpolatedStringHandler.AppendLiteral(": Color=");
defaultInterpolatedStringHandler.AppendFormatted(Color);
defaultInterpolatedStringHandler.AppendLiteral(", Landscape=");
defaultInterpolatedStringHandler.AppendFormatted(Landscape);
defaultInterpolatedStringHandler.AppendLiteral(", Margins=");
defaultInterpolatedStringHandler.AppendFormatted(Margins);
defaultInterpolatedStringHandler.AppendLiteral(", PaperSize=");
defaultInterpolatedStringHandler.AppendFormatted(PaperSize);
defaultInterpolatedStringHandler.AppendLiteral(", PaperSource=");
defaultInterpolatedStringHandler.AppendFormatted(PaperSource);
defaultInterpolatedStringHandler.AppendLiteral(", PrinterResolution=");
defaultInterpolatedStringHandler.AppendFormatted(PrinterResolution);
defaultInterpolatedStringHandler.AppendLiteral("]");
return defaultInterpolatedStringHandler.ToStringAndClear();
}
}
}