CPUMonitor.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Microsoft.Win32;
  10. namespace CPUMonitor
  11. {
  12. public partial class frmCPU : Form
  13. {
  14. private RegistryKey AppRegKey;
  15. private const int LIGHT_MAX = 0x1F;
  16. public frmCPU()
  17. {
  18. InitializeComponent();
  19. nicoNotifyIcon.Icon = this.Icon;
  20. nicoNotifyIcon.MouseClick += new MouseEventHandler(TrayIconClick);
  21. }
  22. private void Form1_Load(object sender, EventArgs e)
  23. {
  24. AppRegKey = Registry.CurrentUser.CreateSubKey("Software\\CPUMonitor");
  25. String[] PortNames = System.IO.Ports.SerialPort.GetPortNames();
  26. Array.Sort<String>(PortNames, delegate(string strA, string strB) { return int.Parse(strA.Substring(3)).CompareTo(int.Parse(strB.Substring(3))); });
  27. cmbComPort.Items.Clear();
  28. cmbComPort.Items.AddRange(PortNames);
  29. cmbComPort.SelectedIndex = System.Convert.ToInt32(AppRegKey.GetValue("Port", "1")) - 1;
  30. serSerialPort.PortName = cmbComPort.Text;
  31. Hide();
  32. }
  33. private void NotifyLight(int Red, int Green, int Blue)
  34. {
  35. byte[] buffer = new byte[3];
  36. buffer[0] = (byte)(0x80 | (Red & LIGHT_MAX));
  37. buffer[1] = (byte)(0x40 | (Green & LIGHT_MAX));
  38. buffer[2] = (byte)(0x20 | (Blue & LIGHT_MAX));
  39. try
  40. {
  41. serSerialPort.PortName = cmbComPort.Text;
  42. serSerialPort.Open();
  43. serSerialPort.Write(buffer, 0, buffer.Length);
  44. serSerialPort.Close();
  45. }
  46. catch (Exception e)
  47. {
  48. }
  49. }
  50. private void tmrCPUTimer_Tick(object sender, EventArgs e)
  51. {
  52. float CPUUsage = pcCPUUsage.NextValue();
  53. int Red = 0;
  54. int Green = 0;
  55. int Blue = 0;
  56. if (CPUUsage < 25)
  57. {
  58. Green = (int)((LIGHT_MAX / 25) * CPUUsage);
  59. }
  60. else if (CPUUsage < 50)
  61. {
  62. Blue = (int)((LIGHT_MAX / 25) * (CPUUsage - 25));
  63. Green = LIGHT_MAX - Blue;
  64. }
  65. else if (CPUUsage < 75)
  66. {
  67. Red = (int)((LIGHT_MAX / 25) * (CPUUsage - 50));
  68. Blue = LIGHT_MAX - Red;
  69. }
  70. else
  71. {
  72. Red = LIGHT_MAX;
  73. }
  74. NotifyLight(Red, Green, Blue);
  75. lblCPU.Text = ((int)CPUUsage).ToString() + "%";
  76. }
  77. private void btnExit_Click(object sender, EventArgs e)
  78. {
  79. Application.Exit();
  80. }
  81. private void btnMinimizeToTray_Click(object sender, EventArgs e)
  82. {
  83. this.Hide();
  84. }
  85. private void TrayIconClick(object sender, MouseEventArgs e)
  86. {
  87. this.Show();
  88. this.WindowState = FormWindowState.Normal;
  89. }
  90. private void cbPort_SelectedIndexChanged(object sender, EventArgs e)
  91. {
  92. AppRegKey.SetValue("Port", cmbComPort.SelectedIndex + 1);
  93. serSerialPort.PortName = cmbComPort.Text;
  94. }
  95. }
  96. }