-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrayApp.cs
96 lines (82 loc) · 2.83 KB
/
TrayApp.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
using System.Diagnostics;
using Timer = System.Windows.Forms.Timer;
namespace tray_monitor;
public partial class TrayApp : Form
{
private NotifyIcon _trayIcon;
private Timer _updateTimer;
private PerformanceCounter _cpuCounter;
private PerformanceCounter _memoryCounter;
public TrayApp()
{
try
{
// Set Icon
_trayIcon = new NotifyIcon()
{
Icon = new Icon(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Icons", "app.ico"), 40, 40),
Visible = true
};
_trayIcon.MouseMove += OnMouseMove;
_trayIcon.MouseClick += OnMouseClick;
// Set Performance Counters
_cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
_memoryCounter = new PerformanceCounter("Memory", "Available MBytes");
// Set Timer
_updateTimer = new Timer
{
Interval = 1000
};
_updateTimer.Tick += UpdateData;
_updateTimer.Start();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(1); // Encerra o aplicativo com código de erro
}
InitializeComponent();
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (_cpuCounter != null && _memoryCounter != null)
{
float CPUUsage = _cpuCounter.NextValue();
float AvailableMemory = _memoryCounter.NextValue();
string Tooltip = $"CPU: {CPUUsage:F1}%\nMemory: {AvailableMemory}MB\nDisk: {GetDiskUsage()}%";
_trayIcon.Text = Tooltip;
}
}
private void OnMouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Application.Exit();
}
else
{
// Open task manager
string TaskManagerPath = Environment.ExpandEnvironmentVariables(@"%windir%\system32\taskmgr.exe");
Process.Start(TaskManagerPath);
}
}
private void UpdateData(object sender, EventArgs e)
{
// Refresh tooltip
float CPUUsage = _cpuCounter.NextValue();
float AvailableMemory = _memoryCounter.NextValue();
string tooltipText = $"CPU: {CPUUsage:F1}%\nRAM: {AvailableMemory}MB\nDisco: {GetDiskUsage()}%";
_trayIcon.Text = tooltipText;
}
private float GetDiskUsage()
{
var DiskCounter = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
return DiskCounter.NextValue();
}
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide main window
ShowInTaskbar = false; // Hide task bar icon
base.OnLoad(e);
}
}