-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.xaml.cs
245 lines (230 loc) · 8.97 KB
/
MainWindow.xaml.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace FullScreenClock
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private static string[] DOW = new string[] {"日","一", "二", "三", "四", "五"};
private string crtFileName = "";
public MainWindow()
{
InitializeComponent();
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Escape:
WindowState = WindowState.Normal;
WindowStyle = WindowStyle.SingleBorderWindow;
ResizeMode = ResizeMode.CanResize;
break;
case Key.Enter:
ResizeMode = ResizeMode.NoResize;
WindowStyle = WindowStyle.None;
WindowState = WindowState.Maximized;
break;
case Key.Right:
case Key.PageDown:
bool isFound = false;
string dn = string.IsNullOrEmpty(crtFileName) ? "images" :
System.IO.Path.GetDirectoryName(crtFileName);
foreach (string fn in Directory.GetFiles(dn, "*.jp*g"))
{
if(isFound)
{
imgBack.Source = new BitmapImage(new Uri(new FileInfo(fn).FullName));
crtFileName = fn;
Properties.Settings.Default.LastFileName = crtFileName;
Properties.Settings.Default.Save();
break;
}
isFound = string.Equals(fn, crtFileName);
}
break;
case Key.Left:
case Key.PageUp:
dn = string.IsNullOrEmpty(crtFileName) ? "images" :
System.IO.Path.GetDirectoryName(crtFileName);
string prevFile = "";
foreach (string fn in Directory.GetFiles(dn, "*.jp*g"))
{
if (string.Equals(fn, crtFileName) && !string.IsNullOrEmpty(prevFile))
{
imgBack.Source = new BitmapImage(new Uri(new FileInfo(prevFile).FullName));
crtFileName = prevFile;
Properties.Settings.Default.LastFileName = crtFileName;
Properties.Settings.Default.Save();
}
prevFile = fn;
}
break;
case Key.Space:
var status = sbMain.GetCurrentState();
if (status == System.Windows.Media.Animation.ClockState.Active)
{
sbMain.Stop();
} else
{
sbMain.Begin();
}
break;
}
}
private void Window_StateChanged(object sender, EventArgs e)
{
if (WindowState==WindowState.Maximized)
{
ResizeMode = ResizeMode.NoResize;
WindowStyle = WindowStyle.None;
WindowState = WindowState.Maximized;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(.1);
timer.Tick += Timer_Tick;
timer.Start();
if (!string.IsNullOrEmpty(Properties.Settings.Default.LastFileName))
{
var fi = new FileInfo(Properties.Settings.Default.LastFileName);
if(fi.Exists)
{
imgBack.Source = new BitmapImage(new Uri(fi.FullName));
crtFileName = fi.FullName;
}
}
if (string.IsNullOrEmpty(crtFileName))
{
LoadFirstFile("images");
}
sbMain.Begin();
}
private void Timer_Tick(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
tbDate.Text = now.ToString("yyyy年M月d日");
tbTime.Text = now.ToString("HH:mm:ss");
tbDayOfWeek.Text = DOW[(int)now.DayOfWeek];
}
private void Window_Drop(object sender, DragEventArgs e)
{
var ar = (System.Array)e.Data.GetData(DataFormats.FileDrop);
if (ar.Length > 0) {
var fn = ar.GetValue(0).ToString();
if (Directory.Exists(fn))
{
foreach (string f in Directory.GetFiles(fn, "*.jp*g"))
{
imgBack.Source = new BitmapImage(new Uri(new FileInfo(f).FullName));
crtFileName = f;
Properties.Settings.Default.LastFileName = crtFileName;
Properties.Settings.Default.Save();
break;
}
} else
{
var fi = new FileInfo(fn);
var ext = System.IO.Path.GetExtension(fn).ToLower();
if (fi.Exists && (".jpg".Equals(ext) || ".jpeg".Equals(ext)))
{
imgBack.Source = new BitmapImage(new Uri(new FileInfo(fn).FullName));
crtFileName = fn;
Properties.Settings.Default.LastFileName = crtFileName;
Properties.Settings.Default.Save();
}
}
}
}
private void Window_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Link;
} else
{
e.Effects = DragDropEffects.None;
}
}
/// <summary>
///
/// </summary>
/// <param name="dn">Directory Name</param>
private void LoadFirstFile(string dn)
{
foreach (string fn in Directory.GetFiles(dn, "*.jp*g"))
{
imgBack.Source = new BitmapImage(new Uri(new FileInfo(fn).FullName));
crtFileName = fn;
break;
}
}
private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
{
var d = e.Delta * stClock.ScaleX / 1000.0;
var ttClockX = ttClock.X;
var ttClockY = ttClock.Y;
var mousePoint = e.GetPosition(this);
// e.GetPosition(grdClock) 获得的是相对转换后的grdClock的,所以这里自己重新计算
mousePoint.Offset(-(this.Width - grdClock.Margin.Right - grdClock.Width),
-(this.Height - grdClock.Margin.Bottom - grdClock.Height));
var startPoint = tgClock.Inverse.Transform(mousePoint);
stClock.ScaleX += d;
stClock.ScaleY += d;
if (stClock.ScaleX < 0.1)
{
stClock.ScaleX = 0.1;
stClock.ScaleY = 0.1;
}
if (stClock.ScaleX > 8)
{
stClock.ScaleX = 8;
stClock.ScaleY = 8;
}
var newPoint = tgClock.Transform(startPoint);
var offset = Point.Subtract(mousePoint, newPoint);
ttClock.X = ttClockX + offset.X / stClock.ScaleX;
ttClock.Y = ttClockY + offset.Y / stClock.ScaleY;
}
private Point mouseStartPos;
private Vector startTtOffset;
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
startTtOffset = new Vector(ttClock.X, ttClock.Y);
mouseStartPos = e.GetPosition(this);
// Do not move CaptureMouse before get ttClock.X, or you'll get wrong value
grdClock.CaptureMouse();
}
private void grdClock_MouseUp(object sender, MouseButtonEventArgs e)
{
grdClock.ReleaseMouseCapture();
}
private void grdClock_MouseMove(object sender, MouseEventArgs e)
{
if (grdClock.IsMouseCaptured)
{
var offset = Point.Subtract(e.GetPosition(this), mouseStartPos);
ttClock.X = startTtOffset.X + offset.X / stClock.ScaleX;
ttClock.Y = startTtOffset.Y + offset.Y / stClock.ScaleY;
}
}
}
}