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

Commit

Permalink
Add charging status, update code formatting, disable signing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Schnall committed May 1, 2021
1 parent 20cc010 commit 1371000
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
92 changes: 42 additions & 50 deletions percentage/percentage/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ namespace percentage
{
class TrayIcon
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern bool DestroyIcon(IntPtr handle);

private const string iconFont = "Segoe UI";
private const int iconFontSize = 14;
private const int fontSize = 18;
private const string font = "Segoe UI";

private string batteryPercentage;
private NotifyIcon notifyIcon;

public TrayIcon()
Expand All @@ -23,82 +22,75 @@ public TrayIcon()

notifyIcon = new NotifyIcon();

// initialize contextMenu
contextMenu.MenuItems.AddRange(new MenuItem[] { menuItem });

// initialize menuItem
menuItem.Click += new System.EventHandler(MenuItemClick);
menuItem.Index = 0;
menuItem.Text = "E&xit";
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 = 1000;
timer.Tick += new EventHandler(TimerTick);
timer.Start();
}

private void timer_Tick(object sender, EventArgs e)
private Bitmap GetTextBitmap(String text, Font font, Color fontColor)
{
PowerStatus powerStatus = SystemInformation.PowerStatus;
batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString();

using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black)))
SizeF imageSize = GetStringImageSize(text, font);
Bitmap bitmap = new Bitmap((int)imageSize.Width, (int)imageSize.Height);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
System.IntPtr intPtr = bitmap.GetHicon();
try
{
using (Icon icon = Icon.FromHandle(intPtr))
{
notifyIcon.Icon = icon;
notifyIcon.Text = batteryPercentage + "%";
}
}
finally
graphics.Clear(Color.FromArgb(0, 0, 0, 0));
using (Brush brush = new SolidBrush(fontColor))
{
DestroyIcon(intPtr);
graphics.DrawString(text, font, brush, 0, 0);
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
graphics.Save();
}
}
return bitmap;
}

private static SizeF GetStringImageSize(string text, Font font)
{
using (Image image = new Bitmap(1, 1))
using (Graphics graphics = Graphics.FromImage(image))
return graphics.MeasureString(text, font);
}

private void menuItem_Click(object sender, EventArgs e)
private void MenuItemClick(object sender, EventArgs e)
{
notifyIcon.Visible = false;
notifyIcon.Dispose();
Application.Exit();
}

private Image DrawText(String text, Font font, Color textColor, Color backColor)
private void TimerTick(object sender, EventArgs e)
{
var textSize = GetImageSize(text, font);
Image image = new Bitmap((int) textSize.Width, (int) textSize.Height);
using (Graphics graphics = Graphics.FromImage(image))
PowerStatus powerStatus = SystemInformation.PowerStatus;
String percentage = (powerStatus.BatteryLifePercent * 100).ToString();
bool isCharging = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online;
String bitmapText = isCharging ? percentage + "*" : percentage;
using (Bitmap bitmap = new Bitmap(GetTextBitmap(bitmapText, new Font(font, fontSize), Color.White)))
{
// paint the background
graphics.Clear(backColor);

// create a brush for the text
using (Brush textBrush = new SolidBrush(textColor))
System.IntPtr intPtr = bitmap.GetHicon();
try
{
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
graphics.DrawString(text, font, textBrush, 0, 0);
graphics.Save();
using (Icon icon = Icon.FromHandle(intPtr))
{
notifyIcon.Icon = icon;
String toolTipText = percentage + "%" + (isCharging ? " Charging" : "");
notifyIcon.Text = toolTipText;
}
}
finally
{
DestroyIcon(intPtr);
}
}

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);
}
}
}
2 changes: 1 addition & 1 deletion percentage/percentage/percentage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<GenerateManifests>true</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<SignManifests>true</SignManifests>
<SignManifests>false</SignManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down

0 comments on commit 1371000

Please sign in to comment.