// Builds and returns a ContextMenu for the tray icon, including menu items for each power plan, one to allow the program to run at startup, and one to exit the program. ContextMenu IntializeContextMenu() { ContextMenu ret = new ContextMenu(); // get power plans System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.FileName = "cmd"; startInfo.Arguments = "/C powercfg -l"; startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; process.StartInfo = startInfo; process.Start(); System.IO.StreamReader output = process.StandardOutput; process.WaitForExit(); // read first three lines of powercfg command to get to the list of power plans output.ReadLine(); output.ReadLine(); output.ReadLine(); // read all power plans while (!output.EndOfStream) { String line = output.ReadLine(); PowerMenuItem pmi = new PowerMenuItem(); pmi.Guid = line.Split(' ')[3]; pmi.PlanName = line.Substring(line.IndexOf('(') + 1, line.IndexOf(')') - line.IndexOf('(') - 1); pmi.Name = pmi.PlanName; pmi.Text = pmi.PlanName; pmi.Checked = line.EndsWith("*"); // * indicates that this is the active power plan pmi.Click += Pmi_Click; ret.MenuItems.Add(pmi); } // Menu item for running at startup. MenuItem runAtStartupItem = new MenuItem("Run At Startup"); RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); runAtStartupItem.Click += RunAtStartupItem_Click; //runAtStartupItem.Checked = IsStartupItem(rkApp); does not properly update the Checked property for some unkown reason runAtStartupItem.PerformClick(); // hacky way to accomplish properly updating the Checked property at startup runAtStartupItem.PerformClick(); ret.MenuItems.Add(runAtStartupItem); ret.MenuItems.Add(new MenuItem("Exit", Exit)); // add Exit MenuItem return(ret); }
// Builds and returns a ContextMenu for the tray icon, including menu items for each power plan, one to allow the program to run at startup, and one to exit the program. ContextMenu IntializeContextMenu() { ContextMenu ret = new ContextMenu(); // get power plans System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.FileName = "cmd"; startInfo.Arguments = "/C powercfg -l"; startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; process.StartInfo = startInfo; process.Start(); System.IO.StreamReader output = process.StandardOutput; process.WaitForExit(); // read first three lines of powercfg command to get to the list of power plans output.ReadLine(); output.ReadLine(); output.ReadLine(); // read all power plans while (!output.EndOfStream) { String line = output.ReadLine(); PowerMenuItem pmi = new PowerMenuItem(); pmi.Guid = line.Split(' ')[3]; pmi.PlanName = line.Substring(line.IndexOf('(') + 1, line.IndexOf(')') - line.IndexOf('(') - 1); pmi.Name = pmi.PlanName; pmi.Text = pmi.PlanName; pmi.Checked = line.EndsWith("*"); // * indicates that this is the active power plan pmi.Click += Pmi_Click; ret.MenuItems.Add(pmi); } // Menu item for running at startup. MenuItem runAtStartupItem = new MenuItem("Run At Startup"); RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); runAtStartupItem.Click += RunAtStartupItem_Click; //runAtStartupItem.Checked = IsStartupItem(rkApp); does not properly update the Checked property for some unkown reason runAtStartupItem.PerformClick(); // hacky way to accomplish properly updating the Checked property at startup runAtStartupItem.PerformClick(); ret.MenuItems.Add(runAtStartupItem); ret.MenuItems.Add(new MenuItem("Exit", Exit)); // add Exit MenuItem return ret; }
private void Pmi_Click(object sender, EventArgs e) { PowerMenuItem pmi = sender as PowerMenuItem; foreach (MenuItem item in trayIcon.ContextMenu.MenuItems) { item.Checked = false; } trayIcon.ContextMenu.MenuItems.Find(pmi.PlanName, false)[0].Checked = true; System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/C powercfg -s " + pmi.Guid; process.StartInfo = startInfo; process.Start(); }