-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScalableWindow.cs
127 lines (108 loc) · 3.4 KB
/
ScalableWindow.cs
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
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
// ReSharper disable once CheckNamespace
namespace HGM.Hotbird64.LicenseManager
{
public class ScalableWindow : Window
{
public ScaleTransform Scaler = new ScaleTransform(1, 1);
public MainWindow MainWindow { get; protected set; }
public ScalableWindow()
{
ContentRendered += window_Rendered;
}
public ScalableWindow(MainWindow mainWindow)
{
Icon = mainWindow.Icon;
MainWindow = mainWindow;
ContentRendered += window_Rendered;
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
e.Handled = false;
if (Keyboard.IsKeyUp(Key.LeftCtrl) && Keyboard.IsKeyUp(Key.RightCtrl)) return;
e.Handled = true;
switch (e.Key)
{
case Key.OemPlus:
ZoomIn(null, null);
break;
case Key.OemMinus:
ZoomOut(null, null);
break;
case Key.D0:
Zoom0(null, null);
break;
default:
e.Handled = false;
break;
}
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Middle || (Keyboard.IsKeyUp(Key.LeftCtrl) && Keyboard.IsKeyUp(Key.RightCtrl)))
{
base.OnPreviewMouseDown(e);
}
else
{
Zoom0(null, null);
}
}
private void window_Rendered(object sender, object e)
{
if (MainWindow == null)
{
Scaler.ScaleX = Scaler.ScaleY = 1;
return;
}
Width *= MainWindow.Scaler.ScaleX;
Height *= MainWindow.Scaler.ScaleY;
Scaler.ScaleX = Scaler.ScaleY = MainWindow.Scaler.ScaleX;
}
protected void ZoomIn(object sender, RoutedEventArgs e)
{
Scaler.ScaleX *= App.ZoomFactor;
Scaler.ScaleY *= App.ZoomFactor;
Height *= App.ZoomFactor;
Width *= App.ZoomFactor;
}
protected void ZoomOut(object sender, RoutedEventArgs e)
{
Scaler.ScaleX /= App.ZoomFactor;
Scaler.ScaleY /= App.ZoomFactor;
Height /= App.ZoomFactor;
Width /= App.ZoomFactor;
}
protected void Zoom0(object sender, RoutedEventArgs e)
{
Height /= Scaler.ScaleY;
Width /= Scaler.ScaleX;
Scaler.ScaleX = 1;
Scaler.ScaleY = 1;
}
protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
{
if (Keyboard.IsKeyUp(Key.LeftCtrl) && Keyboard.IsKeyUp(Key.RightCtrl))
{
e.Handled = false;
return;
}
e.Handled = true;
double tempScale;
if (e.Delta > 0)
{
tempScale = App.ZoomFactor;
}
else
{
tempScale = 1 / App.ZoomFactor;
}
Height *= tempScale;
Width *= tempScale;
Scaler.ScaleX *= tempScale;
Scaler.ScaleY = Scaler.ScaleX;
}
}
}