diff --git a/percentage/percentage/TrayIcon.cs b/percentage/percentage/TrayIcon.cs index d3643c6..e96f396 100644 --- a/percentage/percentage/TrayIcon.cs +++ b/percentage/percentage/TrayIcon.cs @@ -9,13 +9,13 @@ class TrayIcon { [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool DestroyIcon(IntPtr handle); - private const string iconFont = "Segoe UI"; private const int iconFontSize = 14; - private string batteryPercentage; private NotifyIcon notifyIcon; + private int refreshInterval = 1000; // in miliseconds + public TrayIcon() { ContextMenu contextMenu = new ContextMenu(); @@ -28,18 +28,15 @@ public TrayIcon() // initialize menuItem menuItem.Index = 0; - menuItem.Text = "E&xit"; + menuItem.Text = "Exit"; menuItem.Click += new System.EventHandler(menuItem_Click); - notifyIcon.ContextMenu = contextMenu; - batteryPercentage = "?"; - notifyIcon.Visible = true; Timer timer = new Timer(); timer.Tick += new EventHandler(timer_Tick); - timer.Interval = 1000; // in miliseconds + timer.Interval = refreshInterval; timer.Start(); } @@ -47,16 +44,25 @@ private void timer_Tick(object sender, EventArgs e) { PowerStatus powerStatus = SystemInformation.PowerStatus; batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString(); - - using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black))) + + using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Transparent))) { System.IntPtr intPtr = bitmap.GetHicon(); try { using (Icon icon = Icon.FromHandle(intPtr)) { - notifyIcon.Icon = icon; - notifyIcon.Text = batteryPercentage + "%"; + // Offline means the laptop is not connected to a power source + if (powerStatus.PowerLineStatus.ToString().Equals("Offline")) { + notifyIcon.Icon = icon; + var ts = TimeSpan.FromSeconds(powerStatus.BatteryLifeRemaining); + + // if you don't want the leading zeros, you can replace the format by '{0}:{1}' + notifyIcon.Text = string.Format("{0:00}:{1:00}", ts.Hours, ts.Minutes); + } else { + notifyIcon.Icon = icon; + notifyIcon.Text = "Charging"; + } } } finally diff --git a/percentage/percentage/percentage.csproj b/percentage/percentage/percentage.csproj index 5711585..7770dc8 100644 --- a/percentage/percentage/percentage.csproj +++ b/percentage/percentage/percentage.csproj @@ -48,18 +48,7 @@ prompt 4 - - 5EAFB344C4489CCD86726031EE568ACE22CF26C4 - - - percentage_TemporaryKey.pfx - - - true - - - true - +