-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathControlState.cs
More file actions
541 lines (320 loc) · 16.3 KB
/
ControlState.cs
File metadata and controls
541 lines (320 loc) · 16.3 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
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace Gsemac.Forms {
public class ControlState :
IControlState {
// Public members
public void Restore(Control control) {
if (control is null)
throw new ArgumentNullException(nameof(control));
// Layout state is saved before visual state, and they are restored in the opposite order.
// This is because visual state can affect the layout properties (e.g. the "Size" property).
if (visualState is object)
RestoreVisualState(control);
if (layoutState is object)
RestoreLayoutState(control);
}
public static IControlState Save(Control control) {
if (control is null)
throw new ArgumentNullException(nameof(control));
return Save(control, ControlStateOptions.Default);
}
public static IControlState Save(Control control, IControlStateOptions options) {
if (control is null)
throw new ArgumentNullException(nameof(control));
if (options is null)
throw new ArgumentNullException(nameof(options));
return new ControlState(control, options);
}
// Protected members
protected static bool TryGetResizeRedraw(Control control, out bool value) {
PropertyInfo drawModeProperty = control.GetType().GetProperty("ResizeRedraw", BindingFlags.NonPublic | BindingFlags.Instance);
if (drawModeProperty != null) {
value = (bool)drawModeProperty.GetValue(control, null);
return true;
}
else {
value = false;
return false;
}
}
protected static bool TrySetResizeRedraw(Control control, bool value) {
PropertyInfo drawModeProperty = control.GetType().GetProperty("ResizeRedraw", BindingFlags.NonPublic | BindingFlags.Instance);
if (drawModeProperty != null) {
drawModeProperty.SetValue(control, value, null);
return true;
}
else {
return false;
}
}
protected static bool TryGetDrawMode(Control control, out DrawMode value) {
PropertyInfo property = control.GetType().GetProperty("DrawMode", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
value = (DrawMode)property.GetValue(control, null);
return true;
}
else {
value = DrawMode.Normal;
return false;
}
}
protected static bool TrySetDrawMode(Control control, DrawMode value) {
PropertyInfo property = control.GetType().GetProperty("DrawMode", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
property.SetValue(control, value, null);
return true;
}
else {
return false;
}
}
protected static bool TryGetBorderStyle(Control control, out BorderStyle value) {
PropertyInfo property = control.GetType().GetProperty("BorderStyle", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
value = (BorderStyle)property.GetValue(control, null);
return true;
}
else {
value = BorderStyle.None;
return false;
}
}
protected static bool TrySetBorderStyle(Control control, BorderStyle value) {
PropertyInfo property = control.GetType().GetProperty("BorderStyle", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
property.SetValue(control, value, null);
return true;
}
else {
return false;
}
}
protected static bool TryGetFlatStyle(Control control, out FlatStyle value) {
PropertyInfo property = control.GetType().GetProperty("FlatStyle", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
value = (FlatStyle)property.GetValue(control, null);
return true;
}
else {
value = FlatStyle.Standard;
return false;
}
}
protected static bool TrySetFlatStyle(Control control, FlatStyle value) {
PropertyInfo property = control.GetType().GetProperty("FlatStyle", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
property.SetValue(control, value, null);
return true;
}
else {
return false;
}
}
protected static bool TryGetOwnerDraw(Control control, out bool value) {
PropertyInfo property = control.GetType().GetProperty("OwnerDraw", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
value = (bool)property.GetValue(control, null);
return true;
}
else {
value = false;
return false;
}
}
protected static bool TrySetOwnerDraw(Control control, bool value) {
PropertyInfo property = control.GetType().GetProperty("OwnerDraw", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
property.SetValue(control, value, null);
return true;
}
else {
return false;
}
}
protected static bool TryGetUseVisualStyleBackColor(Control control, out bool value) {
PropertyInfo property = control.GetType().GetProperty("UseVisualStyleBackColor", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
value = (bool)property.GetValue(control, null);
return true;
}
else {
value = true;
return false;
}
}
protected static bool TrySetUseVisualStyleBackColor(Control control, bool value) {
PropertyInfo property = control.GetType().GetProperty("UseVisualStyleBackColor", BindingFlags.Public | BindingFlags.Instance);
if (property != null) {
property.SetValue(control, value, null);
return true;
}
else {
return false;
}
}
// Private members
private class LayoutState {
public AnchorStyles Anchor { get; set; }
public bool AutoSize { get; set; }
public DockStyle Dock { get; set; }
public Point Location { get; set; }
public Size Size { get; set; }
public Point? BottomRightPadding { get; set; }
}
private class VisualState {
public Color BackColor { get; set; }
public BorderStyle BorderStyle { get; set; }
public DrawMode DrawMode { get; set; }
public bool EnableHeadersVisualStyles { get; set; }
public FlatStyle FlatStyle { get; set; }
public Color ForeColor { get; set; }
public bool GridLines { get; set; }
public bool OwnerDraw { get; set; }
public bool ResizeRedraw { get; set; }
public ControlStyles Styles { get; set; }
public ToolStripRenderer ToolStripRenderer { get; set; }
public TreeViewDrawMode TreeViewDrawMode { get; set; }
public bool UseVisualStyleBackColor { get; set; }
}
private readonly LayoutState layoutState = null;
private readonly VisualState visualState = null;
private ControlState(Control control, IControlStateOptions options) {
if (control is null)
throw new ArgumentNullException(nameof(control));
if (options is null)
throw new ArgumentNullException(nameof(options));
if (options.IncludeLayoutProperties) {
layoutState = new LayoutState() {
Anchor = control.Anchor,
AutoSize = control.AutoSize,
Dock = control.Dock,
Location = control.Location,
Size = control.Size,
};
if (control.Parent is object) {
layoutState.BottomRightPadding = new Point(
control.Parent.Width - (control.Location.X + control.Width),
control.Parent.Height - (control.Location.Y + control.Height));
}
}
if (options.IncludeVisualProperties) {
visualState = new VisualState() {
BackColor = control.BackColor,
ForeColor = control.ForeColor,
Styles = ControlUtilities.GetStyles(control)
};
if (TryGetResizeRedraw(control, out bool resizeRedraw))
visualState.ResizeRedraw = resizeRedraw;
if (TryGetDrawMode(control, out DrawMode drawMode))
visualState.DrawMode = drawMode;
if (TryGetBorderStyle(control, out BorderStyle borderStyle))
visualState.BorderStyle = borderStyle;
if (TryGetFlatStyle(control, out FlatStyle flatStyle))
visualState.FlatStyle = flatStyle;
if (TryGetOwnerDraw(control, out bool ownerDraw))
visualState.OwnerDraw = ownerDraw;
// This property is important for restoring the visual style of controls like Buttons and TabPages.
if (TryGetUseVisualStyleBackColor(control, out bool useVisualStyleBackColor))
visualState.UseVisualStyleBackColor = useVisualStyleBackColor;
switch (control) {
case DataGridView dataGridView:
visualState.BackColor = dataGridView.BackgroundColor;
visualState.EnableHeadersVisualStyles = dataGridView.EnableHeadersVisualStyles;
break;
case ListView listView:
visualState.GridLines = listView.GridLines;
break;
case ToolStrip toolStrip:
visualState.ToolStripRenderer = toolStrip.Renderer;
break;
case TreeView treeView:
visualState.TreeViewDrawMode = treeView.DrawMode;
break;
}
}
}
private void RestoreLayoutState(Control control) {
// Temporarily remove anchoring so it does not interfere with resizing/repositioning the control.
control.Anchor = AnchorStyles.None;
control.Location = layoutState.Location;
control.Size = layoutState.Size;
if (layoutState.Dock == DockStyle.None && layoutState.BottomRightPadding.HasValue && control.Parent is object) {
Point bottomRightPadding = layoutState.BottomRightPadding.Value;
// Resize the control such that the original relative distances from the sides of its parent control are retained.
if (!layoutState.Anchor.HasFlag(AnchorStyles.Left) && layoutState.Anchor.HasFlag(AnchorStyles.Right)) {
// If the control is anchored to the right but not the left, we need to adjust its location so that it has the same relative position to the right side of its parent.
// The size of the control should be equivalent to its original size.
control.Location = new Point(control.Parent.Width - (control.Width + bottomRightPadding.X), control.Location.Y);
}
else if (layoutState.Anchor.HasFlag(AnchorStyles.Left) && layoutState.Anchor.HasFlag(AnchorStyles.Right)) {
// If the control is anchored to both sides of its parent, we need to adjust its width to compensate for both its current position and its distance from the right side of the parent.
control.Width = control.Parent.Width - control.Location.X - bottomRightPadding.X;
}
else {
control.Width = layoutState.Size.Width;
}
if (!layoutState.Anchor.HasFlag(AnchorStyles.Top) && layoutState.Anchor.HasFlag(AnchorStyles.Bottom)) {
// If the control is anchored to the bottom but not the top, we need to adjust its location so that it has the same relative position to the bottom side of its parent.
// The size of the control should be equivalent to its original size.
control.Location = new Point(control.Location.X, control.Parent.Height - (control.Height + bottomRightPadding.Y));
}
else if (layoutState.Anchor.HasFlag(AnchorStyles.Top) && layoutState.Anchor.HasFlag(AnchorStyles.Bottom)) {
// If the control is anchored to both sides of its parent, we need to adjust its width to compensate for both its current position and its distance from the bottom side of the parent.
control.Height = control.Parent.Height - control.Location.Y - bottomRightPadding.Y;
}
else {
control.Height = layoutState.Size.Height;
}
}
else if (layoutState.Dock == DockStyle.Left || layoutState.Dock == DockStyle.Right) {
control.Width = layoutState.Size.Width;
}
else if (layoutState.Dock == DockStyle.Top || layoutState.Dock == DockStyle.Bottom) {
control.Height = layoutState.Size.Height;
}
else {
control.Size = layoutState.Size;
}
control.Anchor = layoutState.Anchor;
control.AutoSize = layoutState.AutoSize;
control.Dock = layoutState.Dock;
}
private void RestoreVisualState(Control control) {
// Set the colors to their defaults before attempting to apply the stored colors.
// This is so we can avoid changing the colors if they are already the defaults, which makes sure the ToolStripRenderer appears correctly.
// DataGrid is a special case where we can't set BackColor to default (Color.Empty).
// This is because it will throw an exception if we try to set BackColor to a color with transparency (ForeColor is unaffected).
control.BackColor = control is DataGrid ? SystemColors.Window : default;
control.ForeColor = default;
if (visualState.BackColor != control.BackColor)
control.BackColor = visualState.BackColor;
if (visualState.ForeColor != control.ForeColor)
control.ForeColor = visualState.ForeColor;
ControlUtilities.SetStyles(control, visualState.Styles);
TrySetResizeRedraw(control, visualState.ResizeRedraw);
TrySetDrawMode(control, visualState.DrawMode);
TrySetBorderStyle(control, visualState.BorderStyle);
TrySetFlatStyle(control, visualState.FlatStyle);
TrySetOwnerDraw(control, visualState.OwnerDraw);
TrySetUseVisualStyleBackColor(control, visualState.UseVisualStyleBackColor);
switch (control) {
case DataGridView dataGridView:
dataGridView.BackgroundColor = visualState.BackColor;
dataGridView.EnableHeadersVisualStyles = visualState.EnableHeadersVisualStyles;
break;
case ListView listView:
listView.GridLines = visualState.GridLines;
break;
case ToolStrip toolStrip:
toolStrip.Renderer = visualState.ToolStripRenderer;
break;
case TreeView treeView:
treeView.DrawMode = visualState.TreeViewDrawMode;
break;
}
}
}
}