-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
88 lines (75 loc) · 2.4 KB
/
App.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
using Microsoft.Win32;
using System;
using System.Media;
using System.Windows;
using System.Windows.Forms;
using System.Threading;
namespace SilentApp
{
/// <summary>
/// Логика взаимодействия для App.xaml
/// </summary>private NotifyIcon trayIcon;
///
public interface IOnPlayFinishedListener {
void OnPlayFinished();
}
public class PlayerWork
{
private readonly IOnPlayFinishedListener listener;
public PlayerWork(IOnPlayFinishedListener listener) {
this.listener = listener;
}
public void PlaySound() {
GC.Collect();
SoundPlayer soundPlayer = new SoundPlayer(SilentApp.Properties.Resources.silence);
soundPlayer.PlaySync();
soundPlayer.Stop();
listener.OnPlayFinished();
}
}
public partial class App : System.Windows.Application, IOnPlayFinishedListener
{
private NotifyIcon trayIcon;
protected override void OnStartup(StartupEventArgs e) {
OnLoad();
PlaySound();
AddApplicationToStartup();
}
public void OnPlayFinished() {
PlaySound();
}
private void OnLoad()
{
// Initialize Tray Icon
trayIcon = new NotifyIcon()
{
Icon = SilentApp.Properties.Resources.AppIcon,
Text = "Silent App",
ContextMenu = new ContextMenu(
new MenuItem[] {
new MenuItem("Exit", CloseApp)
}),
Visible = true
};
}
private void PlaySound()
{
PlayerWork playerWork = new PlayerWork(this);
Thread t = new Thread(playerWork.PlaySound);
t.Start();
}
private void CloseApp(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
Environment.Exit(0);
}
public void AddApplicationToStartup()
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
{
key.SetValue("Silent App", "\"" + System.Reflection.Assembly.GetExecutingAssembly().Location + "\"");
}
}
}
}