Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Clipz - A Friendly Introduction to the Windows 7 Taskbar Features

, 17 Dec 2009 CPOL
An overview of the Windows 7 taskbar features, and how to use then in your own applications.
Clipz.zip
Clipz
bin
Debug
Clipz.csproj.user
CopyIcon.ico
DeleteIcon.ico
Model
Native Methods
Properties
Settings.settings
Resources
Audio.png
Clipboard.png
NextIcon.ico
PreviousIcon.ico
Utility
Core
AppRestartRecovery
Dialogs
Common
TaskDialogs
Interop
AppRestartRecovery
Dialogs
NetworkList
PowerManagement
TaskDialogs
NetworkList
PowerManagement
Properties
PropertySystem
SafeHandles
Libraries
StructureMap.dll
Shell
Common
CommonFileDialogs
Controls
Design
ShellObjects.cd
ShellThumbnailClassDiagram.cd
DesktopWindowManager
ExplorerBrowser
ExplorerBrowserDiagram.cd
Interop
Common
Dialogs
ExplorerBrowser
KnownFolders
PropertySystem
StockIcons
Taskbar
KnownFolders
Properties
PropertySystem
StockIcons
Taskbar
//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;

namespace Microsoft.WindowsAPICodePack.ApplicationServices
{
    /// <summary>
    /// A snapshot of the state of the battery.
    /// </summary>
    public class BatteryState
    {
        private bool acOnline;
        private int maxCharge;
        private int currentCharge;
        private int dischargeRate;
        private TimeSpan estimatedTimeRemaining;
        private int suggestedCriticalBatteryCharge;
        private int suggestedBatteryWarningCharge;

        internal BatteryState()
        {
            PowerManagementNativeMethods.SystemBatteryState battState = Power.GetSystemBatteryState();
            acOnline = battState.AcOnLine;
            maxCharge = (int)battState.MaxCapacity;
            currentCharge = (int)battState.RemainingCapacity;
            dischargeRate = (int)battState.Rate;
            long estimatedTime = (long)battState.EstimatedTime;
            int minutes = (int)(estimatedTime/60);
            int seconds = (int)(estimatedTime % 60);
            estimatedTimeRemaining = new TimeSpan(0, minutes, seconds);
            suggestedCriticalBatteryCharge = (int)battState.DefaultAlert1;
            suggestedBatteryWarningCharge = (int)battState.DefaultAlert2;
        }

        #region Public properties

        /// <summary>
        /// Gets a value that indicates whether the battery charger is 
        /// operating on external power.
        /// </summary>
        /// <value>A <see cref="System.Boolean"/> value. <b>True</b> indicates the battery charger is operating on AC power.</value>
        public bool ACOnline 
        { 
            get 
            {
                return acOnline;
            } 
        }

        /// <summary>
        /// Gets the maximum charge of the battery (in mW).
        /// </summary>
        /// <value>An <see cref="System.Int32"/> value.</value>
        public int MaxCharge 
        { 
            get 
            {
                return maxCharge;
            } 
        }

        /// <summary>
        /// Gets the current charge of the battery (in mW).
        /// </summary>
        /// <value>An <see cref="System.Int32"/> value.</value>
        public int CurrentCharge 
        { 
            get 
            {
                return currentCharge;
            } 
        }

        /// <summary>
        /// Gets the rate of discharge for the battery (in mW). 
        /// </summary>
        /// <remarks>
        /// A negative value indicates the
        /// charge rate. Not all batteries support charge rate.
        /// </remarks>
        /// <value>An <see cref="System.Int32"/> value.</value>
        public int DischargeRate 
        { 
            get 
            {
                return dischargeRate;
            } 
        }

        /// <summary>
        /// Gets the estimated time remaining until the battery is empty.
        /// </summary>
        /// <value>A <see cref="System.TimeSpan"/> object.</value>
        public TimeSpan EstimatedTimeRemaining 
        { 
            get 
            {
                return estimatedTimeRemaining;
            } 
        }

        /// <summary>
        /// Gets the manufacturer's suggested battery charge level 
        /// that should cause a critical alert to be sent to the user.
        /// </summary>
        /// <value>An <see cref="System.Int32"/> value.</value>
        public int SuggestedCriticalBatteryCharge 
        { 
            get 
            {
                return suggestedCriticalBatteryCharge;
            } 
        }

        /// <summary>
        /// Gets the manufacturer's suggested battery charge level
        /// that should cause a warning to be sent to the user.
        /// </summary>
        /// <value>An <see cref="System.Int32"/> value.</value>
        public int SuggestedBatteryWarningCharge 
        { 
            get 
            {
                return suggestedBatteryWarningCharge;
            } 
        }

        #endregion

        /// <summary>
        /// Generates a string that represents this <b>BatteryState</b> object.
        /// </summary>
        /// <returns>A <see cref="System.String"/> representation of this object's current state.</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object[])"
            , Justification = "We are not currently handling globalization or localization")]
        public override string ToString()
        {
            return string.Format(
                "ACOnline: {1}{0}Max Charge: {2} mWh{0}Current Charge: {3} mWh{0}Discharge Rate: {4} mWh{0}Estimated Time Remaining: {5}{0}Suggested Critical Battery Charge: {6} mWh{0}Suggested Battery Warning Charge: {7} mWh{0}",
                Environment.NewLine, 
                acOnline, 
                maxCharge, 
                currentCharge, 
                dischargeRate, 
                estimatedTimeRemaining, 
                suggestedCriticalBatteryCharge, 
                suggestedBatteryWarningCharge
                );
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

TylerBrinks
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!
 
http://www.pagelabs.com
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150301.1 | Last Updated 17 Dec 2009
Article Copyright 2009 by TylerBrinks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid