-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPageSetupDialog.cs
More file actions
366 lines (319 loc) · 12.2 KB
/
Copy pathPageSetupDialog.cs
File metadata and controls
366 lines (319 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Drawing.Printing;
using System.Globalization;
using Windows.Win32.UI.Controls.Dialogs;
namespace System.Windows.Forms;
/// <summary>
/// Represents a dialog box that allows users to manipulate page settings,
/// including margins and paper orientation.
/// </summary>
[DefaultProperty(nameof(Document))]
[SRDescription(nameof(SR.DescriptionPageSetupDialog))]
public sealed class PageSetupDialog : CommonDialog
{
// If PrintDocument is not null, pageSettings == printDocument.PageSettings
private PrintDocument? _printDocument;
private PageSettings? _pageSettings;
private PrinterSettings? _printerSettings;
private Margins? _minMargins;
/// <summary>
/// Initializes a new instance of the <see cref="PageSetupDialog"/> class.
/// </summary>
public PageSetupDialog() => Reset();
/// <summary>
/// Gets or sets a value indicating whether the margins section of the dialog box is enabled.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(true)]
[SRDescription(nameof(SR.PSDallowMarginsDescr))]
public bool AllowMargins { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the orientation section of the dialog box (landscape vs. portrait)
/// is enabled.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(true)]
[SRDescription(nameof(SR.PSDallowOrientationDescr))]
public bool AllowOrientation { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the paper section of the dialog box (paper size and paper source)
/// is enabled.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(true)]
[SRDescription(nameof(SR.PSDallowPaperDescr))]
public bool AllowPaper { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the Printer button is enabled.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(true)]
[SRDescription(nameof(SR.PSDallowPrinterDescr))]
public bool AllowPrinter { get; set; }
/// <summary>
/// Gets or sets a value indicating the <see cref="PrintDocument"/> to get page settings from.
/// </summary>
[SRCategory(nameof(SR.CatData))]
[DefaultValue(null)]
[SRDescription(nameof(SR.PDdocumentDescr))]
public PrintDocument? Document
{
get => _printDocument;
set
{
_printDocument = value;
if (_printDocument is not null)
{
_pageSettings = _printDocument.DefaultPageSettings;
_printerSettings = _printDocument.PrinterSettings;
}
}
}
/// <summary>
/// This allows the user to override the current behavior where the Metric is converted to ThousandOfInch even
/// for METRIC MEASUREMENTSYSTEM which returns a HUNDREDSOFMILLIMETER value.
/// </summary>
[DefaultValue(false)]
[SRDescription(nameof(SR.PSDenableMetricDescr))]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public bool EnableMetric { get; set; }
/// <summary>
/// Gets or sets a value indicating the minimum margins the user is allowed to select,
/// in hundredths of an inch.
/// </summary>
[SRCategory(nameof(SR.CatData))]
[SRDescription(nameof(SR.PSDminMarginsDescr))]
public Margins? MinMargins
{
get => _minMargins;
set => _minMargins = value ?? new Margins(0, 0, 0, 0);
}
/// <summary>
/// Gets or sets a value indicating the page settings modified by the dialog box.
/// </summary>
[SRCategory(nameof(SR.CatData))]
[DefaultValue(null)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.PSDpageSettingsDescr))]
public PageSettings? PageSettings
{
get => _pageSettings;
set
{
_pageSettings = value;
_printDocument = null;
}
}
/// <summary>
/// Gets or sets the printer settings the dialog box will modify if the user clicks the Printer button.
/// </summary>
[SRCategory(nameof(SR.CatData))]
[DefaultValue(null)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.PSDprinterSettingsDescr))]
public PrinterSettings? PrinterSettings
{
get => _printerSettings;
set
{
_printerSettings = value;
_printDocument = null;
}
}
/// <summary>
/// Gets or sets a value indicating whether the Help button is visible.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(false)]
[SRDescription(nameof(SR.PSDshowHelpDescr))]
public bool ShowHelp { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the Network button is visible.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(true)]
[SRDescription(nameof(SR.PSDshowNetworkDescr))]
public bool ShowNetwork { get; set; }
private PAGESETUPDLG_FLAGS GetFlags()
{
PAGESETUPDLG_FLAGS flags = PAGESETUPDLG_FLAGS.PSD_ENABLEPAGESETUPHOOK;
if (!AllowMargins)
{
flags |= PAGESETUPDLG_FLAGS.PSD_DISABLEMARGINS;
}
if (!AllowOrientation)
{
flags |= PAGESETUPDLG_FLAGS.PSD_DISABLEORIENTATION;
}
if (!AllowPaper)
{
flags |= PAGESETUPDLG_FLAGS.PSD_DISABLEPAPER;
}
if (!AllowPrinter || _printerSettings is null)
{
flags |= PAGESETUPDLG_FLAGS.PSD_DISABLEPRINTER;
}
if (ShowHelp)
{
flags |= PAGESETUPDLG_FLAGS.PSD_SHOWHELP;
}
if (!ShowNetwork)
{
flags |= PAGESETUPDLG_FLAGS.PSD_NONETWORKBUTTON;
}
if (_minMargins is not null)
{
flags |= PAGESETUPDLG_FLAGS.PSD_MINMARGINS;
}
if (_pageSettings?.Margins is not null)
{
flags |= PAGESETUPDLG_FLAGS.PSD_MARGINS;
}
return flags;
}
/// <summary>
/// Resets all options to their default values.
/// </summary>
public override void Reset()
{
AllowMargins = true;
AllowOrientation = true;
AllowPaper = true;
AllowPrinter = true;
MinMargins = null; // turns into Margin with all zeros
_pageSettings = null;
_printDocument = null;
_printerSettings = null;
ShowHelp = false;
ShowNetwork = true;
}
// The next two methods are for designer support.
private void ResetMinMargins() => MinMargins = null;
/// <summary>
/// Indicates whether the <see cref="MinMargins"/> property should be persisted.
/// </summary>
private bool ShouldSerializeMinMargins() =>
_minMargins is not null
&& (_minMargins.Left != 0
|| _minMargins.Right != 0
|| _minMargins.Top != 0
|| _minMargins.Bottom != 0);
private static void UpdateSettings(
PAGESETUPDLGW data,
PageSettings pageSettings,
PrinterSettings? printerSettings)
{
pageSettings.SetHdevmode(data.hDevMode);
if (printerSettings is not null)
{
printerSettings.SetHdevmode(data.hDevMode);
printerSettings.SetHdevnames(data.hDevNames);
}
Margins newMargins = new()
{
Left = data.rtMargin.left,
Top = data.rtMargin.top,
Right = data.rtMargin.right,
Bottom = data.rtMargin.bottom
};
PrinterUnit fromUnit = ((data.Flags & PAGESETUPDLG_FLAGS.PSD_INHUNDREDTHSOFMILLIMETERS) != 0)
? PrinterUnit.HundredthsOfAMillimeter
: PrinterUnit.ThousandthsOfAnInch;
pageSettings.Margins = PrinterUnitConvert.Convert(newMargins, fromUnit, PrinterUnit.Display);
}
protected override unsafe bool RunDialog(IntPtr hwndOwner)
{
if (_pageSettings is null)
{
throw new ArgumentException(SR.PSDcantShowWithoutPage);
}
PAGESETUPDLGW dialogSettings = new()
{
lStructSize = (uint)sizeof(PAGESETUPDLGW),
Flags = GetFlags(),
hwndOwner = (HWND)hwndOwner,
lpfnPageSetupHook = HookProcFunctionPointer
};
PrinterUnit toUnit = PrinterUnit.ThousandthsOfAnInch;
// EnableMetric allows the users to choose between the AutoConversion or not.
if (EnableMetric)
{
// Take the Units of Measurement while determining the Printer Units.
Span<char> buffer = stackalloc char[2];
int result;
fixed (char* pBuffer = buffer)
{
result = PInvoke.GetLocaleInfoEx(
string.Empty, // empty string == LOCALE_NAME_USER_DEFAULT, matches what the native PageSetupDlg dialog uses
PInvoke.LOCALE_IMEASURE,
pBuffer,
buffer.Length);
}
if (result > 0)
{
ReadOnlySpan<char> actualValue = buffer[..(result - 1)];
if (int.TryParse(actualValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value) && value == 0)
{
toUnit = PrinterUnit.HundredthsOfAMillimeter;
}
}
// Explicitly pass the unit flag to the native dialog so it operates in the unit selected
// by toUnit. This keeps the values written into rtMargin/rtMinMargin below and the values read
// back in UpdateSettings in the same unit, regardless of how system-level and user-level locale
// settings may differ.
if (toUnit == PrinterUnit.HundredthsOfAMillimeter)
{
dialogSettings.Flags |= PAGESETUPDLG_FLAGS.PSD_INHUNDREDTHSOFMILLIMETERS;
}
else
{
dialogSettings.Flags |= PAGESETUPDLG_FLAGS.PSD_INTHOUSANDTHSOFINCHES;
}
}
if (MinMargins is not null)
{
Margins margins = PrinterUnitConvert.Convert(MinMargins, PrinterUnit.Display, toUnit);
dialogSettings.rtMinMargin.left = margins.Left;
dialogSettings.rtMinMargin.top = margins.Top;
dialogSettings.rtMinMargin.right = margins.Right;
dialogSettings.rtMinMargin.bottom = margins.Bottom;
}
if (_pageSettings.Margins is not null)
{
Margins margins = PrinterUnitConvert.Convert(_pageSettings.Margins, PrinterUnit.Display, toUnit);
dialogSettings.rtMargin.left = margins.Left;
dialogSettings.rtMargin.top = margins.Top;
dialogSettings.rtMargin.right = margins.Right;
dialogSettings.rtMargin.bottom = margins.Bottom;
}
// Ensure that the margins are >= minMargins.
// This is a requirement of the PAGESETUPDLG structure.
dialogSettings.rtMargin.left = Math.Max(dialogSettings.rtMargin.left, dialogSettings.rtMinMargin.left);
dialogSettings.rtMargin.top = Math.Max(dialogSettings.rtMargin.top, dialogSettings.rtMinMargin.top);
dialogSettings.rtMargin.right = Math.Max(dialogSettings.rtMargin.right, dialogSettings.rtMinMargin.right);
dialogSettings.rtMargin.bottom = Math.Max(dialogSettings.rtMargin.bottom, dialogSettings.rtMinMargin.bottom);
PrinterSettings printer = _printerSettings ?? _pageSettings.PrinterSettings;
dialogSettings.hDevMode = (HGLOBAL)printer.GetHdevmode(_pageSettings);
dialogSettings.hDevNames = (HGLOBAL)printer.GetHdevnames();
try
{
if (!PInvoke.PageSetupDlg(&dialogSettings))
{
return false;
}
// PrinterSettings, not printer
UpdateSettings(dialogSettings, _pageSettings, _printerSettings);
return true;
}
finally
{
PInvokeCore.GlobalFree(dialogSettings.hDevMode);
PInvokeCore.GlobalFree(dialogSettings.hDevNames);
}
}
}