-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
426 lines (381 loc) · 14.2 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
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
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Win32;
using System.Collections;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Diagnostics;
using System.Windows.Shapes;
using VencentLum的视频桌面;
using System.Threading.Tasks;
namespace VencentLum
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
ArrayList file_paths = new ArrayList();
UIElement active_border_element;
Player window_player;
Boolean player_visible = false;
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
public MainWindow()
{
Loaded += OnLoad;
InitializeComponent();
}
public void OnLoad(object sender, RoutedEventArgs e)
{
SetValuesFromStorage();
SetIcon();
VcUtils.MyUtils.SendSortcutToDesktop();
}
/// <summary>
/// 设置后台图标
/// </summary>
public void SetIcon()
{
this.notifyIcon.Text = "VencentLum的视频桌面";
this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this.notifyIcon.Visible = false;
this.notifyIcon.Click += FShow;
}
/// <summary>
/// 完全退出程序
/// </summary>
private void Exit_Click(object sender, RoutedEventArgs routedEventArgs)
{
Environment.Exit(0);
}
/// <summary>
/// 打开主窗口,关闭托盘图标
/// </summary>
private void FShow(object sender, EventArgs e)
{
this.Show();
WindowState = WindowState.Normal;
this.notifyIcon.Visible = false;
}
private void FClose(object sender, EventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
/// <summary>
/// 从本地读取文件
/// </summary>
private void SetValuesFromStorage()
{
JObject obj = Storage.LocalStorage.GetObject();
var array = obj["source"];
foreach (var item in array)
{
string path = item.ToString();
// 判断文件是否存在
if (File.Exists(path))
{
file_paths.Add(path);
}
}
Add_All_Item_In_View(file_paths);
}
// 动态加载图片资源
private Image Load_Image(string path)
{
Image i = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(path, UriKind.Absolute);
src.EndInit();
i.Source = src;
i.Stretch = Stretch.Uniform;
int q = src.PixelHeight; // Image loads here
return i;
}
/// <summary>
/// 视频预览
/// </summary>
private void SetBorderAndMarkVideoPath(string file_path, Image image)
{
ToolTip tootip = new ToolTip();
tootip.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
Border myBorder = new Border();
myBorder.BorderBrush = Brushes.SkyBlue;
myBorder.BorderThickness = new Thickness(2);
myBorder.Background = Brushes.AliceBlue;
myBorder.Padding = new Thickness(5);
myBorder.Margin = new Thickness(3);
myBorder.Width = 240;
myBorder.CornerRadius = new CornerRadius(1);
myBorder.Cursor = Cursors.Hand;
// 绑定右键菜单
myBorder.ContextMenu = this.Resources["ContextMenu"] as ContextMenu;
myBorder.MouseEnter += MyBorder_MouseEnter;
myBorder.MouseLeave += MyBorder_MouseLeave;
myBorder.MouseDown += MyBorder_MouseDown;
//MediaElement mediaElement = new MediaElement();
// 预加载媒体资源,但不播放
//mediaElement.LoadedBehavior = MediaState.Manual;
//mediaElement.IsMuted = true;
//// 设置控件尺寸比例与媒体资源自适应
//mediaElement.Stretch = System.Windows.Media.Stretch.Uniform;
//// 视频的第一帧作为视频封面 start
//mediaElement.ScrubbingEnabled = true;
//mediaElement.Pause();
//mediaElement.Position = TimeSpan.FromTicks(1);
//// 视频的第一帧作为视频封面 end
//// 鼠标经过时开始播放媒体
//mediaElement.MouseEnter += On_MediaPlayer_Play;
//// 鼠标离开时结束播放媒体
//mediaElement.MouseLeave += On_MediaPlayer_Stop;
//// 视频播放失败时回收资源
//mediaElement.MediaFailed += MediaPlayer_Failed;
//// 视频播放完毕时重置播放进度为第一帧
//mediaElement.MediaEnded += new RoutedEventHandler(MediaPlayer_Ended);
//// 设置资源 URI
//mediaElement.Source = new Uri(file_path, UriKind.Absolute);
//// 在布局控件里设置媒体控件
//MediaIndexMark.Mark.SetMarkIndex(myBorder, index);
//myBorder.Child = mediaElement;
MediaMarkInImage.Mark.SetMark(myBorder, file_path);
tootip.Content = MediaMarkInImage.Mark.GetMark(myBorder);
myBorder.ToolTip = tootip;
myBorder.Child = image;
_ = wrap_pannel.Children.Add(myBorder);
}
private void FFmpegPlay(string video_path)
{
Task task = new Task(() =>
{
VcUtils.MyUtils.FFmpegPlay(video_path);
});
task.Start();
}
private void MyBorder_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
Border myBorder = sender as Border;
string video_path = MediaMarkInImage.Mark.GetMark(myBorder);
FFmpegPlay(video_path);
}
}
private void MyBorder_MouseLeave(object sender, MouseEventArgs e)
{
Border? border = sender as Border;
border.Background = Brushes.AliceBlue;
}
private void MyBorder_MouseEnter(object sender, MouseEventArgs e)
{
Border? border = sender as Border;
if (border != null)
{
active_border_element = border;
border.Background = Brushes.Salmon;
}
}
private void MediaPlayer_Failed(object? sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show(e.ErrorException.ToString());
}
/// <summary>
/// 视频播放完毕再重新播放
/// </summary>
private void MediaPlayer_Ended(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement? media = sender as MediaElement;
media.Position = TimeSpan.FromTicks(1);
media.Play();
}
void On_MediaPlayer_Play(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement? media = sender as MediaElement;
media.Play();
}
void On_MediaPlayer_Stop(object sender, RoutedEventArgs routedEventArgs)
{
MediaElement? media = sender as MediaElement;
media.Stop();
}
/// <summary>
/// 在文件系统中选取文件
/// </summary>
void On_Select_File(object sender, RoutedEventArgs routedEventArgs)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择数据源文件";
openFileDialog.Filter = "视频文件|*.MP4;*.MOV;*.WMV;*.WAV;*.3gp;*.avi";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
openFileDialog.RestoreDirectory = true;
openFileDialog.DefaultExt = "mp4";
if (openFileDialog.ShowDialog() == false)
{
return;
}
string file_path = openFileDialog.FileName;
Add_Files_Path(file_path);
Storage.LocalStorage.SaveJson(file_paths);
}
/// <summary>
/// 异步转码并返回新的文件地址
/// </summary>
/// <param name="file_path"></param>
/// <returns></returns>
Task<string> GetNewVideoPath(string file_path)
{
Task<string> task = new Task<string>(() =>
{
string video_path = VcUtils.MyUtils.ConvertVideo(file_path);
return video_path;
});
task.Start();
return task;
}
/// <summary>
/// 添加文件进来
/// </summary>
async void Add_Files_Path(string file_path)
{
bool is_includes = ((IList)file_paths).Contains(file_path);
if (is_includes == false)
{
LockWindow lock_window = new LockWindow();
lock_window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
lock_window.Owner = this;
lock_window.Show();
string video_path = await GetNewVideoPath(file_path);
string image_path = VcUtils.MyUtils.CutVideoToImage(video_path);
lock_window.Close();
if (File.Exists(image_path))
{
Image image = Load_Image(image_path);
Add_Item_In_View(video_path, image);
file_paths.Add(video_path);
}
else
{
MessageBox.Show("可以试试其它的视频或者检查文件名是否包含空格和其它特殊字符", "哦豁!!视频转码失败了");
}
}
else
{
MessageBox.Show("此文件已经添加过");
}
}
void Add_Item_In_View(string path, Image image)
{
SetBorderAndMarkVideoPath(path, image);
}
void Add_All_Item_In_View(ArrayList paths)
{
for (int i = 0; i < paths.Count; i++)
{
if (paths[i] != null)
{
string video_path = paths[i].ToString();
string? image_path = VcUtils.MyUtils.GetVideoPreviewImage(video_path);
if(image_path != null)
{
Image image = Load_Image(image_path);
Add_Item_In_View(video_path, image);
}
}
}
}
/// <summary>
/// 设置后台播放
/// </summary>
void Set_Window_Background(object sender, RoutedEventArgs routedEventArgs)
{
Uri uri = new Uri(MediaMarkInImage.Mark.GetMark(active_border_element), UriKind.Absolute);
if (!player_visible)
{
window_player = new Player();
window_player.uri = uri;
window_player.Show();
player_visible = true;
}
else
{
On_Stop_window(sender, routedEventArgs);
Set_Window_Background(sender, routedEventArgs);
}
}
/// <summary>
/// 移除视频
/// </summary>
void Remove_Item_Handler(object sender, RoutedEventArgs routedEventArgs)
{
int has_element = wrap_pannel.Children.IndexOf(active_border_element);
if (active_border_element != null && has_element != -1)
{
int index = wrap_pannel.Children.IndexOf(active_border_element);
try
{
file_paths.RemoveAt(index);
wrap_pannel.Children.Remove(active_border_element);
}
catch (Exception msg)
{
MessageBox.Show(msg.Message);
}
finally
{
Storage.LocalStorage.SaveJson(file_paths);
}
}
}
void On_Stop_window(object sender, RoutedEventArgs routedEventArgs)
{
if (window_player != null && window_player.ShowActivated)
{
window_player.Close();
player_visible = false;
}
}
/// <summary>
/// 根据类型查找子元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="typename"></param>
/// <returns></returns>
public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : FrameworkElement
{
DependencyObject? child = null;
List<T> childList = new List<T>();
for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
{
child = VisualTreeHelper.GetChild(obj, i);
if (child is T && (((T)child).GetType() == typename))
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child, typename));
}
return childList;
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
Environment.Exit(0);
}
//拦截最小化事件
private void Window_StateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
// WindowState = WindowState.Normal;
this.Hide();
this.notifyIcon.Visible = true;
}
}
}
}