[C# Helper]
Index Books FAQ Contact About Rod
[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]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

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

Title: Display battery status in a friendly way in C#

[Display battery status in a friendly way in C#]

The example Display battery status in C# shows how you can get the computer's battery status. This example shows a couple of different ways you can display the battery's status in user-friendly ways.

First the program uses the following code to display the status textually.

PowerStatus status = SystemInformation.PowerStatus; float percent = status.BatteryLifePercent; string percent_text = percent.ToString("P0"); if (status.PowerLineStatus == PowerLineStatus.Online) { if (percent < 1.0f) lblStatus.Text = percent_text + ", charging"; else lblStatus.Text = "Online fully charged"; } else { lblStatus.Text = "Offline, " + percent_text + " remaining"; }

This code displays the status in one of the following three easy-to-understand formats:

  • 75 %, charging
  • Online fully charged
  • Offline, 75 % remaining

(UI Tip: This text demonstrates a useful UI principle: Make different things look different. If the first and last messages were "75 %, charging" and "75 % remaining," then it would be easier to confuse the two. This is probably not the end of the world, but it would make the user waste precious brain cells examining the message more carefully.)

Next the program calls the following DrawBattery method four times to draw the graphical battery representations.

private Bitmap DrawBattery( float percent, int wid, int hgt, Color bg_color, Color outline_color, Color charged_color, Color uncharged_color, bool striped) { Bitmap bm = new Bitmap(wid, hgt); using (Graphics gr = Graphics.FromImage(bm)) { // If the battery has a horizontal orientation, // rotate so we can draw it vertically. if (wid > hgt) { gr.RotateTransform(90, MatrixOrder.Append); gr.TranslateTransform(wid, 0, MatrixOrder.Append); int temp = wid; wid = hgt; hgt = temp; } // Draw the battery. DrawVerticalBattery(gr, percent, wid, hgt, bg_color, outline_color, charged_color, uncharged_color, striped); } return bm; }

This method creates a bitmap of the desired size. If the width is greater than the height, the method assumes the battery should be drawn horizontally. (You could change that if you wanted to draw a really short fat battery.)

If the battery should be drawn horizontally, the code adds a rotation and translation to the Graphics object so the program can draw the battery vertically. If it should be horizontal, those transformations rotate the result sideways so it comes out correctly.

The method then calls the following DrawVerticalBattery method to draw a vertical battery.

// Draw a vertically oriented battery with // the indicated percentage filled in. private void DrawVerticalBattery(Graphics gr, float percent, int wid, int hgt, Color bg_color, Color outline_color, Color charged_color, Color uncharged_color, bool striped) { gr.Clear(bg_color); gr.SmoothingMode = SmoothingMode.AntiAlias; // Make a rectangle for the main body. float thickness = hgt / 20f; RectangleF body_rect = new RectangleF( thickness * 0.5f, thickness * 1.5f, wid - thickness, hgt - thickness * 2f); using (Pen pen = new Pen(outline_color, thickness)) { // Fill the body with the uncharged color. using (Brush brush = new SolidBrush(uncharged_color)) { gr.FillRectangle(brush, body_rect); } // Fill the charged area. float charged_hgt = body_rect.Height * percent; RectangleF charged_rect = new RectangleF( body_rect.Left, body_rect.Bottom - charged_hgt, body_rect.Width, charged_hgt); using (Brush brush = new SolidBrush(charged_color)) { gr.FillRectangle(brush, charged_rect); } // Optionally stripe multiples of 25% if (striped) for (int i = 1; i <= 3; i++) { float y = body_rect.Bottom - i * 0.25f * body_rect.Height; gr.DrawLine(pen, body_rect.Left, y, body_rect.Right, y); } // Draw the main body. gr.DrawPath(pen, MakeRoundedRect( body_rect, thickness, thickness, true, true, true, true)); // Draw the positive terminal. RectangleF terminal_rect = new RectangleF( wid / 2f - thickness, 0, 2 * thickness, thickness); gr.DrawPath(pen, MakeRoundedRect( terminal_rect, thickness / 2f, thickness / 2f, true, true, false, false)); } }

This method calculates a reasonable thickness for the battery's outline, makes a rectangle to represent the battery's body area, and creates a thick pen using the desired outline color.

Next the code fills the body area with the "uncharged" color, which is white in this example. The code then fills the part of the body that represents the percent that is charged.

If the striped parameter is true, the code then draws bars through the body representing 25%, 50%, and 75% charges.

The method finishes by using the MakeRoundedRect method described in the post Draw rounded rectangles in C# to draw the battery's body and the positive terminal (the little bump on the end).

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

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