2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
13 public partial class frmCPU : Form
15 private RegistryKey AppRegKey;
17 private const int LIGHT_MAX = 0x1F;
21 InitializeComponent();
23 nicoNotifyIcon.Icon = this.Icon;
24 nicoNotifyIcon.MouseClick += new MouseEventHandler(TrayIconClick);
27 private void Form1_Load(object sender, EventArgs e)
29 AppRegKey = Registry.CurrentUser.CreateSubKey("Software\\CPUMonitor");
31 String[] PortNames = System.IO.Ports.SerialPort.GetPortNames();
32 Array.Sort<String>(PortNames, delegate(string strA, string strB) { return int.Parse(strA.Substring(3)).CompareTo(int.Parse(strB.Substring(3))); });
33 cmbComPort.Items.Clear();
34 cmbComPort.Items.AddRange(PortNames);
36 cmbComPort.SelectedIndex = System.Convert.ToInt32(AppRegKey.GetValue("Port", "1")) - 1;
37 serSerialPort.PortName = cmbComPort.Text;
42 private void NotifyLight(int Red, int Green, int Blue)
44 byte[] buffer = new byte[3];
45 buffer[0] = (byte)(0x80 | (Red & LIGHT_MAX));
46 buffer[1] = (byte)(0x40 | (Green & LIGHT_MAX));
47 buffer[2] = (byte)(0x20 | (Blue & LIGHT_MAX));
51 serSerialPort.PortName = cmbComPort.Text;
53 serSerialPort.Write(buffer, 0, buffer.Length);
54 serSerialPort.Close();
62 private void tmrCPUTimer_Tick(object sender, EventArgs e)
64 float CPUUsage = pcCPUUsage.NextValue();
72 Green = (int)((LIGHT_MAX / 25) * CPUUsage);
74 else if (CPUUsage < 50)
76 Blue = (int)((LIGHT_MAX / 25) * (CPUUsage - 25));
77 Green = LIGHT_MAX - Blue;
79 else if (CPUUsage < 75)
81 Red = (int)((LIGHT_MAX / 25) * (CPUUsage - 50));
82 Blue = LIGHT_MAX - Red;
89 NotifyLight(Red, Green, Blue);
90 lblCPU.Text = ((int)CPUUsage).ToString() + "%";
93 private void btnExit_Click(object sender, EventArgs e)
98 private void btnMinimizeToTray_Click(object sender, EventArgs e)
103 private void TrayIconClick(object sender, MouseEventArgs e)
106 this.WindowState = FormWindowState.Normal;
109 private void cbPort_SelectedIndexChanged(object sender, EventArgs e)
111 AppRegKey.SetValue("Port", cmbComPort.SelectedIndex + 1);
112 serSerialPort.PortName = cmbComPort.Text;