-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSysTrayAppContext.cs
68 lines (56 loc) · 1.84 KB
/
SysTrayAppContext.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
using System;
using System.Windows.Forms;
namespace BrightAdjust
{
class SysTrayAppContext : ApplicationContext
{
private readonly NotifyIcon _notifyIcon;
private readonly ContextMenuStrip _contextMenu;
protected SysTrayAppContext()
{
Application.ApplicationExit += this.ApplicationExitHandler;
_contextMenu = new ContextMenuStrip();
_notifyIcon = new NotifyIcon
{
ContextMenuStrip = _contextMenu,
Text = Application.ProductName,
Visible = true
};
this._notifyIcon.MouseDoubleClick += this.IconDoubleClickHandler;
this._notifyIcon.MouseClick += this.IconClickHandler;
}
protected ContextMenuStrip ContextMenu
{
get { return _contextMenu; }
}
protected NotifyIcon TrayIcon
{
get { return _notifyIcon; }
}
protected virtual void OnTrayIconClicked(MouseEventArgs e) {}
protected virtual void OnTrayIconDoubleClicked(MouseEventArgs e) {}
private void IconClickHandler(object sender, MouseEventArgs e)
{
this.OnTrayIconClicked(e);
}
private void IconDoubleClickHandler(object sender, MouseEventArgs e)
{
this.OnTrayIconDoubleClicked(e);
}
protected virtual void OnApplicationExit(EventArgs e)
{
// dispose of notification icon
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
// dispose of the context menu
if (_contextMenu != null)
{
_contextMenu.Dispose();
}
}
private void ApplicationExitHandler(object sender, EventArgs e)
{
this.OnApplicationExit(e);
}
}
}