-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
98 lines (81 loc) · 3.3 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
89
90
91
92
93
94
95
96
97
98
using System;
using System.Drawing;
using System.Windows;
using Forms = System.Windows.Forms;
namespace MinimalSittingNotifier {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application {
private const int MaxSitMinute = 30;
private int _timeCountNumber;
private readonly Font _defaultIconFont = new Font("Calibri", 8);
private readonly Color _defaultIconFontColor = Color.White;
private readonly Color _defaultIconStopColor = Color.Yellow;
private const int IconSize = 16;
private System.Timers.Timer _timer;
// private int h, m, s;
private readonly Forms.NotifyIcon _notifyIcon;
public App() {
_notifyIcon = new Forms.NotifyIcon();
}
protected override void OnStartup(StartupEventArgs e) {
ShowNumberIcon(MaxSitMinute.ToString());
_notifyIcon.ContextMenuStrip = new Forms.ContextMenuStrip();
_notifyIcon.ContextMenuStrip.Items.Add("Exit", null, ExitProgram);
_notifyIcon.MouseClick += ReStartTimer;
_notifyIcon.Visible = true;
StartTimer();
base.OnStartup(e);
}
private void StartTimer() {
_timeCountNumber = MaxSitMinute;
_timer = new System.Timers.Timer(1000 * 60); //1 sec * 60 = 1 min interval;
_timer.Elapsed += OnTimedEvent;
_timer.Enabled = true;
ShowNumberIcon(_timeCountNumber.ToString());
}
private void ReStartTimer(object? sender, Forms.MouseEventArgs e) {
if (e.Button == Forms.MouseButtons.Left) {
_timer.Dispose();
StartTimer();
}
}
private void OnTimedEvent(object? sender, System.Timers.ElapsedEventArgs e) {
if (_timeCountNumber <= 1) {
_timer.Enabled = false;
ShowStopIcon();
}
else {
_timeCountNumber--;
ShowNumberIcon(_timeCountNumber.ToString());
}
}
private void ExitProgram(object? sender, EventArgs e) {
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
Environment.Exit(0);
}
private void ShowNumberIcon(String numberText) {
Brush brush = new SolidBrush(_defaultIconFontColor);
Bitmap bitmap = new Bitmap(IconSize, IconSize);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawString(numberText, _defaultIconFont, brush, 0, 0);
Icon icon = Icon.FromHandle(bitmap.GetHicon());
_notifyIcon.Icon = icon;
}
private void ShowStopIcon() {
Brush brush = new SolidBrush(_defaultIconStopColor);
Bitmap bitmap = new Bitmap(IconSize, IconSize);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.FillRectangle(brush, 0, 0, IconSize, IconSize);
var icon = Icon.FromHandle(bitmap.GetHicon());
_notifyIcon.Icon = icon;
}
protected override void OnExit(ExitEventArgs e) {
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
base.OnExit(e);
}
}
}