-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathForm1.cs
More file actions
703 lines (499 loc) · 23.9 KB
/
Form1.cs
File metadata and controls
703 lines (499 loc) · 23.9 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Threading;
using System.Web.Query.Dynamic;
using System.Windows.Forms;
namespace CoreTracker
{
#region application
public partial class Form1 : Form
{
// Constant Definition
private List<NotifyIcon> th_list = new List<NotifyIcon>();
private NotifyIcon CpuTmpereaute = new NotifyIcon() { Visible = false, Icon = Properties.Resources._10_c, BalloonTipIcon = ToolTipIcon.Info, BalloonTipTitle = "Info From CPU Temperaute" };
private NotifyIcon RamUsage = new NotifyIcon() { Visible = false, Icon = Properties.Resources._10_r, BalloonTipIcon = ToolTipIcon.Info, BalloonTipTitle = "Info From Memory Temperaute" };
private NotifyIcon BoardTmpereaute = new NotifyIcon() { Visible = false, Icon = Properties.Resources._10_b, BalloonTipIcon = ToolTipIcon.Info, BalloonTipTitle = "Info From Marderboard Temperaute" };
private NotifyIcon GraphicTmpereaute = new NotifyIcon() { Visible = false, Icon = Properties.Resources._10_g, BalloonTipIcon = ToolTipIcon.Info, BalloonTipTitle = "Info From GPU Temperaute" };
private bool run = false;
private Thread th = null;
private Int16 ModeSlow = 5000;
private Int16 ModeNormarl = 3000;
private Int16 ModeFast = 1000;
private string VERSION = "v0.10.3";
private string GITHUB = "https://github.com/Fhwang0926/CoreTracker";
private bool mouseDown;
private bool is_updateing = false;
private Point lastLocation;
private Ragistry Ragistry = new Ragistry();
private Controller controller = new Controller();
public void SetTimeout(System.Action action, int timeout)
{
var timer = new System.Windows.Forms.Timer();
var noti = new NotifyIcon() { Visible = true, Icon = Properties.Resources.form };
timer.Interval = timeout;
timer.Tick += delegate (object sender, EventArgs args)
{
action();
timer.Stop();
noti.Dispose();
btn_tray.Enabled = true;
};
noti.ShowBalloonTip(2000, "[CoreTracker Notice] : Auto Start", "Soon minimize to tray icon", ToolTipIcon.Info);
timer.Start();
}
public Form1()
{
InitializeComponent();
KeyPreview = true;
// under code run after InitializeComponent
// update
l_core_value.Text = (Environment.ProcessorCount / 2).ToString();
l_th_value.Text = Environment.ProcessorCount.ToString();
// make Notch
this.FormBorderStyle = FormBorderStyle.None;
Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20,20));
btn_notch.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, btn_notch.Height, btn_notch.Width, -400, 20, 20));
// ass running status
pic_status.Image = Properties.Resources.bad;
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(0, 0, pic_status.Width - 4, pic_status.Height - 2);
pic_status.Region = new Region(gp);
// add NotifyIcon mouse right button menu
ti_main.ContextMenu = new ContextMenu();
ti_main.ContextMenu.MenuItems.Add(0, new MenuItem("Exit", new System.EventHandler(Exit_Click)));
ti_main.ContextMenu.MenuItems.Add(1, new MenuItem("Show", new System.EventHandler(Show_Click)));
ti_main.ContextMenu.MenuItems.Add(2, new MenuItem("Hide", new System.EventHandler(Hide_Click)));
ti_main.ContextMenu.MenuItems.Add(3, new MenuItem("BUG Report(Github)", new System.EventHandler(Report_Click)));
ti_main.ContextMenu.MenuItems.Add(4, new MenuItem("Reset", new System.EventHandler(Reset_Click)));
ti_main.ContextMenu.MenuItems.Add(4, new MenuItem("Update", new System.EventHandler(Update_Click)));
// add NotifyIcon by core count
for (int c = 1; c <= Environment.ProcessorCount; c++) { th_list.Add(new NotifyIcon() { Visible = true, Icon = Properties.Resources._10 }); }
// intialize thread && check auto start
ch_graphic_temperature.Checked = Ragistry.ChecGraphicTemperature();
ch_ram_temperature.Checked = Ragistry.CheckRamTemperature();
ch_cpu_temperature.Checked = Ragistry.CheckCpuTemperature();
ch_board_temperature.Checked = Ragistry.CheckBoardTemperature();
chk_disable_alert.Checked = Ragistry.ChecDisableBusyAlert();
bool auto_run = Ragistry.CheckAutoRun();
init_CPU_Watcher(auto_run);
l_version.Text = VERSION;
// check auto update
bool auto_update = Ragistry.CheckAutoUpdate();
if (auto_update) { ch_auto_update.Checked = true; self_update(); }
// option area
ManagementObjectCollection searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration").Get();
if (searcher.Count > 0) { ch_graphic_temperature.Enabled = true; }
searcher.Dispose();
// display trayicon
ch_trayicon_setting.Checked = Ragistry.CheckTrayIconConfig();
if (!is_updateing && !ch_trayicon_setting.Checked)
{
// recommended enable this setting
ch_trayicon_setting.Checked = toggleTraySetting();
}
if (auto_run)
{
btn_tray.Enabled = false;
// auto run
ch_auto_start.Checked = true;
// start new thred for hide
SetTimeout(toggleMe, 3000);
}
Activate();
}
private void init_CPU_Watcher(bool Immediate_start = true)
{
if (th != null) { th.Abort(); th = null; }
th = new Thread(new ThreadStart(runner));
GraphicTmpereaute.Icon = setTrayIcon(0, "graphic");
CpuTmpereaute.Icon = setTrayIcon(0, "cpu");
RamUsage.Icon = setTrayIcon(0, "ram");
BoardTmpereaute.Icon = setTrayIcon(0, "board");
foreach (var item in th_list) { item.Icon = Properties.Resources._10; }
if (Immediate_start) {
th.Start();
ti_main.ShowBalloonTip(1000, "[CoreTracker Notice] : Reset", "CPU Status Checker Refreshed", ToolTipIcon.Info);
}
}
private void Report_Click(Object sender, System.EventArgs e)
{
// github report
Process.Start($"{GITHUB}/issues/new");
}
private void Update_Click(Object sender, System.EventArgs e)
{
self_update(true);
}
// update function with msgbox
private async void self_update(bool updateAnswer = false)
{
// auto update latest
updateFormat rs = await controller.CompareVersion(VERSION.Replace("v", string.Empty));
if (rs.is_error)
{
MessageBox.Show(rs?.msg.ToString(), "Update failed!! compare version", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (rs.latest) {
if (updateAnswer) { MessageBox.Show(rs?.msg.ToString(), $"{Process.GetCurrentProcess().ProcessName}", MessageBoxButtons.OK, MessageBoxIcon.Information); }
}
else
{
is_updateing = true;
DialogResult result = MessageBox.Show(rs?.msg.ToString(), $"{Process.GetCurrentProcess().ProcessName}", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == System.Windows.Forms.DialogResult.OK)
{
// unload start
updateFormat restart = controller.startDownload(rs.target);
// real restart
if (!restart.is_error)
{
if (controller.restart()) { Close(); }
}
else
{
MessageBox.Show("failed update :/", "Update Failed : restart", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
is_updateing = false;
}
}
}
}
private void Reset_Click(Object sender, System.EventArgs e)
{
init_CPU_Watcher();
}
private void Exit_Click(Object sender, System.EventArgs e)
{
Close();
}
private void Hide_Click(Object sender, System.EventArgs e)
{
toggleMe();
}
private void Show_Click(Object sender, System.EventArgs e)
{
toggleMe();
}
private void btn_tray_Click(object sender, EventArgs e)
{
toggleMe();
if (!run && !th.IsAlive) { run = true; th.Start(); }
}
private void runner()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
try
{
pic_status.Image = Properties.Resources.good;
bool noticeStatus = false;
Int16 busyCount = 0;
// init
controller.hardwareMoniterInit();
while (true)
{
// refresh temperaute
if (ch_board_temperature.Checked || ch_cpu_temperature.Checked || ch_graphic_temperature.Checked || ch_ram_temperature.Checked)
{
controller.hardwareInfo();
}
if (ch_graphic_temperature.Checked)
{
controller.computer.GPUEnabled = ch_graphic_temperature.Checked; GraphicTmpereaute.Icon = setTrayIcon(controller.sb.gpu_temperature, "graphic");
GraphicTmpereaute.BalloonTipText = $"GPU Temperature : {controller.sb.gpu_temperature}";
}
if (ch_cpu_temperature.Checked)
{
controller.computer.CPUEnabled = ch_cpu_temperature.Checked; CpuTmpereaute.Icon = setTrayIcon(controller.sb.cpu_temperature, "cpu");
CpuTmpereaute.BalloonTipText = $"CPU Temperature : {controller.sb.cpu_temperature}";
}
if (ch_ram_temperature.Checked)
{
controller.computer.RAMEnabled = ch_ram_temperature.Checked; RamUsage.Icon = setTrayIcon(controller.sb.ram_usage, "ram");
RamUsage.BalloonTipText = $"RAM Usage status : {controller.sb.ram_usage}";
}
if (ch_board_temperature.Checked)
{
controller.computer.MainboardEnabled = ch_board_temperature.Checked; BoardTmpereaute.Icon = setTrayIcon(controller.sb.board_temperature, "board");
BoardTmpereaute.BalloonTipText = $"Marderboard Temperature : {controller.sb.board_temperature}";
}
var cpu_info = searcher.Get().Cast<ManagementObject>().Select(mo => new { Name = mo["Name"], Usage = Convert.ToInt32(mo["PercentProcessorTime"]) }).ToList();
foreach (var c in cpu_info)
{
if (c.Name.ToString() == "_Total")
{
if (chk_disable_alert.Checked) { continue; }
// if show windows system notification more then 80% usage
if (busyCount >= 10) { noticeStatus = false; busyCount = 0; continue; }
else if (noticeStatus) { busyCount++; continue; }
else if (Convert.ToInt32(c.Usage) > 80)
{
ti_main.ShowBalloonTip(5000, "[CoreTracker Notice]CPU Busy", "CPU so busy, the reason is your action? if is not, scan cryptojacking malware on PC", ToolTipIcon.Warning);
noticeStatus = true;
}
continue;
}
else
{
th_list[Convert.ToInt32(c.Name)].Icon = setTrayIcon(c.Usage);
}
}
if (rb_normal.Checked) { Thread.Sleep(ModeNormarl); }
else if (rb_slow.Checked) { Thread.Sleep(ModeSlow); }
else if (rb_fast.Checked) { Thread.Sleep(ModeFast); }
}
} catch (Exception e) {
Console.WriteLine($"runner Error : {e.Message}\n\n{e.StackTrace}");
searcher.Dispose();
}
}
private void toggleMe()
{
if (ti_main.Visible)
{
Show();
this.WindowState = FormWindowState.Normal;
ti_main.Visible = false;
ti_main.ContextMenu.MenuItems[2].Enabled = true;
} else
{
Hide();
ti_main.Visible = true;
ti_main.ContextMenu.MenuItems[2].Enabled = false;
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
toggleMe();
}
private static Icon setTrayIcon(int value, string type = "")
{
// setTrayIcon find type nogada
switch (type)
{
case "cpu":
if (0 <= value && value < 20) { return Properties.Resources._10_c; }
else if (20 <= value && value < 40) { return Properties.Resources._20_c; }
else if (40 <= value && value < 60) { return Properties.Resources._40_c; }
else if (60 <= value && value < 80) { return Properties.Resources._60_c; }
else { return Properties.Resources._80_c; }
case "ram":
if (0 <= value && value < 20) { return Properties.Resources._10_r; }
else if (20 <= value && value < 40) { return Properties.Resources._20_r; }
else if (40 <= value && value < 60) { return Properties.Resources._40_r; }
else if (60 <= value && value < 80) { return Properties.Resources._60_r; }
else { return Properties.Resources._80_r; }
case "board":
if (0 <= value && value < 20) { return Properties.Resources._10_b; }
else if (20 <= value && value < 40) { return Properties.Resources._20_b; }
else if (40 <= value && value < 60) { return Properties.Resources._40_b; }
else if (60 <= value && value < 80) { return Properties.Resources._60_b; }
else { return Properties.Resources._80_b; }
case "graphic":
if (0 <= value && value < 20) { return Properties.Resources._10_g; }
else if (20 <= value && value < 40) { return Properties.Resources._20_g; }
else if (40 <= value && value < 60) { return Properties.Resources._40_g; }
else if (60 <= value && value < 80) { return Properties.Resources._60_g; }
else { return Properties.Resources._80_g; }
default:
if (0 <= value && value < 20) { return Properties.Resources._10; }
else if (20 <= value && value < 40) { return Properties.Resources._20; }
else if (40 <= value && value < 60) { return Properties.Resources._40; }
else if (60 <= value && value < 80) { return Properties.Resources._60; }
else { return Properties.Resources._80; }
}
}
private static Icon setTrayIcon(int value)
{
if (0 <= value && value < 20) { return Properties.Resources._10; }
else if (20 <= value && value < 40) { return Properties.Resources._20; }
else if (40 <= value && value < 60) { return Properties.Resources._40; }
else if (60 <= value && value < 80) { return Properties.Resources._60; }
else { return Properties.Resources._80; }
}
private void Form1_Load(object sender, EventArgs e)
{
// not run here, too slow loading
// change using taryicon dispose > windows auto refresh
controller.RefreshTrayArea();
}
private void Form1_FormClosing(object sender, EventArgs e)
{
if (th != null) { th.Abort(); th = null; }
foreach (var t in th_list) { t.Dispose(); }
controller.RefreshTrayArea();
controller.Dispose();
BoardTmpereaute.Dispose();
CpuTmpereaute.Dispose();
GraphicTmpereaute.Dispose();
RamUsage.Dispose();
}
private void Form1_Resize(object sender, EventArgs e)
{
// Hide The Form when it's minimized
if (FormWindowState.Minimized == WindowState) {
toggleMe();
if (!run && !th.IsAlive) { run = true; th.Start(); }
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(GITHUB);
}
private void ch_auto_start_CheckedChanged(object sender, EventArgs e)
{
ch_auto_start.Enabled = false;
string me = Process.GetCurrentProcess().MainModule.FileName.Split('\\').LastOrDefault().Replace(".exe", "");
if (ch_auto_start.Checked)
{
string cmd = $"schtasks /Create /TR \"{Application.ExecutablePath}\" /TN CoreTracker /SC ONLOGON /IT /RL HIGHEST /F";
if (!controller.exeCmd(cmd))
{
MessageBox.Show("Add Auto start Scheduler Failed");
return;
}
Ragistry.enable_auto_run();
}
else
{
string cmd = $"schtasks /Delete /TN \"{"CoreTracker"}\" /F";
if (!controller.exeCmd(cmd))
{
MessageBox.Show("Delete Auto start Scheduler Failed");
return;
}
Ragistry.disable_auto_run();
}
ch_auto_start.Enabled = true;
}
private void l_close_Click(object sender, EventArgs e)
{
Close();
}
private void l_hide_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void btn_notch_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
lastLocation = e.Location;
}
// Notch move
private void btn_notch_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
this.Location = new Point((this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void btn_notch_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void l_close_MouseHover(object sender, EventArgs e)
{
l_close.BackColor = System.Drawing.Color.DodgerBlue;
}
private void l_close_MouseLeave(object sender, EventArgs e)
{
l_close.BackColor = System.Drawing.Color.Transparent;
}
private void l_hide_MouseHover(object sender, EventArgs e)
{
l_hide.BackColor = System.Drawing.Color.DodgerBlue;
}
private void l_hide_MouseLeave(object sender, EventArgs e)
{
l_hide.BackColor = System.Drawing.Color.Transparent;
}
private void ch_auto_update_CheckedChanged(object sender, EventArgs e)
{
ch_auto_update.Enabled = false;
if (ch_auto_update.Checked) { Ragistry.enable_auto_update(); }
else { Ragistry.disable_auto_update(); }
ch_auto_update.Enabled = true;
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e == null) { return; }
if(ti_main.Visible) { return; }
else if (e.KeyData == Keys.Escape) { toggleMe(); }
//base.OnKeyUp(e);
}
private void ch_cpu_temperature_CheckedChanged(object sender, EventArgs e)
{
ch_cpu_temperature.Enabled = false;
if (ch_cpu_temperature.Checked) { Ragistry.enable_cpu_temperature(); }
else { Ragistry.disable_cpu_temperature(); }
CpuTmpereaute.Visible = ch_cpu_temperature.Checked;
ch_cpu_temperature.Enabled = true;
}
private void ch_ram_temperature_CheckedChanged(object sender, EventArgs e)
{
ch_ram_temperature.Enabled = false;
if (ch_ram_temperature.Checked) { Ragistry.enable_ram_temperature(); }
else { Ragistry.disable_ram_temperature(); }
RamUsage.Visible = ch_ram_temperature.Checked;
ch_ram_temperature.Enabled = true;
}
private void ch_board_temperature_CheckedChanged(object sender, EventArgs e)
{
ch_board_temperature.Enabled = false;
if (ch_board_temperature.Checked) { Ragistry.enable_board_temperature(); }
else { Ragistry.disable_board_temperature(); }
BoardTmpereaute.Visible = ch_board_temperature.Checked;
ch_board_temperature.Enabled = true;
}
private void ch_graphic_temperature_CheckedChanged(object sender, EventArgs e)
{
ch_graphic_temperature.Enabled = false;
if (ch_graphic_temperature.Checked) { Ragistry.enable_graphic_temperature(); }
else { Ragistry.disable_graphic_temperature(); }
GraphicTmpereaute.Visible = ch_graphic_temperature.Checked;
ch_graphic_temperature.Enabled = true; ;
}
private void chk_disable_alert_CheckedChanged(object sender, EventArgs e)
{
chk_disable_alert.Enabled = false;
if (chk_disable_alert.Checked) { Ragistry.enable_busy_alert(); }
else { Ragistry.disable_busy_alert(); }
chk_disable_alert.Enabled = true;
}
private void ch_trayicon_setting_Click(object sender, EventArgs e)
{
toggleTraySetting();
ch_trayicon_setting.Checked = Ragistry.CheckTrayIconConfig();
}
private bool toggleTraySetting()
{
try
{
var rs = MessageBox.Show(
"Requires setting to place tray icon area on taskbar\n\nWhen set, Explorer will restart.\n\nOther unsaved programs may be affected",
"Notice",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning
);
if (rs == DialogResult.Yes)
{
Ragistry.ToggleTrayIconConfig();
bool restarted = controller.RestartExplorer();
if (!restarted) { MessageBox.Show("Explorer Restart Failed, tray later or self restart plz", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; }
}
return true;
} catch (Exception)
{
return false;
}
}
}
#endregion
}