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

Commit 1371000

Browse files
author
Kenneth Schnall
committed
Add charging status, update code formatting, disable signing
1 parent 20cc010 commit 1371000

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

percentage/percentage/TrayIcon.cs

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ namespace percentage
77
{
88
class TrayIcon
99
{
10-
[DllImport("user32.dll", CharSet = CharSet.Auto)]
10+
[DllImport("user32.dll", CharSet=CharSet.Auto)]
1111
static extern bool DestroyIcon(IntPtr handle);
1212

13-
private const string iconFont = "Segoe UI";
14-
private const int iconFontSize = 14;
13+
private const int fontSize = 18;
14+
private const string font = "Segoe UI";
1515

16-
private string batteryPercentage;
1716
private NotifyIcon notifyIcon;
1817

1918
public TrayIcon()
@@ -23,82 +22,75 @@ public TrayIcon()
2322

2423
notifyIcon = new NotifyIcon();
2524

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

29-
// initialize menuItem
27+
menuItem.Click += new System.EventHandler(MenuItemClick);
3028
menuItem.Index = 0;
3129
menuItem.Text = "E&xit";
32-
menuItem.Click += new System.EventHandler(menuItem_Click);
3330

3431
notifyIcon.ContextMenu = contextMenu;
35-
36-
batteryPercentage = "?";
37-
3832
notifyIcon.Visible = true;
3933

4034
Timer timer = new Timer();
41-
timer.Tick += new EventHandler(timer_Tick);
42-
timer.Interval = 1000; // in miliseconds
35+
timer.Interval = 1000;
36+
timer.Tick += new EventHandler(TimerTick);
4337
timer.Start();
4438
}
4539

46-
private void timer_Tick(object sender, EventArgs e)
40+
private Bitmap GetTextBitmap(String text, Font font, Color fontColor)
4741
{
48-
PowerStatus powerStatus = SystemInformation.PowerStatus;
49-
batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString();
50-
51-
using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black)))
42+
SizeF imageSize = GetStringImageSize(text, font);
43+
Bitmap bitmap = new Bitmap((int)imageSize.Width, (int)imageSize.Height);
44+
using (Graphics graphics = Graphics.FromImage(bitmap))
5245
{
53-
System.IntPtr intPtr = bitmap.GetHicon();
54-
try
55-
{
56-
using (Icon icon = Icon.FromHandle(intPtr))
57-
{
58-
notifyIcon.Icon = icon;
59-
notifyIcon.Text = batteryPercentage + "%";
60-
}
61-
}
62-
finally
46+
graphics.Clear(Color.FromArgb(0, 0, 0, 0));
47+
using (Brush brush = new SolidBrush(fontColor))
6348
{
64-
DestroyIcon(intPtr);
49+
graphics.DrawString(text, font, brush, 0, 0);
50+
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
51+
graphics.Save();
6552
}
6653
}
54+
return bitmap;
55+
}
56+
57+
private static SizeF GetStringImageSize(string text, Font font)
58+
{
59+
using (Image image = new Bitmap(1, 1))
60+
using (Graphics graphics = Graphics.FromImage(image))
61+
return graphics.MeasureString(text, font);
6762
}
6863

69-
private void menuItem_Click(object sender, EventArgs e)
64+
private void MenuItemClick(object sender, EventArgs e)
7065
{
7166
notifyIcon.Visible = false;
7267
notifyIcon.Dispose();
7368
Application.Exit();
7469
}
7570

76-
private Image DrawText(String text, Font font, Color textColor, Color backColor)
71+
private void TimerTick(object sender, EventArgs e)
7772
{
78-
var textSize = GetImageSize(text, font);
79-
Image image = new Bitmap((int) textSize.Width, (int) textSize.Height);
80-
using (Graphics graphics = Graphics.FromImage(image))
73+
PowerStatus powerStatus = SystemInformation.PowerStatus;
74+
String percentage = (powerStatus.BatteryLifePercent * 100).ToString();
75+
bool isCharging = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online;
76+
String bitmapText = isCharging ? percentage + "*" : percentage;
77+
using (Bitmap bitmap = new Bitmap(GetTextBitmap(bitmapText, new Font(font, fontSize), Color.White)))
8178
{
82-
// paint the background
83-
graphics.Clear(backColor);
84-
85-
// create a brush for the text
86-
using (Brush textBrush = new SolidBrush(textColor))
79+
System.IntPtr intPtr = bitmap.GetHicon();
80+
try
8781
{
88-
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
89-
graphics.DrawString(text, font, textBrush, 0, 0);
90-
graphics.Save();
82+
using (Icon icon = Icon.FromHandle(intPtr))
83+
{
84+
notifyIcon.Icon = icon;
85+
String toolTipText = percentage + "%" + (isCharging ? " Charging" : "");
86+
notifyIcon.Text = toolTipText;
87+
}
88+
}
89+
finally
90+
{
91+
DestroyIcon(intPtr);
9192
}
9293
}
93-
94-
return image;
95-
}
96-
97-
private static SizeF GetImageSize(string text, Font font)
98-
{
99-
using (Image image = new Bitmap(1, 1))
100-
using (Graphics graphics = Graphics.FromImage(image))
101-
return graphics.MeasureString(text, font);
10294
}
10395
}
10496
}

percentage/percentage/percentage.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<GenerateManifests>true</GenerateManifests>
5959
</PropertyGroup>
6060
<PropertyGroup>
61-
<SignManifests>true</SignManifests>
61+
<SignManifests>false</SignManifests>
6262
</PropertyGroup>
6363
<ItemGroup>
6464
<Reference Include="System" />

0 commit comments

Comments
 (0)