RSS

C Sharp: How to make your own Battery Monitor

08 Mar

My laptop’s battery is totally screwed, it gets shut down within a minute once the power is off… often when i’m working the charger plug gets disturbed and come out a bit and the power is cut off and I don’t even get to know!

Unfortunately the computer suddenly shuts down while I’m working and all my data and progress is lost 😦

I thus tried to make my own Battery Monitor to tackle this problem. Now a popup window appears as soon as my laptop comes on battery!!!

Here’s the way… try out making one for your PC too…



C# Battery Monitor:
1). Create a new C# windows application project in Visual Studio.
2). Resize the form to 500×280 px
3). From the properties box make the values of MaximizeBox and MinimizeBox equal to false.
4). Drag the following controls from the toolbox(press Ctrl+Alt+X to show toolbox):
a). 1 Button
b). 1 Check Box
c). 1 Timer
d). 1 Progress Bar
e). 5 Labels
g). 1 Group Box
h). 2 Picture Box
5). Apply two different images to your picture box one showing a charging battery and another a discharging battery.
6). Rename the controls as follows:
label1—>BatteryStatus
label2—>ChargeRemaining
label3—>labelAlert
progressBar1—>BatteryIndicator
button1—>buttonOK
checkBox1—>chkRun
pictureBox1—>pictureBoxBattery
pictureBox2—>pictureBoxCharging

7). Arrange all the controls to give your form the following look:

8). Note the the 2 picture boxes overlap each other.
9). Write the following code:
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Media;

namespace Battery
{
public partial class Battery : Form
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(“SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run”, true);
PowerStatus power = SystemInformation.PowerStatus;
int x,y;
public Battery()
{
InitializeComponent();
if (rkApp.GetValue(“MyApp”) == null)
{chkRun.Checked = false;}
else
{chkRun.Checked = true;}
float secondsRemaining = power.BatteryLifePercent;
if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + “% available.”;
}
else
{
ChargeRemaining.Text = string.Empty;
}
}

private void Battery_Load(object sender, EventArgs e)
{
RefreshStatus();
timer1.Enabled = true;
y = this.Left;
}

private void RefreshStatus()
{
int powerPercent = (int)(power.BatteryLifePercent * 100);
if (powerPercent <= 100) BatteryIndicator.Value = powerPercent; else BatteryIndicator.Value = 0; switch (power.PowerLineStatus)
{
case PowerLineStatus.Online: ;
groupBox1.Text = “Running On Mains”;
x = 1;
labelAlert.ForeColor = Color.Chartreuse;
labelAlert.Text=”Plugged In”;
pictureBoxCharging.Visible = true;
pictureBoxBattery.Visible = false;
float secondsRemaining = power.BatteryLifePercent;
if (secondsRemaining >= 0)
{
ChargeRemaining.Text = (secondsRemaining * 100).ToString() + “% available.”;
}
else
{
ChargeRemaining.Text = string.Empty;
}
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
break;

case PowerLineStatus.Offline:


groupBox1.Text = “Running On Battery”;
if (x == 1)
{
SoundPlayer simpleSound = new SoundPlayer(@”c:\Windows\Media\Windows Exclamation.wav”);
simpleSound.Play();
labelAlert.ForeColor = Color.Red;
labelAlert.Text = “On Battery”;
pictureBoxCharging.Visible = false;
pictureBoxBattery.Visible = true;
this.WindowState = FormWindowState.Normal;
this.Left = y;
BatteryStatus.Text = power.BatteryChargeStatus.ToString();
}
x = 0;
break;

case PowerLineStatus.Unknown:

break;
}

}

private void timer1_Tick(object sender, EventArgs e)
{
RefreshStatus();
}

private void buttonOK_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void chkRun_CheckedChanged(object sender, EventArgs e)
{
if (chkRun.Checked)
{
rkApp.SetValue(“MyApp”, Application.ExecutablePath.ToString());
}
else
{
rkApp.DeleteValue(“MyApp”, false);
}
}
}
}

 

Run your application! 🙂
While charging:

While running on battery:


Advertisements
 
Leave a comment

Posted by on March 8, 2011 in C#

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: