Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from jayotterbein/more-disposing
Browse files Browse the repository at this point in the history
More disposing
  • Loading branch information
Kenneth Schnall authored Sep 13, 2017
2 parents ee3f03e + 8d066f0 commit 08441f7
Showing 1 changed file with 44 additions and 37 deletions.
81 changes: 44 additions & 37 deletions percentage/percentage/TrayIcon.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 08441f7

Please sign in to comment.