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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// 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.
[…] is a minor update to the example Display battery status in a friendly way in C#. That example periodically checks the battery’s charge. It then draws textual and graphical […]