Earlier I promised applications that use the WMI Win32_Battery class to display battery status. I started this series of articles with C# (Windows XP: Monitoring Batteries in C Sharp) so I will start with a C# application. For those of you who know me, I am not a user interface developer and will leave fancy GUIs to those of you who are better than I am. This application is a fairly simple Dialog box that can display charge remaining for one or two batteries, and shows whether the system is running on battery.
The Dialog looks like this for one battery running on battery:
With two batteries running on AC:
Most of the code is very straight forward, so let’s focus on the code specific to monitoring and displaying the battery status. The code that updates the dialog is in DisplayBatteryPercent()
        private void DisplayBatteryPercent()
        {
            if (GetSystemPowerStatus(PowerStatus))
            {
                this.RunningOnBattery.Checked = (ACLineStatus.Offline == PowerStatus._ACLineStatus);
                Batteries.Refresh();
                int NumBatteries = Batteries.NumberOfBatteries();
                if (NumBatteries > 0)
                    this.Battery1Percent.Text = Batteries.GetPercentChargeRemaining(0).ToString() + "%";
                else
                    this.Battery1Percent.Text = "No Info Available";
                if (NumBatteries > 1)
                {
                    Battery2groupBox.Show();
                    Battery2label.Show();
                    Battery2Percent.Show();
                    this.Battery2Percent.Text = Batteries.GetPercentChargeRemaining(1).ToString() + "%";
                }
                else
                {
                    Battery2groupBox.Hide();
                    Battery2label.Hide();
                    Battery2Percent.Hide();
                }
            }
        }
 
DisplayBatteryPercent() does the following:
1.       Call GetSystemPowerStatus() which returns status info about the battery and most important here determines if the system is running on battery or AC power.
2.       Sets the Running On Battery check box based if the system is running on battery.
3.       Uses the Win32_Battery class discussed in Windows XP: Monitoring Batteries in C Sharp to get the current battery status by calling Refresh()
4.       If a battery is found, sets the battery percent charge remaining
5.       If more than one batter is found, show the second battery information
Of course a system might have more than 2 batteries available, but I will leave that as an exercise for you.
DisplayBatteryPercent() is the meat of the application, but there is also some magic. The magic is in how DisplayBatteryPercent() is called. We could set up a timer to poll the battery, but running code unnecessarily while on battery is counterproductive. But the OS sends a message when the battery status changes, and we can use the message to instigate calling DisplayBatteryPercent().
The message is handled in C# as the event SystemEvents.PowerModeChanged, which I set in the function RegisterForPowerNotifications() and set the hander to be the function PowerModeChangedEventHandler().
        private void RegisterForPowerNotifications(IntPtr hwnd)
        {
            SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(XP_SystemEvents_PowerModeChanged);
        }
 
        //private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        private void XP_SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)      
        {
            PowerModes mode = e.Mode;
           
            if (PowerModes.StatusChange == mode)
            {
                DisplayBatteryPercent();
            }
        }
 
This is the first in a series of applications, all will mostly do the same thing, but in slightly different ways and programming languages. At the end, I will make the full source code available.
Copyright © 2010 – Bruce Eitman
All Rights Reserved