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

Time remaining hover text #6

Open
wants to merge 20 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
28 changes: 17 additions & 11 deletions percentage/percentage/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ 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;

private string batteryPercentage;
private NotifyIcon notifyIcon;

private int refreshInterval = 1000; // in miliseconds

public TrayIcon()
{
ContextMenu contextMenu = new ContextMenu();
Expand All @@ -28,35 +28,41 @@ public TrayIcon()

// initialize menuItem
menuItem.Index = 0;
menuItem.Text = "E&xit";
menuItem.Text = "Exit";
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 = refreshInterval;
timer.Start();
}

private void timer_Tick(object sender, EventArgs e)
{
PowerStatus powerStatus = SystemInformation.PowerStatus;
batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString();

using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black)))
using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Transparent)))
{
System.IntPtr intPtr = bitmap.GetHicon();
try
{
using (Icon icon = Icon.FromHandle(intPtr))
{
notifyIcon.Icon = icon;
notifyIcon.Text = batteryPercentage + "%";
// Offline means the laptop is not connected to a power source
if (powerStatus.PowerLineStatus.ToString().Equals("Offline")) {
notifyIcon.Icon = icon;
var ts = TimeSpan.FromSeconds(powerStatus.BatteryLifeRemaining);

// if you don't want the leading zeros, you can replace the format by '{0}:{1}'
notifyIcon.Text = string.Format("{0:00}:{1:00}", ts.Hours, ts.Minutes);
} else {
notifyIcon.Icon = icon;
notifyIcon.Text = "Charging";
}
}
}
finally
Expand Down
13 changes: 1 addition & 12 deletions percentage/percentage/percentage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>5EAFB344C4489CCD86726031EE568ACE22CF26C4</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>percentage_TemporaryKey.pfx</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>true</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<SignManifests>true</SignManifests>
</PropertyGroup>

<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down