-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
150 lines (134 loc) · 4.92 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ClockResIcon
{
class Program
{
/* Refresh icon timer */
private Timer timer = new Timer();
/* Refresh timers (context menu) */
private int[] refreshTimers = new int[] { 1, 10, 30 };
/* Notification area icon */
private NotifyIcon nfIcon = new NotifyIcon();
public Program()
{
int resolution = GetTimerResolution();
int lastTimerResolution = resolution;
nfIcon.ContextMenuStrip = CreateContextMenu();
nfIcon.Icon = GetIcon(resolution);
nfIcon.Visible = true;
/* Invoke a 'click' on the first item of the context menu */
(nfIcon.ContextMenuStrip.Items[0] as ToolStripMenuItem).PerformClick();
/* Timer tick: update nfIcon */
timer.Tick += delegate
{
try
{
resolution = GetTimerResolution();
/* Update icon on timer change */
if (resolution != lastTimerResolution)
{
/* Destroy the previous Icon Handle to avoid a Handle leak */
WinApiCalls.DestroyIcon(nfIcon.Icon.Handle);
/* Update icon */
nfIcon.Icon = GetIcon(resolution);
/* Save last timer */
lastTimerResolution = resolution;
}
}
catch (Exception e)
{
nfIcon.ShowBalloonTip(5000, "Error", e.ToString(), ToolTipIcon.Error);
};
};
}
public void Run()
{
Application.Run();
/* When application is done, hide nfIcon */
nfIcon.Visible = false;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
/* Run baby run! */
new Program().Run();
}
/* Create the icon context menu */
private ContextMenuStrip CreateContextMenu()
{
ContextMenuStrip menu = new ContextMenuStrip();
foreach (int interval in refreshTimers)
{
ToolStripMenuItem item = new ToolStripMenuItem("Refresh: " + interval.ToString() + "s");
item.Name = "refresh";
item.Click += RefreshItemClick;
item.Tag = interval;
menu.Items.Add(item);
}
menu.Items.Add("-");
menu.Items.Add("Exit", null, delegate { Application.Exit(); });
return menu;
}
private void RefreshItemClick(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
/* Update the timer interval */
timer.Interval = (int) item.Tag * 1000;
/* Uncheck menu items */
foreach (ToolStripItem i in nfIcon.ContextMenuStrip.Items)
{
if (i.Name == "refresh") (i as ToolStripMenuItem).Checked = false;
}
/* Check me! */
item.Checked = true;
/* Start timer */
if (!timer.Enabled) timer.Start();
}
private int GetTimerResolution()
{
/* Query the timer resolution from Windows API */
TimerCaps caps = WinApiCalls.QueryTimerResolution();
return (int)(caps.PeriodCurrent / 10000.0);
}
private Icon GetIcon(int resolution)
{
/* Icon creation */
Bitmap bitmap = new Bitmap(32, 32);
Graphics graphics = Graphics.FromImage(bitmap);
Font font = new Font("Segoe UI", 18, FontStyle.Regular);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
SolidBrush fill = new SolidBrush(Color.Black);
if (resolution < 5)
{
fill.Color = Color.Red;
font = new Font("Segoe UI", 18, FontStyle.Bold);
}
else if (resolution < 10)
{
fill.Color = Color.DarkOrange;
font = new Font("Segoe UI", 18, FontStyle.Bold);
}
else if (resolution < 15)
{
fill.Color = Color.Orange;
}
graphics.FillRectangle(fill, 0, 0, bitmap.Height, bitmap.Width);
graphics.DrawString(resolution.ToString(), font, new SolidBrush(Color.White), 16, -2, format);
Icon createdIcon = Icon.FromHandle(bitmap.GetHicon());
/* Purge */
fill.Dispose();
font.Dispose();
format.Dispose();
graphics.Dispose();
bitmap.Dispose();
/* Icon */
return createdIcon;
}
}
}