-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
127 lines (112 loc) · 3.73 KB
/
Program.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;
using System.Windows.Forms;
using CorionisServiceManager.NET.Properties;
using Microsoft.Win32;
using Tulpep.NotificationWindow;
namespace CorionisServiceManager.NET
{
/// <summary>
/// Corionis Service Manager.
/// Monitor and manage selected Windows services.
/// </summary>
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new CsmContext());
}
}
public class CsmContext : ApplicationContext
{
private Config cfg;
private ProgramForm form;
public NotifyIcon trayIcon;
public CsmContext()
{
// Load the configuration from JSON file
cfg = new Config();
cfg.Load();
// Initialize Tray Icon
trayIcon = new NotifyIcon()
{
Icon = Resources.manager_round_bronco,
ContextMenu = new ContextMenu(new MenuItem[]
{
new MenuItem(cfg.GetProgramTitle()), // faux title
new MenuItem("-"),
new MenuItem("&Show", Show),
new MenuItem("&Hide", Hide),
new MenuItem("-"),
new MenuItem("E&xit", Exit)
}),
Visible = true
};
// Setup the system tray icon
trayIcon.Text = cfg.GetProgramTitle();
trayIcon.DoubleClick += DoubleClick;
trayIcon.ContextMenu.MenuItems[0].Enabled = false; // disable content menu faux title
// Display the app if configured
if (cfg.StartMinimized == false)
{
Show(null, null);
}
}
private void DoubleClick(object Sender, EventArgs e)
{
if (form == null || form.Visible == false || form.WindowState == FormWindowState.Minimized)
{
Show(null, null);
}
else
{
form.WindowState = FormWindowState.Minimized;
}
}
public void Exit(object sender, EventArgs e)
{
form.logger.Write("Exit");
// Save size and coordinates
if (form != null && !form.IsDisposed)
{
form.SaveUserPreferences();
form.Visible = false;
}
// Hide tray icon, otherwise it will remain until mouse over
trayIcon.Visible = false;
Application.Exit();
}
public void Hide(object sender, EventArgs e)
{
if (form != null && !form.IsDisposed && form.WindowState != FormWindowState.Minimized)
{
form.WindowState = FormWindowState.Minimized;
}
}
public void Popup(String title, String message)
{
if (cfg.DisplayNotifications)
{
PopupNotifier popup = new PopupNotifier();
popup.TitleText = title;
popup.ContentText = message;
popup.BodyColor = cfg.ColorFromHex("#ffb44a");
popup.ShowCloseButton = true;
popup.Popup();
}
}
public void Show(object sender, EventArgs e)
{
if (form == null)
{
form = new ProgramForm(cfg, this);
}
form.ShowForm();
}
}
}