-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathForm.cs
More file actions
6714 lines (5951 loc) · 229 KB
/
Copy pathForm.cs
File metadata and controls
6714 lines (5951 loc) · 229 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.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Forms.Layout;
using System.Windows.Forms.VisualStyles;
using Windows.Win32.System.Threading;
using static Interop;
namespace System.Windows.Forms;
/// <summary>
/// Represents a window or dialog box that makes up an application's user interface.
/// </summary>
[ToolboxItemFilter("System.Windows.Forms.Control.TopLevel")]
[ToolboxItem(false)]
[DesignTimeVisible(false)]
[Designer($"System.Windows.Forms.Design.FormDocumentDesigner, {AssemblyRef.SystemDesign}", typeof(IRootDesigner))]
[DefaultEvent(nameof(Load))]
[InitializationEvent(nameof(Load))]
[DesignerCategory("Form")]
public partial class Form : ContainerControl
{
private static readonly object EVENT_ACTIVATED = new();
private static readonly object EVENT_CLOSING = new();
private static readonly object EVENT_CLOSED = new();
private static readonly object EVENT_FORMCLOSING = new();
private static readonly object EVENT_FORMCLOSED = new();
private static readonly object EVENT_DEACTIVATE = new();
private static readonly object EVENT_LOAD = new();
private static readonly object EVENT_MDI_CHILD_ACTIVATE = new();
private static readonly object EVENT_INPUTLANGCHANGE = new();
private static readonly object EVENT_INPUTLANGCHANGEREQUEST = new();
private static readonly object EVENT_MENUSTART = new();
private static readonly object EVENT_MENUCOMPLETE = new();
private static readonly object EVENT_MAXIMUMSIZECHANGED = new();
private static readonly object EVENT_MINIMUMSIZECHANGED = new();
private static readonly object EVENT_HELPBUTTONCLICKED = new();
private static readonly object EVENT_SHOWN = new();
private static readonly object EVENT_RESIZEBEGIN = new();
private static readonly object EVENT_RESIZEEND = new();
private static readonly object EVENT_RIGHTTOLEFTLAYOUTCHANGED = new();
private static readonly object EVENT_DPI_CHANGED = new();
//
// The following flags should be used with formState[..] not formStateEx[..]
// Don't add any more sections to this vector, it is already full.
//
private static readonly BitVector32.Section FormStateAllowTransparency = BitVector32.CreateSection(1);
private static readonly BitVector32.Section FormStateBorderStyle = BitVector32.CreateSection(6, FormStateAllowTransparency);
private static readonly BitVector32.Section FormStateTaskBar = BitVector32.CreateSection(1, FormStateBorderStyle);
private static readonly BitVector32.Section FormStateControlBox = BitVector32.CreateSection(1, FormStateTaskBar);
private static readonly BitVector32.Section FormStateKeyPreview = BitVector32.CreateSection(1, FormStateControlBox);
private static readonly BitVector32.Section FormStateLayered = BitVector32.CreateSection(1, FormStateKeyPreview);
private static readonly BitVector32.Section FormStateMaximizeBox = BitVector32.CreateSection(1, FormStateLayered);
private static readonly BitVector32.Section FormStateMinimizeBox = BitVector32.CreateSection(1, FormStateMaximizeBox);
private static readonly BitVector32.Section FormStateHelpButton = BitVector32.CreateSection(1, FormStateMinimizeBox);
private static readonly BitVector32.Section FormStateStartPos = BitVector32.CreateSection(4, FormStateHelpButton);
private static readonly BitVector32.Section FormStateWindowState = BitVector32.CreateSection(2, FormStateStartPos);
private static readonly BitVector32.Section FormStateShowWindowOnCreate = BitVector32.CreateSection(1, FormStateWindowState);
private static readonly BitVector32.Section FormStateAutoScaling = BitVector32.CreateSection(1, FormStateShowWindowOnCreate);
private static readonly BitVector32.Section FormStateSetClientSize = BitVector32.CreateSection(1, FormStateAutoScaling);
private static readonly BitVector32.Section FormStateTopMost = BitVector32.CreateSection(1, FormStateSetClientSize);
private static readonly BitVector32.Section FormStateSWCalled = BitVector32.CreateSection(1, FormStateTopMost);
private static readonly BitVector32.Section FormStateMdiChildMax = BitVector32.CreateSection(1, FormStateSWCalled);
private static readonly BitVector32.Section FormStateRenderSizeGrip = BitVector32.CreateSection(1, FormStateMdiChildMax);
private static readonly BitVector32.Section FormStateSizeGripStyle = BitVector32.CreateSection(2, FormStateRenderSizeGrip);
private static readonly BitVector32.Section FormStateIsWindowActivated = BitVector32.CreateSection(1, FormStateSizeGripStyle);
private static readonly BitVector32.Section FormStateIsTextEmpty = BitVector32.CreateSection(1, FormStateIsWindowActivated);
private static readonly BitVector32.Section FormStateIsActive = BitVector32.CreateSection(1, FormStateIsTextEmpty);
private static readonly BitVector32.Section FormStateIconSet = BitVector32.CreateSection(1, FormStateIsActive);
// The following flags should be used with formStateEx[...] not formState[..]
private static readonly BitVector32.Section FormStateExCalledClosing = BitVector32.CreateSection(1);
private static readonly BitVector32.Section FormStateExUpdateMenuHandlesSuspendCount = BitVector32.CreateSection(8, FormStateExCalledClosing);
private static readonly BitVector32.Section FormStateExUpdateMenuHandlesDeferred = BitVector32.CreateSection(1, FormStateExUpdateMenuHandlesSuspendCount);
private static readonly BitVector32.Section FormStateExUseMdiChildProc = BitVector32.CreateSection(1, FormStateExUpdateMenuHandlesDeferred);
private static readonly BitVector32.Section FormStateExCalledOnLoad = BitVector32.CreateSection(1, FormStateExUseMdiChildProc);
private static readonly BitVector32.Section FormStateExCalledMakeVisible = BitVector32.CreateSection(1, FormStateExCalledOnLoad);
private static readonly BitVector32.Section FormStateExCalledCreateControl = BitVector32.CreateSection(1, FormStateExCalledMakeVisible);
private static readonly BitVector32.Section FormStateExAutoSize = BitVector32.CreateSection(1, FormStateExCalledCreateControl);
private static readonly BitVector32.Section FormStateExInUpdateMdiControlStrip = BitVector32.CreateSection(1, FormStateExAutoSize);
private static readonly BitVector32.Section FormStateExShowIcon = BitVector32.CreateSection(1, FormStateExInUpdateMdiControlStrip);
private static readonly BitVector32.Section FormStateExMnemonicProcessed = BitVector32.CreateSection(1, FormStateExShowIcon);
private static readonly BitVector32.Section FormStateExInScale = BitVector32.CreateSection(1, FormStateExMnemonicProcessed);
private static readonly BitVector32.Section FormStateExInModalSizingLoop = BitVector32.CreateSection(1, FormStateExInScale);
private static readonly BitVector32.Section FormStateExSettingAutoScale = BitVector32.CreateSection(1, FormStateExInModalSizingLoop);
private static readonly BitVector32.Section FormStateExWindowBoundsWidthIsClientSize = BitVector32.CreateSection(1, FormStateExSettingAutoScale);
private static readonly BitVector32.Section FormStateExWindowBoundsHeightIsClientSize = BitVector32.CreateSection(1, FormStateExWindowBoundsWidthIsClientSize);
private static readonly BitVector32.Section FormStateExWindowClosing = BitVector32.CreateSection(1, FormStateExWindowBoundsHeightIsClientSize);
private const int SizeGripSize = 16;
private static Icon? defaultIcon;
private static readonly object internalSyncObject = new();
// Property store keys for properties. The property store allocates most efficiently
// in groups of four, so we try to lump properties in groups of four based on how
// likely they are going to be used in a group.
//
private static readonly int PropAcceptButton = PropertyStore.CreateKey();
private static readonly int PropCancelButton = PropertyStore.CreateKey();
private static readonly int PropDefaultButton = PropertyStore.CreateKey();
private static readonly int PropDialogOwner = PropertyStore.CreateKey();
private static readonly int PropOwner = PropertyStore.CreateKey();
private static readonly int PropOwnedForms = PropertyStore.CreateKey();
private static readonly int PropMaximizedBounds = PropertyStore.CreateKey();
private static readonly int PropOwnedFormsCount = PropertyStore.CreateKey();
private static readonly int PropMinTrackSizeWidth = PropertyStore.CreateKey();
private static readonly int PropMinTrackSizeHeight = PropertyStore.CreateKey();
private static readonly int PropMaxTrackSizeWidth = PropertyStore.CreateKey();
private static readonly int PropMaxTrackSizeHeight = PropertyStore.CreateKey();
private static readonly int PropFormMdiParent = PropertyStore.CreateKey();
private static readonly int PropActiveMdiChild = PropertyStore.CreateKey();
private static readonly int PropFormerlyActiveMdiChild = PropertyStore.CreateKey();
private static readonly int PropMdiChildFocusable = PropertyStore.CreateKey();
private static readonly int PropDummyMdiMenu = PropertyStore.CreateKey();
private static readonly int PropMainMenuStrip = PropertyStore.CreateKey();
private static readonly int PropMdiWindowListStrip = PropertyStore.CreateKey();
private static readonly int PropMdiControlStrip = PropertyStore.CreateKey();
private static readonly int PropOpacity = PropertyStore.CreateKey();
private static readonly int PropTransparencyKey = PropertyStore.CreateKey();
// Form per instance members
// Note: Do not add anything to this list unless absolutely necessary.
private BitVector32 _formState = new(0x21338); // magic value... all the defaults... see the ctor for details...
private BitVector32 _formStateEx;
private Icon? _icon;
private Icon? _smallIcon;
private Size _autoScaleBaseSize = Size.Empty;
private Size _minAutoSize = Size.Empty;
private Rectangle _restoredWindowBounds = new(-1, -1, -1, -1);
private BoundsSpecified _restoredWindowBoundsSpecified;
private DialogResult _dialogResult;
private MdiClient? _ctlClient;
private NativeWindow? _ownerWindow;
private bool _rightToLeftLayout;
private Rectangle _restoreBounds = new(-1, -1, -1, -1);
private CloseReason _closeReason = CloseReason.None;
private VisualStyleRenderer? _sizeGripRenderer;
// Cache Form's size for the DPI. When Form is moved between the monitors with different DPI settings, we use
// cached values to set the size matching the DPI on the Form instead of recalculating the size again. This help
// preventing rounding error in size calculations with float DPI factor and rounding it to nearest integer.
private Dictionary<int, Size>? _dpiFormSizes;
private bool _processingDpiChanged;
private bool _inRecreateHandle;
/// <summary>
/// Initializes a new instance of the <see cref="Form"/> class.
/// </summary>
public Form() : base()
{
// Assert section.
Debug.Assert(_formState[FormStateAllowTransparency] == 0, "Failed to set formState[FormStateAllowTransparency]");
Debug.Assert(_formState[FormStateBorderStyle] == (int)FormBorderStyle.Sizable, "Failed to set formState[FormStateBorderStyle]");
Debug.Assert(_formState[FormStateTaskBar] == 1, "Failed to set formState[FormStateTaskBar]");
Debug.Assert(_formState[FormStateControlBox] == 1, "Failed to set formState[FormStateControlBox]");
Debug.Assert(_formState[FormStateKeyPreview] == 0, "Failed to set formState[FormStateKeyPreview]");
Debug.Assert(_formState[FormStateLayered] == 0, "Failed to set formState[FormStateLayered]");
Debug.Assert(_formState[FormStateMaximizeBox] == 1, "Failed to set formState[FormStateMaximizeBox]");
Debug.Assert(_formState[FormStateMinimizeBox] == 1, "Failed to set formState[FormStateMinimizeBox]");
Debug.Assert(_formState[FormStateHelpButton] == 0, "Failed to set formState[FormStateHelpButton]");
Debug.Assert(_formState[FormStateStartPos] == (int)FormStartPosition.WindowsDefaultLocation, "Failed to set formState[FormStateStartPos]");
Debug.Assert(_formState[FormStateWindowState] == (int)FormWindowState.Normal, "Failed to set formState[FormStateWindowState]");
Debug.Assert(_formState[FormStateShowWindowOnCreate] == 0, "Failed to set formState[FormStateShowWindowOnCreate]");
Debug.Assert(_formState[FormStateAutoScaling] == 1, "Failed to set formState[FormStateAutoScaling]");
Debug.Assert(_formState[FormStateSetClientSize] == 0, "Failed to set formState[FormStateSetClientSize]");
Debug.Assert(_formState[FormStateTopMost] == 0, "Failed to set formState[FormStateTopMost]");
Debug.Assert(_formState[FormStateSWCalled] == 0, "Failed to set formState[FormStateSWCalled]");
Debug.Assert(_formState[FormStateMdiChildMax] == 0, "Failed to set formState[FormStateMdiChildMax]");
Debug.Assert(_formState[FormStateRenderSizeGrip] == 0, "Failed to set formState[FormStateRenderSizeGrip]");
Debug.Assert(_formState[FormStateSizeGripStyle] == 0, "Failed to set formState[FormStateSizeGripStyle]");
Debug.Assert(_formState[FormStateIsWindowActivated] == 0, "Failed to set formState[FormStateIsWindowActivated]");
Debug.Assert(_formState[FormStateIsTextEmpty] == 0, "Failed to set formState[FormStateIsTextEmpty]");
Debug.Assert(_formState[FormStateIsActive] == 0, "Failed to set formState[FormStateIsActive]");
Debug.Assert(_formState[FormStateIconSet] == 0, "Failed to set formState[FormStateIconSet]");
_formStateEx[FormStateExShowIcon] = 1;
SetState(States.Visible, false);
SetState(States.TopLevel, true);
}
/// <summary>
/// Indicates the <see cref="Button"/> control on the form that is clicked when
/// the user presses the ENTER key.
/// </summary>
[DefaultValue(null)]
[SRDescription(nameof(SR.FormAcceptButtonDescr))]
public IButtonControl? AcceptButton
{
get
{
return (IButtonControl?)Properties.GetObject(PropAcceptButton);
}
set
{
if (AcceptButton != value)
{
Properties.SetObject(PropAcceptButton, value);
UpdateDefaultButton();
}
}
}
/// <summary>
/// Retrieves true if this form is currently active.
/// </summary>
internal bool Active
{
get
{
Form? parentForm = ParentForm;
if (parentForm is null)
{
return _formState[FormStateIsActive] != 0;
}
return parentForm.ActiveControl == this && parentForm.Active;
}
set
{
s_focusTracing.TraceVerbose($"Form::set_Active - {Name}");
if ((_formState[FormStateIsActive] != 0) != value)
{
if (value)
{
if (!CanRecreateHandle())
{
return;
}
}
_formState[FormStateIsActive] = value ? 1 : 0;
if (value)
{
_formState[FormStateIsWindowActivated] = 1;
// Check if validation has been canceled to avoid raising Validation event multiple times.
if (!ValidationCancelled)
{
if (ActiveControl is null)
{
// If no control is selected focus will go to form
SelectNextControl(null, true, true, true, false);
}
InnerMostActiveContainerControl.FocusActiveControlInternal();
}
OnActivated(EventArgs.Empty);
}
else
{
_formState[FormStateIsWindowActivated] = 0;
OnDeactivate(EventArgs.Empty);
}
}
}
}
/// <summary>
/// Gets the currently active form for this application.
/// </summary>
public static Form? ActiveForm => FromHandle(PInvoke.GetForegroundWindow()) as Form;
/// <summary>
///
/// Gets the currently active multiple document interface (MDI) child window.
/// Note: Don't use this property internally, use ActiveMdiChildInternal instead (see comments below).
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.FormActiveMDIChildDescr))]
public Form? ActiveMdiChild
{
get
{
Form? mdiChild = ActiveMdiChildInternal;
// We keep the active mdi child in the cached in the property store; when changing its value
// (due to a change to one of the following properties/methods: Visible, Enabled, Active, Show/Hide,
// Focus() or as the result of WM_SETFOCUS/WM_ACTIVATE/WM_MDIACTIVATE) we temporarily set it to null
// (to properly handle menu merging among other things) rendering the cache out-of-date; the problem
// arises when the user has an event handler that is raised during this process; in that case we ask
// Windows for it (see ActiveMdiChildFromWindows).
if (mdiChild is null)
{
// If this.MdiClient is not null it means this.IsMdiContainer == true.
if (_ctlClient is not null && _ctlClient.IsHandleCreated)
{
IntPtr hwnd = PInvoke.SendMessage(_ctlClient, PInvoke.WM_MDIGETACTIVE);
mdiChild = FromHandle(hwnd) as Form;
}
}
if (mdiChild is not null && mdiChild.Visible && mdiChild.Enabled)
{
return mdiChild;
}
return null;
}
}
/// <summary>
/// Property to be used internally. See comments a on ActiveMdiChild property.
/// </summary>
internal Form? ActiveMdiChildInternal
{
get
{
return (Form?)Properties.GetObject(PropActiveMdiChild);
}
set
{
Properties.SetObject(PropActiveMdiChild, value);
}
}
//we don't repaint the mdi child that used to be active any more. We used to do this in Activated, but no
//longer do because of added event Deactivate.
private Form? FormerlyActiveMdiChild
{
get
{
return (Form?)Properties.GetObject(PropFormerlyActiveMdiChild);
}
set
{
Properties.SetObject(PropFormerlyActiveMdiChild, value);
}
}
/// <summary>
/// Gets or sets
/// a value indicating whether the opacity of the form can be
/// adjusted.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.ControlAllowTransparencyDescr))]
public bool AllowTransparency
{
get
{
return _formState[FormStateAllowTransparency] != 0;
}
set
{
if (value != (_formState[FormStateAllowTransparency] != 0))
{
_formState[FormStateAllowTransparency] = (value ? 1 : 0);
_formState[FormStateLayered] = _formState[FormStateAllowTransparency];
UpdateStyles();
if (!value)
{
if (Properties.ContainsObject(PropOpacity))
{
Properties.SetObject(PropOpacity, 1.0f);
}
if (Properties.ContainsObject(PropTransparencyKey))
{
Properties.SetObject(PropTransparencyKey, Color.Empty);
}
UpdateLayered();
}
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the form will adjust its size
/// to fit the height of the font used on the form and scale
/// its controls.
/// </summary>
[SRCategory(nameof(SR.CatLayout))]
[SRDescription(nameof(SR.FormAutoScaleDescr)),
Obsolete("This property has been deprecated. Use the AutoScaleMode property instead. https://go.microsoft.com/fwlink/?linkid=14202")]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool AutoScale
{
get
{
return _formState[FormStateAutoScaling] != 0;
}
set
{
_formStateEx[FormStateExSettingAutoScale] = 1;
try
{
if (value)
{
_formState[FormStateAutoScaling] = 1;
// if someone insists on auto scaling,
// force the new property back to none so they
// don't compete.
AutoScaleMode = AutoScaleMode.None;
}
else
{
_formState[FormStateAutoScaling] = 0;
}
}
finally
{
_formStateEx[FormStateExSettingAutoScale] = 0;
}
}
}
// Our STRONG recommendation to customers is to upgrade to AutoScaleDimensions
// however, since this is generated by default in Everett, and there's not a 1:1 mapping of
// the old to the new, we are un-obsoleting the setter for AutoScaleBaseSize only.
/// <summary>
/// The base size used for autoscaling. The AutoScaleBaseSize is used
/// internally to determine how much to scale the form when AutoScaling is
/// used.
/// </summary>
//
// Virtual so subclasses like PrintPreviewDialog can prevent changes.
[Localizable(true)]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual Size AutoScaleBaseSize
{
get
{
if (_autoScaleBaseSize.IsEmpty)
{
SizeF real = GetAutoScaleSize(Font);
return new Size((int)Math.Round(real.Width), (int)Math.Round(real.Height));
}
return _autoScaleBaseSize;
}
set
{
// Only allow the set when not in designmode, this prevents us from
// preserving an old value. The form design should prevent this for
// us by shadowing this property, so we just assert that the designer
// is doing its job.
//
Debug.Assert(!DesignMode, "Form designer should not allow base size set in design mode.");
_autoScaleBaseSize = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether the form implements
/// autoscrolling.
/// </summary>
[Localizable(true)]
public override bool AutoScroll
{
get => base.AutoScroll;
set
{
if (value)
{
IsMdiContainer = false;
}
base.AutoScroll = value;
}
}
// Forms implement their own AutoSize in OnLayout so we shadow this property
// just in case someone parents a Form to a container control.
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override bool AutoSize
{
get { return _formStateEx[FormStateExAutoSize] != 0; }
set
{
if (value != AutoSize)
{
_formStateEx[FormStateExAutoSize] = value ? 1 : 0;
if (!AutoSize)
{
_minAutoSize = Size.Empty;
// If we just disabled AutoSize, restore the original size.
Size = CommonProperties.GetSpecifiedBounds(this).Size;
}
LayoutTransaction.DoLayout(this, this, PropertyNames.AutoSize);
OnAutoSizeChanged(EventArgs.Empty);
}
Debug.Assert(AutoSize == value, "Error detected setting Form.AutoSize.");
}
}
[SRCategory(nameof(SR.CatPropertyChanged))]
[SRDescription(nameof(SR.ControlOnAutoSizeChangedDescr))]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public new event EventHandler? AutoSizeChanged
{
add => base.AutoSizeChanged += value;
remove => base.AutoSizeChanged -= value;
}
/// <summary>
/// Allows the control to optionally shrink when AutoSize is true.
/// </summary>
[SRDescription(nameof(SR.ControlAutoSizeModeDescr))]
[SRCategory(nameof(SR.CatLayout))]
[Browsable(true)]
[DefaultValue(AutoSizeMode.GrowOnly)]
[Localizable(true)]
public AutoSizeMode AutoSizeMode
{
get
{
return GetAutoSizeMode();
}
set
{
SourceGenerated.EnumValidator.Validate(value);
if (GetAutoSizeMode() != value)
{
SetAutoSizeMode(value);
Control toLayout = DesignMode || ParentInternal is null ? this : ParentInternal;
if (toLayout is not null)
{
// DefaultLayout does not keep anchor information until it needs to. When
// AutoSize became a common property, we could no longer blindly call into
// DefaultLayout, so now we do a special InitLayout just for DefaultLayout.
if (toLayout.LayoutEngine == DefaultLayout.Instance)
{
toLayout.LayoutEngine.InitLayout(this, BoundsSpecified.Size);
}
LayoutTransaction.DoLayout(toLayout, this, PropertyNames.AutoSize);
}
}
}
}
/// <summary>
/// Indicates whether controls in this container will be automatically validated when the focus changes.
/// </summary>
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public override AutoValidate AutoValidate
{
get => base.AutoValidate;
set => base.AutoValidate = value;
}
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public new event EventHandler? AutoValidateChanged
{
add => base.AutoValidateChanged += value;
remove => base.AutoValidateChanged -= value;
}
/// <summary>
/// The background color of this control. This is an ambient property and
/// will always return a non-null value.
/// </summary>
public override Color BackColor
{
get
{
// Forms should not inherit BackColor from their parent,
// particularly if the parent is an MDIClient.
Color c = RawBackColor; // inheritedProperties.BackColor
if (!c.IsEmpty)
{
return c;
}
return DefaultBackColor;
}
set => base.BackColor = value;
}
private bool CalledClosing
{
get
{
return _formStateEx[FormStateExCalledClosing] != 0;
}
set
{
_formStateEx[FormStateExCalledClosing] = (value ? 1 : 0);
}
}
private bool CalledCreateControl
{
get
{
return _formStateEx[FormStateExCalledCreateControl] != 0;
}
set
{
_formStateEx[FormStateExCalledCreateControl] = (value ? 1 : 0);
}
}
private bool CalledMakeVisible
{
get
{
return _formStateEx[FormStateExCalledMakeVisible] != 0;
}
set
{
_formStateEx[FormStateExCalledMakeVisible] = (value ? 1 : 0);
}
}
private bool CalledOnLoad
{
get
{
return _formStateEx[FormStateExCalledOnLoad] != 0;
}
set
{
_formStateEx[FormStateExCalledOnLoad] = (value ? 1 : 0);
}
}
/// <summary>
/// Gets or sets the border style of the form.
/// </summary>
[SRCategory(nameof(SR.CatAppearance))]
[DefaultValue(FormBorderStyle.Sizable)]
[DispId(PInvoke.DISPID_BORDERSTYLE)]
[SRDescription(nameof(SR.FormBorderStyleDescr))]
public FormBorderStyle FormBorderStyle
{
get => (FormBorderStyle)_formState[FormStateBorderStyle];
set
{
SourceGenerated.EnumValidator.Validate(value);
_formState[FormStateBorderStyle] = (int)value;
if (_formState[FormStateSetClientSize] == 1 && !IsHandleCreated)
{
ClientSize = ClientSize;
}
// Since setting the border style induces a call to SetBoundsCore, which,
// when the WindowState is not Normal, will cause the values stored in the field restoredWindowBounds
// to be replaced. The side-effect of this, of course, is that when the form window is restored,
// the Form's size is restored to the size it was when in a non-Normal state.
// So, we need to cache these values now before the call to UpdateFormStyles() to prevent
// these existing values from being lost. Then, if the WindowState is something other than
// FormWindowState.Normal after the call to UpdateFormStyles(), restore these cached values to
// the restoredWindowBounds field.
Rectangle preClientUpdateRestoredWindowBounds = _restoredWindowBounds;
BoundsSpecified preClientUpdateRestoredWindowBoundsSpecified = _restoredWindowBoundsSpecified;
int preWindowBoundsWidthIsClientSize = _formStateEx[FormStateExWindowBoundsWidthIsClientSize];
int preWindowBoundsHeightIsClientSize = _formStateEx[FormStateExWindowBoundsHeightIsClientSize];
UpdateFormStyles();
// In Windows Theme, the FixedDialog tend to have a small Icon.
// So to make this behave uniformly with other styles, we need to make
// the call to UpdateIcon after the form styles have been updated.
if (_formState[FormStateIconSet] == 0)
{
UpdateWindowIcon(false);
}
// Now restore the values cached above.
if (WindowState != FormWindowState.Normal)
{
_restoredWindowBounds = preClientUpdateRestoredWindowBounds;
_restoredWindowBoundsSpecified = preClientUpdateRestoredWindowBoundsSpecified;
_formStateEx[FormStateExWindowBoundsWidthIsClientSize] = preWindowBoundsWidthIsClientSize;
_formStateEx[FormStateExWindowBoundsHeightIsClientSize] = preWindowBoundsHeightIsClientSize;
}
}
}
/// <summary>
/// Gets
/// or
/// sets the button control that will be clicked when the
/// user presses the ESC key.
/// </summary>
[DefaultValue(null)]
[SRDescription(nameof(SR.FormCancelButtonDescr))]
public IButtonControl? CancelButton
{
get
{
return (IButtonControl?)Properties.GetObject(PropCancelButton);
}
set
{
Properties.SetObject(PropCancelButton, value);
if (value is not null && value.DialogResult == DialogResult.None)
{
value.DialogResult = DialogResult.Cancel;
}
}
}
/// <summary>
/// Gets or sets the size of the client area of the form.
/// </summary>
[Localizable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public new Size ClientSize
{
get => base.ClientSize;
set => base.ClientSize = value;
}
/// <summary>
/// Gets or sets a value indicating whether a control box is displayed in the
/// caption bar of the form.
/// </summary>
[SRCategory(nameof(SR.CatWindowStyle))]
[DefaultValue(true)]
[SRDescription(nameof(SR.FormControlBoxDescr))]
public bool ControlBox
{
get => _formState[FormStateControlBox] != 0;
set
{
_formState[FormStateControlBox] = value ? 1 : 0;
UpdateFormStyles();
}
}
/// <summary>
/// Retrieves the CreateParams used to create the window.
/// If a subclass overrides this function, it must call the base implementation.
/// </summary>
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
if (IsHandleCreated && WindowStyle.HasFlag(WINDOW_STYLE.WS_DISABLED))
{
// Forms that are parent of a modal dialog must keep their WS_DISABLED style
cp.Style |= (int)WINDOW_STYLE.WS_DISABLED;
}
else if (TopLevel)
{
// It doesn't seem to make sense to allow a top-level form to be disabled
//
cp.Style &= ~(int)WINDOW_STYLE.WS_DISABLED;
}
if (TopLevel && (_formState[FormStateLayered] != 0))
{
cp.ExStyle |= (int)WINDOW_EX_STYLE.WS_EX_LAYERED;
}
IWin32Window? dialogOwner = (IWin32Window?)Properties.GetObject(PropDialogOwner);
if (dialogOwner is not null)
{
cp.Parent = GetSafeHandle(dialogOwner).Handle;
}
FillInCreateParamsBorderStyles(cp);
FillInCreateParamsWindowState(cp);
FillInCreateParamsBorderIcons(cp);
if (_formState[FormStateTaskBar] != 0)
{
cp.ExStyle |= (int)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
}
FormBorderStyle borderStyle = FormBorderStyle;
if (!ShowIcon &&
(borderStyle == FormBorderStyle.Sizable ||
borderStyle == FormBorderStyle.Fixed3D ||
borderStyle == FormBorderStyle.FixedSingle))
{
cp.ExStyle |= (int)WINDOW_EX_STYLE.WS_EX_DLGMODALFRAME;
}
if (IsMdiChild)
{
if (Visible
&& (WindowState == FormWindowState.Maximized
|| WindowState == FormWindowState.Normal))
{
Form? formMdiParent = (Form?)Properties.GetObject(PropFormMdiParent);
Form? form = formMdiParent?.ActiveMdiChildInternal;
if (form is not null
&& form.WindowState == FormWindowState.Maximized)
{
cp.Style |= (int)WINDOW_STYLE.WS_MAXIMIZE;
_formState[FormStateWindowState] = (int)FormWindowState.Maximized;
SetState(States.SizeLockedByOS, true);
}
}
if (_formState[FormStateMdiChildMax] != 0)
{
cp.Style |= (int)WINDOW_STYLE.WS_MAXIMIZE;
}
cp.ExStyle |= (int)WINDOW_EX_STYLE.WS_EX_MDICHILD;
}
if (TopLevel || IsMdiChild)
{
FillInCreateParamsStartPosition(cp);
// Delay setting to visible until after the handle gets created
// to allow applyClientSize to adjust the size before displaying
// the form.
//
if ((cp.Style & (int)WINDOW_STYLE.WS_VISIBLE) != 0)
{
_formState[FormStateShowWindowOnCreate] = 1;
cp.Style &= ~(int)WINDOW_STYLE.WS_VISIBLE;
}
else
{
_formState[FormStateShowWindowOnCreate] = 0;
}
}
if (RightToLeft == RightToLeft.Yes && RightToLeftLayout)
{
//We want to turn on mirroring for Form explicitly.
cp.ExStyle |= (int)(WINDOW_EX_STYLE.WS_EX_LAYOUTRTL | WINDOW_EX_STYLE.WS_EX_NOINHERITLAYOUT);
//Don't need these styles when mirroring is turned on.
cp.ExStyle &= ~(int)(WINDOW_EX_STYLE.WS_EX_RTLREADING | WINDOW_EX_STYLE.WS_EX_RIGHT | WINDOW_EX_STYLE.WS_EX_LEFTSCROLLBAR);
}
return cp;
}
}
internal CloseReason CloseReason
{
get { return _closeReason; }
set { _closeReason = value; }
}
/// <summary>
/// The default icon used by the Form. This is the standard "windows forms" icon.
/// </summary>
internal static Icon DefaultIcon
{
get
{
// Avoid locking if the value is filled in...
if (defaultIcon is null)
{
lock (internalSyncObject)
{
// Once we grab the lock, we re-check the value to avoid a
// race condition.
defaultIcon ??= new Icon(typeof(Form), "wfc");
}
}
return defaultIcon;
}
}
protected override ImeMode DefaultImeMode
{
get
{
return ImeMode.NoControl;
}
}
/// <summary>
/// Deriving classes can override this to configure a default size for their control.
/// This is more efficient than setting the size in the control's constructor.
/// </summary>
protected override Size DefaultSize
{
get
{
return new Size(300, 300);
}
}
/// <summary>
/// Gets or sets the size and location of the form on the Windows desktop.
/// </summary>
[SRCategory(nameof(SR.CatLayout))]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.FormDesktopBoundsDescr))]
public Rectangle DesktopBounds
{
get
{
Rectangle screen = SystemInformation.WorkingArea;
Rectangle bounds = Bounds;
bounds.X -= screen.X;
bounds.Y -= screen.Y;
return bounds;
}
set
{
SetDesktopBounds(value.X, value.Y, value.Width, value.Height);
}
}
/// <summary>
/// Gets or sets the location of the form on the Windows desktop.
/// </summary>
[SRCategory(nameof(SR.CatLayout))]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.FormDesktopLocationDescr))]
public Point DesktopLocation
{
get
{
Rectangle screen = SystemInformation.WorkingArea;
Point loc = Location;
loc.X -= screen.X;
loc.Y -= screen.Y;
return loc;
}
set
{
SetDesktopLocation(value.X, value.Y);
}
}
/// <summary>
/// Gets or sets the dialog result for the form.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[SRDescription(nameof(SR.FormDialogResultDescr))]
public DialogResult DialogResult
{
get
{
return _dialogResult;
}
set
{
//valid values are 0x0 to 0x7
SourceGenerated.EnumValidator.Validate(value);
_dialogResult = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether a
/// help button should be displayed in the caption box of the form.
/// </summary>