From 6fbbf25eae5956b606dc692b968498325cb2a1ac Mon Sep 17 00:00:00 2001 From: Lorenz Stangier Date: Wed, 24 Jan 2018 10:36:34 +0100 Subject: [PATCH 1/3] Changed to cleartype and displaying green text when charging --- percentage/percentage/Program.cs | 1 + percentage/percentage/TrayIcon.cs | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/percentage/percentage/Program.cs b/percentage/percentage/Program.cs index 0e58ed0..0c5741e 100644 --- a/percentage/percentage/Program.cs +++ b/percentage/percentage/Program.cs @@ -11,6 +11,7 @@ static class Program [STAThread] static void Main() { + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); diff --git a/percentage/percentage/TrayIcon.cs b/percentage/percentage/TrayIcon.cs index d3643c6..825f16c 100644 --- a/percentage/percentage/TrayIcon.cs +++ b/percentage/percentage/TrayIcon.cs @@ -47,8 +47,15 @@ private void timer_Tick(object sender, EventArgs e) { PowerStatus powerStatus = SystemInformation.PowerStatus; batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString(); + BatteryChargeStatus batteryChargeStatus = SystemInformation.PowerStatus.BatteryChargeStatus; - using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black))) + Color fontColor; + if (batteryChargeStatus.HasFlag(BatteryChargeStatus.Charging)) + fontColor = Color.FromArgb(255, 0, 255, 0); + else + fontColor = Color.FromArgb(255, 255, 255, 255); + + using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), fontColor, Color.FromArgb(0, 0, 0, 0)))) { System.IntPtr intPtr = bitmap.GetHicon(); try @@ -85,7 +92,7 @@ private Image DrawText(String text, Font font, Color textColor, Color backColor) // create a brush for the text using (Brush textBrush = new SolidBrush(textColor)) { - graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; graphics.DrawString(text, font, textBrush, 0, 0); graphics.Save(); } From a3c0c8fe62dc4a8b4ed12d5f508d796ae228b8d0 Mon Sep 17 00:00:00 2001 From: Lorenz Stangier Date: Wed, 24 Jan 2018 16:53:48 +0100 Subject: [PATCH 2/3] Remaining time now shown in tooltip --- percentage/percentage/TrayIcon.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/percentage/percentage/TrayIcon.cs b/percentage/percentage/TrayIcon.cs index 825f16c..d73c2bb 100644 --- a/percentage/percentage/TrayIcon.cs +++ b/percentage/percentage/TrayIcon.cs @@ -47,10 +47,10 @@ private void timer_Tick(object sender, EventArgs e) { PowerStatus powerStatus = SystemInformation.PowerStatus; batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString(); - BatteryChargeStatus batteryChargeStatus = SystemInformation.PowerStatus.BatteryChargeStatus; + bool charging = SystemInformation.PowerStatus.BatteryChargeStatus.HasFlag(BatteryChargeStatus.Charging); Color fontColor; - if (batteryChargeStatus.HasFlag(BatteryChargeStatus.Charging)) + if (charging) fontColor = Color.FromArgb(255, 0, 255, 0); else fontColor = Color.FromArgb(255, 255, 255, 255); @@ -62,8 +62,16 @@ private void timer_Tick(object sender, EventArgs e) { using (Icon icon = Icon.FromHandle(intPtr)) { - notifyIcon.Icon = icon; + notifyIcon.Icon = icon; notifyIcon.Text = batteryPercentage + "%"; + if (!charging) + { + int seconds = SystemInformation.PowerStatus.BatteryLifeRemaining; + int mins = seconds / 60; + int hours = mins / 60; + mins = mins % 60; + notifyIcon.Text += "\n" + " " + hours + ":" + mins + " remaining"; + } } } finally From 8b092ca0816db2da1f0f788c56629818473d3e9b Mon Sep 17 00:00:00 2001 From: Lorenz Stangier Date: Mon, 19 Feb 2018 09:41:45 +0100 Subject: [PATCH 3/3] Better charging behaviour --- percentage/percentage/TrayIcon.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/percentage/percentage/TrayIcon.cs b/percentage/percentage/TrayIcon.cs index d73c2bb..aa5dd8e 100644 --- a/percentage/percentage/TrayIcon.cs +++ b/percentage/percentage/TrayIcon.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Drawing; +using System.Management; using System.Runtime.InteropServices; using System.Windows.Forms; @@ -51,10 +53,15 @@ private void timer_Tick(object sender, EventArgs e) Color fontColor; if (charging) + { fontColor = Color.FromArgb(255, 0, 255, 0); + } else + { fontColor = Color.FromArgb(255, 255, 255, 255); + } + using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), fontColor, Color.FromArgb(0, 0, 0, 0)))) { System.IntPtr intPtr = bitmap.GetHicon(); @@ -72,6 +79,10 @@ private void timer_Tick(object sender, EventArgs e) mins = mins % 60; notifyIcon.Text += "\n" + " " + hours + ":" + mins + " remaining"; } + else + { + notifyIcon.Text = "Charging"; + } } } finally