[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Display battery status using a notify icon in C#

[Display battery status using a notify icon in C#]

The example Display battery status in a friendly way in C# shows how to draw an image of a battery to show battery status. This example displays a battery image and the battery's textual status in a notify icon.

In earlier versions of Windows, the battery status used to be easy to find in the task bar. In Windows 10 it sometimes seems to disappear. With a lot of work, you can sometimes turn it back on. (In Windows 11, it seems to be back, but who knows what tomorrow will bring?) Rather than go to all the trouble of restoring it every time it disappears, I decided to write my own status monitor for the task bar. That also allows you to customize its appearance if you like.

To build the program, I added a NotifyIcon component to the form. I also added a PictureBox, although you won't be able to see it at run time because the form is hidden. You can unhide the form to see the PictureBox for debugging purposes.

When the program starts, it uses the following code to completely hide the form.

// Hide the form. private void Form1_Load(object sender, EventArgs e) { FormBorderStyle = FormBorderStyle.None; Size = new Size(0, 0); ShowInTaskbar = false; WindowState = FormWindowState.Minimized; Hide(); ShowStatus(); }

This code is self-explanatory, except for the ShowStatus method shown in the following code.

// The last displayed charge percent. private int Percent = -1; // Update the battery status. private void ShowStatus() { // Get the current charge percent. PowerStatus status = SystemInformation.PowerStatus; int percent = (int)(status.BatteryLifePercent * 100); // If the percent is unchanged, do nothing. if (Percent == percent) return; Percent = percent; // Display the status in the NotifyIcon's text. niBatteryStatus.Text = percent.ToString() + "% " + status.PowerLineStatus.ToString(); // Change the icon. // Draw the battery image. int wid = picIcon.ClientSize.Width / 2; int hgt = picIcon.ClientSize.Height; Bitmap battery_bm = BatteryStuff.DrawBattery( percent / 100f, wid, hgt, Color.Transparent, Color.Black, Color.Lime, Color.White, true); // Convert the battery image into a square icon. Bitmap square_bm = new Bitmap(hgt, hgt); using (Graphics gr = Graphics.FromImage(square_bm)) { gr.Clear(Color.Transparent); Point[] dest = { new Point((int)(0.5 * wid), 0), new Point((int)(1.5 * wid), 0), new Point((int)(0.5 * wid), hgt), }; Rectangle source = new Rectangle(0, 0, wid, hgt); gr.DrawImage(battery_bm, dest, source, GraphicsUnit.Pixel); } picIcon.Image = square_bm; // Icon icon = Icon.FromHandle(square_bm.GetHicon()); niBatteryStatus.Icon = icon; }

The Percent variables keeps track of the last battery percentage that was displayed.

The ShowStatus method gets the battery's current charge percentage and compares it to the Percent variable. If the percentage hasn't changed, the method exits.

Next the method updates the NotifyIcon component's Text property. If you hover the mouse over the notify icon, it displays this text. That's the main way you can use the example to learn the battery status.

The method then uses the DrawBattery method to draw a small image of the battery. See the earlier example and download this one to see how that method works.

The DrawBattery method draws the battery image so it is relatively tall and thin, but an icon must be square. If you try to use an icon that isn't square, the system resizes it so it is square and that will distort it.

To avoid that distortion, the program draws the battery image onto the middle of a new square bitmap. The code then displays the square image in the form's PictureBox for debugging purposes.

Finally the method converts the square bitmap into an icon and sets the NotifyIcon component's Icon property to it.

The last part of the program lets the user close it. Because the form is completely hidden, there's no obvious way to make the program exit. At design time, I added a ContextMenu to the form and set the NotifyIcon component's ContextMenu property to it. The ContextMenu has a single menu item, Exit, that calls the form's Close method. Right-click the icon to make the menu appear so you can select Exit.

UI aside: This program demonstrates several UI principles:

  • Display qualitative values graphically (you have lots of power)
  • Display quantitative values textually (you have 87% left)
  • Hide extra detail until requested (via the tooltip)

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.