My laptop is old, so I decided it would be good if my laptop would scream at me if I kick power brick, so I would make the brick work again, and not notice only when the battery runs out and I lose all the data (battery meter is not to be trusted).
namespace POWER {
class Program {
private static System.Speech.Synthesis.SpeechSynthesizer synthesizer;
private static System.Windows.Forms.NotifyIcon theIcon;
[System.STAThread]
static void Main(string[] args) {
synthesizer = new System.Speech.Synthesis.SpeechSynthesizer();
synthesizer.SetOutputToDefaultAudioDevice();
theIcon = new System.Windows.Forms.NotifyIcon();
theIcon.Icon = System.Drawing.SystemIcons.Asterisk;
theIcon.ContextMenu = new System.Windows.Forms.ContextMenu();
theIcon.ContextMenu.MenuItems.Add("Exit", (s, e) => {
theIcon.Visible = false;
System.Windows.Forms.Application.Exit();
});
theIcon.MouseClick += (s, e) => {
if(e.Button == System.Windows.Forms.MouseButtons.Left) {
//make both mouse buttons show context menu
var method = typeof(System.Windows.Forms.NotifyIcon).GetMethod("ShowContextMenu", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
method.Invoke(theIcon, null);
}
};
var timer=new System.Windows.Forms.Timer();
timer.Interval = 3000;
timer.Tick += (s, e) => {
Tick();
};
timer.Enabled = true;
Tick();
theIcon.ContextMenu.MenuItems.Add(0,new System.Windows.Forms.MenuItem("Status",(s,e)=>{
var status=System.Windows.Forms.SystemInformation.PowerStatus;
synthesizer.SpeakAsync(string.Format("power line {0}, battery {1} {2}%", status.PowerLineStatus, status.BatteryChargeStatus, status.BatteryLifePercent * 100));
}));
synthesizer.SpeakAsync("almost ready.");
theIcon.Visible = true;
System.Windows.Forms.Application.Run();
}
private static void Tick() {
var status=System.Windows.Forms.SystemInformation.PowerStatus;
if(status.PowerLineStatus == System.Windows.Forms.PowerLineStatus.Offline) {
synthesizer.SpeakAsync("power line disconnected!");
theIcon.Icon = System.Drawing.SystemIcons.Error;
} else {
theIcon.Icon = System.Drawing.SystemIcons.Application;
}
}
}
}