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

Using Clear Type for better text readability & show remaining time in tooltip #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions percentage/percentage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static class Program
[STAThread]
static void Main()
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Expand Down
32 changes: 29 additions & 3 deletions percentage/percentage/TrayIcon.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Management;
using System.Runtime.InteropServices;
using System.Windows.Forms;

Expand Down Expand Up @@ -47,16 +49,40 @@ private void timer_Tick(object sender, EventArgs e)
{
PowerStatus powerStatus = SystemInformation.PowerStatus;
batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString();
bool charging = SystemInformation.PowerStatus.BatteryChargeStatus.HasFlag(BatteryChargeStatus.Charging);

using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black)))
Color fontColor;
if (charging)
{
fontColor = Color.FromArgb(255, 0, 255, 0);
}
else
{
fontColor = Color.FromArgb(255, 255, 255, 255);

}

using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), fontColor, Color.FromArgb(0, 0, 0, 0))))
{
System.IntPtr intPtr = bitmap.GetHicon();
try
{
using (Icon icon = Icon.FromHandle(intPtr))
{
notifyIcon.Icon = icon;
notifyIcon.Icon = icon;
notifyIcon.Text = batteryPercentage + "%";
if (!charging)
{
int seconds = SystemInformation.PowerStatus.BatteryLifeRemaining;
int mins = seconds / 60;
int hours = mins / 60;
mins = mins % 60;
notifyIcon.Text += "\n" + " " + hours + ":" + mins + " remaining";
}
else
{
notifyIcon.Text = "Charging";
}
}
}
finally
Expand Down Expand Up @@ -85,7 +111,7 @@ private Image DrawText(String text, Font font, Color textColor, Color backColor)
// create a brush for the text
using (Brush textBrush = new SolidBrush(textColor))
{
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphics.DrawString(text, font, textBrush, 0, 0);
graphics.Save();
}
Expand Down