forked from dotnet/winforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintPreviewControl.cs
More file actions
1087 lines (922 loc) · 32.7 KB
/
PrintPreviewControl.cs
File metadata and controls
1087 lines (922 loc) · 32.7 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using Windows.Win32.UI.Accessibility;
namespace System.Windows.Forms;
/// <summary>
/// The raw "preview" part of print previewing, without any dialogs or buttons. Most <see cref="PrintPreviewControl"/>
/// objects are found on <see cref="PrintPreviewDialog"/> objects, but they don't have to be.
/// </summary>
[DefaultProperty(nameof(Document))]
[SRDescription(nameof(SR.DescriptionPrintPreviewControl))]
public partial class PrintPreviewControl : Control
{
private const int ScrollSmallChange = 5;
private const double DefaultZoom = .3;
// Spacing per page, in mm
private const int Border = 10;
private static readonly object s_startPageChangedEvent = new();
private PrintDocument? _document;
private PreviewPageInfo[]? _pageInfo; // null if needs refreshing
private int _startPage; // 0-based
private int _rows = 1;
private int _columns = 1;
private bool _autoZoom = true;
private Size _virtualSize = new(1, 1);
private Point _position = new(0, 0);
private readonly int _focusHOffset = SystemInformation.HorizontalFocusThickness;
private readonly int _focusVOffset = SystemInformation.VerticalFocusThickness;
private readonly HorizontalScrollBar _hScrollBar;
private readonly VerticalScrollBar _vScrollBar;
private bool _scrollLayoutPending;
// The following are all computed by ComputeLayout
private bool _layoutOk;
// 100ths of an inch, not pixels
private Size _imageSize = Size.Empty;
private Point _screenDPI = Point.Empty;
private double _zoom = DefaultZoom;
private bool _pageInfoCalcPending;
private bool _exceptionPrinting;
/// <summary>
/// Initializes a new instance of the <see cref="PrintPreviewControl"/> class.
/// </summary>
public PrintPreviewControl()
{
ResetBackColor();
ResetForeColor();
Size = new Size(100, 100);
SetStyle(ControlStyles.ResizeRedraw, false);
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
TabStop = false;
_hScrollBar = new()
{
AccessibleName = SR.HScrollBarDefaultAccessibleName,
Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right,
Left = _focusHOffset,
SmallChange = ScrollSmallChange,
RightToLeft = RightToLeft.No,
Visible = false,
TabStop = false
};
_hScrollBar.ValueChanged += scrollBar_ValueChanged;
Controls.Add(_hScrollBar);
_vScrollBar = new()
{
AccessibleName = SR.VScrollBarDefaultAccessibleName,
Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom,
SmallChange = ScrollSmallChange,
Top = _focusVOffset,
Visible = false,
TabStop = false
};
_vScrollBar.ValueChanged += scrollBar_ValueChanged;
Controls.Add(_vScrollBar);
}
/// <summary>
/// Gets or sets a value indicating whether printing uses the anti-aliasing features of the operating system.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(false)]
[SRDescription(nameof(SR.PrintPreviewAntiAliasDescr))]
public bool UseAntiAlias
{
get; set;
}
/// <summary>
/// Gets or sets a value indicating whether resizing the control or changing the number of pages shown
/// automatically adjusts the <see cref="Zoom"/> property.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(true)]
[SRDescription(nameof(SR.PrintPreviewAutoZoomDescr))]
public bool AutoZoom
{
get => _autoZoom;
set
{
if (_autoZoom != value)
{
_autoZoom = value;
InvalidateLayout();
}
}
}
/// <summary>
/// Gets or sets a value indicating how large the pages will appear.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[SRDescription(nameof(SR.PrintPreviewZoomDescr))]
[DefaultValue(DefaultZoom)]
public double Zoom
{
get => _zoom;
set
{
if (value <= 0)
{
throw new ArgumentException(SR.PrintPreviewControlZoomNegative);
}
_autoZoom = false;
_zoom = value;
InvalidateLayout();
}
}
/// <summary>
/// Gets or sets a value indicating the document to preview.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[DefaultValue(null)]
[SRDescription(nameof(SR.PrintPreviewDocumentDescr))]
public PrintDocument? Document
{
get => _document;
set
{
_document = value;
InvalidatePreview();
}
}
/// <summary>
/// Gets or sets the number of pages displayed vertically down the screen.
/// </summary>
[DefaultValue(1)]
[SRDescription(nameof(SR.PrintPreviewRowsDescr))]
[SRCategory(nameof(SR.CatBehavior))]
public int Rows
{
get => _rows;
set
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
_rows = value;
InvalidateLayout();
}
}
/// <summary>
/// Gets or sets the number of pages displayed horizontally across the screen.
/// </summary>
[DefaultValue(1)]
[SRCategory(nameof(SR.CatLayout))]
[SRDescription(nameof(SR.PrintPreviewColumnsDescr))]
public int Columns
{
get => _columns;
set
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value);
_columns = value;
InvalidateLayout();
}
}
/// <summary>
/// Gets or sets the page number of the upper left page.
/// </summary>
[DefaultValue(0)]
[SRDescription(nameof(SR.PrintPreviewStartPageDescr))]
[SRCategory(nameof(SR.CatBehavior))]
public int StartPage
{
get
{
int value = _startPage;
if (_pageInfo is not null)
{
value = Math.Min(value, _pageInfo.Length - (_rows * _columns));
}
value = Math.Max(value, 0);
return value;
}
set
{
ArgumentOutOfRangeException.ThrowIfNegative(value);
int oldValue = StartPage;
_startPage = value;
if (oldValue != _startPage)
{
InvalidateLayout();
OnStartPageChanged(EventArgs.Empty);
}
}
}
[SRCategory(nameof(SR.CatPropertyChanged))]
[SRDescription(nameof(SR.RadioButtonOnStartPageChangedDescr))]
public event EventHandler? StartPageChanged
{
add => Events.AddHandler(s_startPageChangedEvent, value);
remove => Events.RemoveHandler(s_startPageChangedEvent, value);
}
protected virtual void OnStartPageChanged(EventArgs e)
{
if (Events[s_startPageChangedEvent] is EventHandler eh)
{
eh(this, e);
}
}
/// <summary>
/// Gets or sets a value indicating whether control's elements are aligned to support locales using right-to-left fonts.
/// </summary>
[SRCategory(nameof(SR.CatAppearance))]
[Localizable(true)]
[AmbientValue(RightToLeft.Inherit)]
[SRDescription(nameof(SR.ControlRightToLeftDescr))]
public override RightToLeft RightToLeft
{
get => base.RightToLeft;
set
{
base.RightToLeft = value;
InvalidatePreview();
}
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[AllowNull]
public override string Text
{
get => base.Text;
set => base.Text = value;
}
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new event EventHandler? TextChanged
{
add => base.TextChanged += value;
remove => base.TextChanged -= value;
}
[DefaultValue(false)]
[DispId(PInvokeCore.DISPID_TABSTOP)]
public new bool TabStop
{
get => base.TabStop;
set => base.TabStop = value;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public override void ResetBackColor() => BackColor = SystemColors.AppWorkspace;
internal override bool ShouldSerializeBackColor() => !BackColor.Equals(SystemColors.AppWorkspace);
[EditorBrowsable(EditorBrowsableState.Never)]
public override void ResetForeColor() => ForeColor = Color.White;
internal override bool ShouldSerializeForeColor() => !ForeColor.Equals(Color.White);
internal override bool SupportsUiaProviders => true;
protected override AccessibleObject CreateAccessibilityInstance()
=> new PrintPreviewControlAccessibleObject(this);
protected override void OnResize(EventArgs eventargs)
{
if (!IsHandleCreated)
{
return;
}
InvalidateLayout();
base.OnResize(eventargs);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (!Focused)
{
Focus();
}
base.OnMouseDown(e);
}
protected override void OnGotFocus(EventArgs e)
{
Invalidate();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
Invalidate();
base.OnLostFocus(e);
}
protected override void OnPaintBackground(PaintEventArgs e) => PaintTransparentBackground(e, ClientRectangle);
protected override void OnPaint(PaintEventArgs pevent)
{
bool isHighContrast = SystemInformation.HighContrast;
Color backColor = GetBackColor(isHighContrast);
using var backBrush = backColor.GetCachedSolidBrushScope();
PaintResizeBox(pevent);
PaintFocus(pevent, isHighContrast);
if (_pageInfo is null || _pageInfo.Length == 0)
{
Rectangle rect = InsideRectangle;
pevent.Graphics.FillRectangle(backBrush, rect);
if (_pageInfo is not null || _exceptionPrinting)
{
DrawMessage(pevent.Graphics, rect, _exceptionPrinting);
}
else
{
BeginInvoke(new MethodInvoker(CalculatePageInfo));
}
}
else
{
if (!_layoutOk)
{
ComputeLayout();
}
DrawPages(pevent.Graphics, InsideRectangle, _pageInfo, backBrush);
}
base.OnPaint(pevent); // raise paint event
}
/// <summary>
/// How big the control would be if the screen was infinitely large.
/// </summary>
[SRCategory(nameof(SR.CatLayout))]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.ControlWithScrollbarsVirtualSizeDescr))]
private Size VirtualSize
{
get => _virtualSize;
set
{
SetVirtualSizeNoInvalidate(value);
Invalidate();
}
}
/// <summary>
/// The virtual coordinate of the upper left visible pixel.
/// </summary>
[SRCategory(nameof(SR.CatLayout))]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.ControlWithScrollbarsPositionDescr))]
private Point Position
{
get => _position;
set
{
SetPositionNoInvalidate(value);
Invalidate();
}
}
/// <summary>
/// Equal to the vertical percentage of the entire control that is currently viewable.
/// </summary>
internal double VerticalViewSize => InsideRectangle.Height * 100.0 / VirtualSize.Height;
/// <summary>
/// Equal to the horizontal percentage of the entire control that is currently viewable.
/// </summary>
internal double HorizontalViewSize => InsideRectangle.Width * 100.0 / VirtualSize.Width;
private Rectangle InnerClientRectangle
{
get
{
Rectangle rect = ClientRectangle;
rect.Inflate(-_focusHOffset, -_focusVOffset);
rect.Width = Math.Max(0, rect.Width - 1);
rect.Height = Math.Max(0, rect.Height - 1);
return rect;
}
}
private Rectangle InsideRectangle
{
get
{
Rectangle rect = InnerClientRectangle;
if (_hScrollBar.Visible)
{
rect.Height -= _hScrollBar.Height;
}
if (_vScrollBar.Visible)
{
rect.Width -= _vScrollBar.Width;
if (RightToLeft == RightToLeft.Yes)
{
rect.X += _vScrollBar.Width;
}
}
return rect;
}
}
private Rectangle FocusRectangle => new(0, 0, Width - 1, Height - 1);
private Rectangle ResizeBoxRectangle => new(_vScrollBar.Left, _hScrollBar.Top, _vScrollBar.Width, _hScrollBar.Height);
// This function computes everything in terms of physical size (millimeters), not pixels
private void ComputeLayout()
{
Debug.Assert(_pageInfo is not null, "Must call ComputePreview first");
_layoutOk = true;
if (_pageInfo.Length == 0)
{
return;
}
using GetDcScope hdc = new(HWND);
_screenDPI = new Point(
PInvokeCore.GetDeviceCaps(hdc, GET_DEVICE_CAPS_INDEX.LOGPIXELSX),
PInvokeCore.GetDeviceCaps(hdc, GET_DEVICE_CAPS_INDEX.LOGPIXELSY));
Size pageSize = _pageInfo[StartPage].PhysicalSize;
Size controlPhysicalSize = PixelsToPhysical(Size, _screenDPI);
if (_autoZoom)
{
double zoomX = ((double)controlPhysicalSize.Width - Border * (_columns + 1)) / (_columns * pageSize.Width);
double zoomY = ((double)controlPhysicalSize.Height - Border * (_rows + 1)) / (_rows * pageSize.Height);
_zoom = Math.Min(zoomX, zoomY);
}
_imageSize = new Size((int)(_zoom * pageSize.Width), (int)(_zoom * pageSize.Height));
int virtualX = (_imageSize.Width * _columns) + Border * (_columns + 1);
int virtualY = (_imageSize.Height * _rows) + Border * (_rows + 1);
SetVirtualSizeNoInvalidate(PhysicalToPixels(new Size(virtualX, virtualY), _screenDPI));
}
// "Prints" the document to memory
private void ComputePreview()
{
int oldStart = StartPage;
if (_document is null)
{
_pageInfo = [];
}
else
{
PrintController oldController = _document.PrintController;
PreviewPrintController previewController = new PreviewPrintController
{
UseAntiAlias = UseAntiAlias
};
_document.PrintController = new PrintControllerWithStatusDialog(
previewController,
SR.PrintControllerWithStatusDialog_DialogTitlePreview);
_document.Print();
_pageInfo = previewController.GetPreviewPageInfo();
Debug.Assert(_pageInfo is not null, $"{nameof(PreviewPrintController)} did not give us preview info.");
_document.PrintController = oldController;
}
if (oldStart != StartPage)
{
OnStartPageChanged(EventArgs.Empty);
}
}
// Recomputes the sizes and positions of pages without forcing a new "preview print"
private void InvalidateLayout()
{
if (!IsHandleCreated)
{
return;
}
_layoutOk = false;
LayoutScrollBars();
Invalidate();
}
/// <summary>
/// Refreshes the preview of the document.
/// </summary>
public void InvalidatePreview()
{
if (!IsHandleCreated)
{
return;
}
_pageInfo = null;
_exceptionPrinting = false;
SetVirtualSizeNoInvalidate(Size.Empty);
InvalidateLayout();
}
private void CalculatePageInfo()
{
if (_pageInfoCalcPending)
{
return;
}
_pageInfoCalcPending = true;
try
{
if (_pageInfo is null)
{
try
{
ComputePreview();
}
catch
{
_exceptionPrinting = true;
throw;
}
finally
{
Invalidate();
}
}
}
finally
{
_pageInfoCalcPending = false;
}
}
private void DrawMessage(Graphics g, Rectangle rect, bool isExceptionPrinting)
{
Color brushColor = ShouldSerializeForeColor() ? ForeColor : SystemColors.ControlText;
if (SystemInformation.HighContrast && Parent is Control parent)
{
brushColor = parent.BackColor;
}
using var brush = brushColor.GetCachedSolidBrushScope();
using StringFormat format = new()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
string message = isExceptionPrinting ? SR.PrintPreviewExceptionPrinting : SR.PrintPreviewNoPages;
g.DrawString(message, Font, brush, rect, format);
}
private void DrawPages(Graphics g, Rectangle rect, PreviewPageInfo[] pages, Brush backBrush)
{
using GraphicsClipScope clipScope = new(g);
g.SetClip(rect);
_position.X = _hScrollBar.Value;
_position.Y = _vScrollBar.Value;
Size controlPhysicalSize = PixelsToPhysical(rect.Size, _screenDPI);
// center pages on screen if small enough
Point offset = new(
Math.Max(0, (rect.Width - _virtualSize.Width) / 2),
Math.Max(0, (rect.Height - _virtualSize.Height) / 2));
offset.X -= Position.X;
offset.Y -= Position.Y;
int borderPixelsX = PhysicalToPixels(Border, _screenDPI.X);
int borderPixelsY = PhysicalToPixels(Border, _screenDPI.Y);
Rectangle[] pageRenderArea = new Rectangle[_rows * _columns];
Point lastImageSize = Point.Empty;
int maxImageHeight = 0;
using (new GraphicsClipScope(g))
{
for (int row = 0; row < _rows; row++)
{
// Initialize our LastImageSize variable...
lastImageSize.X = 0;
lastImageSize.Y = maxImageHeight * row;
for (int column = 0; column < _columns; column++)
{
int imageIndex = StartPage + column + row * _columns;
if (imageIndex < pages.Length)
{
Size pageSize = pages[imageIndex].PhysicalSize;
if (_autoZoom)
{
double zoomX = ((double)controlPhysicalSize.Width - Border * (_columns + 1))
/ (_columns * pageSize.Width);
double zoomY = ((double)controlPhysicalSize.Height - Border * (_rows + 1))
/ (_rows * pageSize.Height);
_zoom = Math.Min(zoomX, zoomY);
}
_imageSize = new Size((int)(_zoom * pageSize.Width), (int)(_zoom * pageSize.Height));
Size imagePixels = PhysicalToPixels(_imageSize, _screenDPI);
int x = offset.X + borderPixelsX * (column + 1) + lastImageSize.X;
int y = offset.Y + borderPixelsY * (row + 1) + lastImageSize.Y;
lastImageSize.X += imagePixels.Width;
// The Height is the Max of any PageHeight..
maxImageHeight = Math.Max(maxImageHeight, imagePixels.Height);
pageRenderArea[imageIndex - StartPage] = new Rectangle(x, y, imagePixels.Width, imagePixels.Height);
g.ExcludeClip(pageRenderArea[imageIndex - StartPage]);
}
}
}
g.FillRectangle(backBrush, rect);
}
for (int i = 0; i < pageRenderArea.Length; i++)
{
if (i + StartPage < pages.Length)
{
Rectangle box = pageRenderArea[i];
g.DrawRectangle(Pens.Black, box);
using (var brush = ForeColor.GetCachedSolidBrushScope())
{
g.FillRectangle(brush, box);
}
box.Inflate(-1, -1);
if (pages[i + StartPage].Image is not null)
{
g.DrawImage(pages[i + StartPage].Image, box);
}
box.Width--;
box.Height--;
g.DrawRectangle(Pens.Black, box);
}
}
}
private void PaintResizeBox(PaintEventArgs e)
{
if (!_hScrollBar.Visible || !_vScrollBar.Visible)
{
return;
}
e.Graphics.FillRectangle(SystemBrushes.Control, ResizeBoxRectangle);
}
private void PaintFocus(PaintEventArgs e, bool isHighContrast)
{
Rectangle focusRect = FocusRectangle;
if (!e.ClipRectangle.Contains(focusRect) || !Focused || !ShowFocusCues)
{
return;
}
if (isHighContrast)
{
ControlPaint.DrawHighContrastFocusRectangle(e.Graphics, focusRect, SystemColors.ControlText);
}
else
{
ControlPaint.DrawFocusRectangle(e.Graphics, focusRect);
}
}
/// <summary>
/// Gets back color respectively to the High Contrast theme is applied or not
/// and taking into account saved custom back color.
/// </summary>
/// <param name="isHighContract">Indicates whether High Contrast theme is applied or not.</param>
/// <returns>
/// Standard back color for PrintPreview control in standard theme (1),
/// contrasted color if there is High Contrast theme applied (2) and
/// custom color if this is set irrespectively to HC or not HC mode (3).
/// </returns>
private Color GetBackColor(bool isHighContract)
{
return (isHighContract && !ShouldSerializeBackColor()) ? SystemColors.ControlDarkDark : BackColor;
}
private static int PixelsToPhysical(int pixels, int dpi) => (int)(pixels * 100.0 / dpi);
private static Size PixelsToPhysical(Size pixels, Point dpi) =>
new(PixelsToPhysical(pixels.Width, dpi.X), PixelsToPhysical(pixels.Height, dpi.Y));
private static int PhysicalToPixels(int physicalSize, int dpi) =>
(int)(physicalSize * dpi / 100.0);
private static Size PhysicalToPixels(Size physical, Point dpi) =>
new(PhysicalToPixels(physical.Width, dpi.X), PhysicalToPixels(physical.Height, dpi.Y));
private void SetPositionNoInvalidate(Point value)
{
_scrollLayoutPending = true;
_hScrollBar.Value = Math.Clamp(value.X, _hScrollBar.Minimum, _hScrollBar.Maximum);
_vScrollBar.Value = Math.Clamp(value.Y, _vScrollBar.Minimum, _vScrollBar.Maximum);
_scrollLayoutPending = false;
}
internal void SetVirtualSizeNoInvalidate(Size value)
{
if (!IsHandleCreated)
{
return;
}
_virtualSize = value;
LayoutScrollBars();
}
private void LayoutScrollBars()
{
if (!IsHandleCreated)
{
return;
}
_scrollLayoutPending = true;
SuspendLayout();
Rectangle availableRect = InnerClientRectangle;
(bool horizontalScrollNeeded, bool verticalScrollNeeded) = IsScrollNeeded(availableRect.Size);
bool scrollBarsVisibilityChanged = (_vScrollBar.Visible ^ verticalScrollNeeded) || (_hScrollBar.Visible ^ horizontalScrollNeeded);
_hScrollBar.Visible = horizontalScrollNeeded;
if (horizontalScrollNeeded)
{
_hScrollBar.Top = availableRect.Bottom - _hScrollBar.Height;
_hScrollBar.Width = availableRect.Width - (verticalScrollNeeded ? _vScrollBar.Width : 0);
AdjustScroll(
_hScrollBar,
virtualDimension: _virtualSize.Width,
displayDimension: availableRect.Width,
offset: (verticalScrollNeeded ? _vScrollBar.Width : 0));
}
else if (_hScrollBar.Value > 0)
{
_hScrollBar.Value = 0;
}
_vScrollBar.Visible = verticalScrollNeeded;
if (verticalScrollNeeded)
{
_vScrollBar.Height = availableRect.Height - (horizontalScrollNeeded ? _hScrollBar.Height : 0);
if (RightToLeft == RightToLeft.Yes)
{
_vScrollBar.Left = availableRect.Left;
_vScrollBar.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom;
}
else
{
_vScrollBar.Left = availableRect.Right - _vScrollBar.Width;
_vScrollBar.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
}
AdjustScroll(
_vScrollBar,
virtualDimension: _virtualSize.Height,
displayDimension: availableRect.Height,
offset: (horizontalScrollNeeded ? _hScrollBar.Height : 0));
}
else if (_vScrollBar.Value > 0)
{
_vScrollBar.Value = 0;
}
ResumeLayout(true);
_scrollLayoutPending = false;
if (scrollBarsVisibilityChanged
&& IsAccessibilityObjectCreated
&& AccessibilityObject is PrintPreviewControlAccessibleObject ao)
{
ao.RaiseStructureChangedEvent(StructureChangeType.StructureChangeType_ChildrenInvalidated, []);
}
(bool horizontal, bool vertical) IsScrollNeeded(Size displaySize)
{
bool horizontal = _virtualSize.Width > displaySize.Width && displaySize.Width > _vScrollBar.Width;
bool vertical = _virtualSize.Height > displaySize.Height && displaySize.Height > _hScrollBar.Height;
if (!horizontal && vertical)
{
horizontal = _virtualSize.Width > (displaySize.Width - _vScrollBar.Width);
}
if (!vertical && horizontal)
{
vertical = _virtualSize.Height > (displaySize.Height - _hScrollBar.Height);
}
return (horizontal, vertical);
}
void AdjustScroll(ScrollBar scrollBar, int virtualDimension, int displayDimension, int offset)
{
int oldLargeChange = scrollBar.LargeChange;
scrollBar.Maximum = virtualDimension;
scrollBar.LargeChange = displayDimension - offset;
if (scrollBar.Value > 0)
{
int diff = scrollBar.LargeChange - oldLargeChange;
if (scrollBar.Value >= diff)
{
scrollBar.Value -= diff;
}
}
}
}
private void scrollBar_ValueChanged(object? sender, EventArgs e)
{
if (_scrollLayoutPending)
{
return;
}
Invalidate(InsideRectangle);
}
/// <summary>
/// Handles the WM_KEYDOWN message.
/// </summary>
private void WmKeyDown(ref Message msg)
{
Keys keyData = (Keys)(nint)msg.WParamInternal | ModifierKeys;
Point locPos = Position;
int pos;
int maxPos;
bool scrollVisible = _vScrollBar.Visible || _hScrollBar.Visible;
bool vertical = _vScrollBar.Visible;
int largeChange = vertical ? _vScrollBar.LargeChange : _hScrollBar.LargeChange;
switch (keyData & Keys.KeyCode)
{
case Keys.PageUp:
if ((keyData & Keys.Modifiers) == Keys.Control)
{
if (scrollVisible)
{
pos = vertical ? locPos.Y : locPos.X;
if (pos > largeChange)
{
pos -= largeChange;
}
else
{
pos = 0;
}
if (vertical)
{
locPos.Y = pos;
}
else
{
locPos.X = pos;
}
Position = locPos;
}
}
else if (StartPage > 0)
{
StartPage--;
}
break;
case Keys.PageDown:
if ((keyData & Keys.Modifiers) == Keys.Control)
{
if (scrollVisible)
{
pos = vertical ? locPos.Y : locPos.X;
maxPos = vertical ? _vScrollBar.Maximum : _hScrollBar.Maximum;
if (pos < maxPos - largeChange)
{
pos += largeChange;
}
else
{
pos = maxPos;
}
if (vertical)
{
locPos.Y = pos;
}
else
{
locPos.X = pos;
}
Position = locPos;
}
}
else if (_pageInfo is not null && StartPage < _pageInfo.Length)
{
StartPage++;
}
break;
case Keys.Home:
if ((keyData & Keys.Modifiers) == Keys.Control)
{
StartPage = 0;
}
break;
case Keys.End:
if (_pageInfo is not null && (keyData & Keys.Modifiers) == Keys.Control)
{
StartPage = _pageInfo.Length;
}
break;
case Keys.Up:
if (_vScrollBar.Visible)
{
pos = locPos.Y;
if (pos > _vScrollBar.SmallChange)