diff --git a/percentage/percentage/TrayIcon.cs b/percentage/percentage/TrayIcon.cs index f23d772..d3643c6 100644 --- a/percentage/percentage/TrayIcon.cs +++ b/percentage/percentage/TrayIcon.cs @@ -1,11 +1,15 @@ using System; using System.Drawing; +using System.Runtime.InteropServices; using System.Windows.Forms; namespace percentage { 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; @@ -44,54 +48,57 @@ private void timer_Tick(object sender, EventArgs e) PowerStatus powerStatus = SystemInformation.PowerStatus; batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString(); - Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black)); - - System.IntPtr intPtr = bitmap.GetHicon(); - Icon icon = Icon.FromHandle(intPtr); - - notifyIcon.Icon = icon; - notifyIcon.Text = batteryPercentage + "%"; + using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black))) + { + System.IntPtr intPtr = bitmap.GetHicon(); + try + { + using (Icon icon = Icon.FromHandle(intPtr)) + { + notifyIcon.Icon = icon; + notifyIcon.Text = batteryPercentage + "%"; + } + } + finally + { + DestroyIcon(intPtr); + } + } } private void menuItem_Click(object sender, EventArgs e) { + notifyIcon.Visible = false; + notifyIcon.Dispose(); Application.Exit(); } private Image DrawText(String text, Font font, Color textColor, Color backColor) { - // create a dummy bitmap to get a graphics object - Image image = new Bitmap(1, 1); - Graphics graphics = Graphics.FromImage(image); - - // measure the string to see how big the image needs to be - SizeF textSize = graphics.MeasureString(text, font); - - // free up the dummy image and old graphics object - image.Dispose(); - graphics.Dispose(); - - // create a new image of the right size - image = new Bitmap((int) textSize.Width, (int) textSize.Height); - - graphics = Graphics.FromImage(image); - - // paint the background - graphics.Clear(backColor); - - // create a brush for the text - Brush textBrush = new SolidBrush(textColor); - - graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; - - graphics.DrawString(text, font, textBrush, 0, 0); - - graphics.Save(); - - textBrush.Dispose(); - graphics.Dispose(); + var textSize = GetImageSize(text, font); + Image image = new Bitmap((int) textSize.Width, (int) textSize.Height); + using (Graphics graphics = Graphics.FromImage(image)) + { + // paint the background + graphics.Clear(backColor); + + // create a brush for the text + using (Brush textBrush = new SolidBrush(textColor)) + { + graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + graphics.DrawString(text, font, textBrush, 0, 0); + graphics.Save(); + } + } return image; } + + private static SizeF GetImageSize(string text, Font font) + { + using (Image image = new Bitmap(1, 1)) + using (Graphics graphics = Graphics.FromImage(image)) + return graphics.MeasureString(text, font); + } } }