Skip to content

Commit

Permalink
Added dispose and custom DestroyIcon call for the items created in th…
Browse files Browse the repository at this point in the history
…e timer_Tick method following the documentation on MSDN: https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.gethicon(v=vs.110).aspx
  • Loading branch information
Jay Otterbein committed Sep 12, 2017
1 parent b1d2cb4 commit 8d066f0
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 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 @@ -47,10 +51,18 @@ private void timer_Tick(object sender, EventArgs e)
using (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 + "%";
try
{
using (Icon icon = Icon.FromHandle(intPtr))
{
notifyIcon.Icon = icon;
notifyIcon.Text = batteryPercentage + "%";
}
}
finally
{
DestroyIcon(intPtr);
}
}
}

Expand Down

0 comments on commit 8d066f0

Please sign in to comment.