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
|
public int FormScaleFactor = 1;
/// <summary>
/// Form ggf. skalieren, wenn Bildschirmhöhe größer 640
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form_Shown(object sender, EventArgs e)
{
if (this.DesignMode) return;
int intY = Screen.PrimaryScreen.Bounds.Height;
if (intY >= 640)
{
FormScaleFactor = 2;
this.SuspendLayout();
//Font skalieren
float currentSize = this.Font.SizeInPoints;
currentSize = currentSize * FormScaleFactor;
this.Font = new Font(this.Font.Name, currentSize,
this.Font.Style);
//Form vergrößern
this.Size = new Size(
this.Width * FormScaleFactor,
this.Height * FormScaleFactor);
//Controls des Forms skalieren
foreach (Control ctrl in this.Controls)
ReSizeByFactor(ctrl);
this.ResumeLayout();
}
}
/// <summary>
/// Skaliert ein Control und ruft rekursiv die Childcontrols auf
/// </summary>
/// <param name="ctrl">Control</param>
/// <remarks>Faktor in FormScaleFactor</remarks>
public void ReSizeByFactor(Control ctrl)
{
if (FormScaleFactor == 1) return;
DockStyle olddock = ctrl.Dock;
AnchorStyles oldanchor = ctrl.Anchor;
Point oldLocation = new Point(ctrl.Left, ctrl.Top);
ctrl.Dock = DockStyle.None;
ctrl.Anchor = AnchorStyles.None;
ctrl.Size = new Size(
ctrl.Width * FormScaleFactor,
ctrl.Height * FormScaleFactor);
ctrl.Location = new Point(
oldLocation.X * FormScaleFactor,
oldLocation.Y * FormScaleFactor);
if (ctrl.Font != ctrl.Parent.Font)
{
//Font vergrößern, wenn Control eine eigene Schrift hat
float currentSize = ctrl.Font.SizeInPoints;
currentSize = currentSize * FormScaleFactor;
ctrl.Font = new Font(ctrl.Font.Name, currentSize,
ctrl.Font.Style);
}
//bestimmte Control-Klassen noch gesondert behandeln
if (ctrl is Button)
{
//Bild vergrößern
//Die Methode ResziePicByWitdh in anderem Snippet suchen!
Button btn = ctrl as Button;
if (btn.Image != null)
btn.Image = ResizePicByWidth(
btn.Image, btn.Image.Width * FormScaleFactor);
if (btn.BackgroundImage != null)
btn.BackgroundImage = ResizePicByWidth(
btn.BackgroundImage, btn.BackgroundImage.Width * FormScaleFactor);
}
else if (ctrl is ListView)
{
ListView lst = ctrl as ListView;
foreach (ColumnHeader clh in lst.Columns)
{
clh.Width = clh.Width * FormScaleFactor;
}
}
//else if (ctrl is ...)
ctrl.Dock = olddock;
ctrl.Anchor = oldanchor;
//Rekursiv die enthaltenen Controls weiter skalieren
foreach (Control ctrlchild in ctrl.Controls)
ReSizeByFactor(ctrlchild);
}
|