[C#]使用GetSystemPowerStatus API查看目前电源使用状态

[C#]使用GetSystemPowerStatus API查看目前电源使用状态


Introduction

这篇简单记录一下,如何透过GetSystemPowerStatus API,来查看目前电源的使用状态。

Library

kernel32.dll

GetSystemPowerStatus API

GetSystemPowerStatus API可取得系统电源状态,包括使用交流电还是直流电、是否为充电状态、剩余可用时间等资讯。其函数原型如下:
BOOL WINAPI GetSystemPowerStatus(__out LPSYSTEM_POWER_STATUS lpSystemPowerStatus);

传入的参数为LPSYSTEM_POWER_STATUS型态的结构
typedef struct _SYSTEM_POWER_STATUS {
BYTE ACLineStatus;
BYTE BatteryFlag;
BYTE BatteryLifePercent;
BYTE Reserved1;
DWORD BatteryLifeTime;
DWORD BatteryFullLifeTime; } SYSTEM_POWER_STATUS,
*LPSYSTEM_POWER_STATUS;

 
结构成员所代表的意义简略如下:
BatteryFlag 表示目前充电状态。
1为High(66%以上电力)、2为Low(33%以上电力)、4为Critical(5%以下电力)、8为Charging、128为NoBattery、255为UnKnow
BatteryFullLifeTime 表示充满电力可使用多久时间(-1为不详)。
BatteryLifePercent 表示电力剩余多少百分比,其正常值为0~100,255为电力不详。
BatteryLifeTime 表示剩余电力可使用多久时间(-1为不详)。
ACLineStatus 表示电源状态。
0为offline、1为online、255为unknow。
 
使用时只要把LPSYSTEM_POWER_STATUS型态的结构传入GetSystemPowerStatus,再从传入的结构中取出感兴趣的数据就可以了。
 

Example

为了使用方便,也便于对照比较。这边我把GetSystemPowerStatus API包成了类似System.Windows.Form.PowerStatus的类。


using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Battery
{
    [Flags]
    public enum BatteryChargeStatus : byte
    {
        High = 1,
        Low = 2,
        Critical = 4,
        Charging = 8,
        NoSystemBattery = 128,
        Unknown = 255
    }

    public enum PowerLineStatus : byte
    {
        Offline = 0,
        Online = 1,
        Unknown = 255
    }

    class PowerStatus
    {
        [DllImport("kernel32", EntryPoint = "GetSystemPowerStatus")]
        private static extern void GetSystemPowerStatus(ref SystemPowerStatus powerStatus);
        
        private struct SystemPowerStatus
        {
            public PowerLineStatus PowerLineStatus;
            public BatteryChargeStatus BatteryChargeStatus;
            public Byte BatteryLifePercent;
            public Byte Reserved;
            public int BatteryLifeRemaining;
            public int BatteryFullLifeTime;
        }

        private SystemPowerStatus _powerStatus;

        public PowerLineStatus PowerLineStatus
        {
            get
            {
                return _powerStatus.PowerLineStatus;
            }
        }

        public BatteryChargeStatus BatteryChargeStatus
        {
            get
            {
                return _powerStatus.BatteryChargeStatus;
            }
        }

        public float BatteryLifePercent
        {
            get
            {
                return _powerStatus.BatteryLifePercent;
            }
        }

        public int BatteryLifeRemaining
        {
            get
            {
                return _powerStatus.BatteryLifeRemaining;
            }
        }

        public int BatteryFullLifeTime
        {
            get
            {
                return _powerStatus.BatteryFullLifeTime;
            }
        }

        public PowerStatus()
        {
            UpdatePowerInfo();
        }


        public void UpdatePowerInfo()
        {
            GetSystemPowerStatus(ref _powerStatus);
        }
    }
}

使用上跟.NET内建的PowerStatus大同小异。只要取得对应的属性值即可。


using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace BatteryPower
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void UpdateInfo()
        {
            Battery.PowerStatus power = new Battery.PowerStatus();
            this.tbxPowerStatus.Text = power.PowerLineStatus.ToString();
            this.tbxBatteryChargeStatus.Text = power.BatteryChargeStatus.ToString();
            this.tbxBatteryFullLifetime.Text = power.BatteryFullLifeTime.ToString();
            this.tbxBatteryLifePercent.Text = power.BatteryLifePercent.ToString();
            this.tbxBatteryLifeRemaining.Text = power.BatteryLifeRemaining.ToString();
        }

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

        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateInfo();
        }
    }
}

执行画面

image

Link

  • GetSystemPowerStatus Function
  • SYSTEM_POWER_STATUS Structure
  • C#透过Windows API取得NoteBook电池使用状况
  • 如何得知 Notebook中 电池 的使用状况

原文:大专栏  [C#]使用GetSystemPowerStatus API查看目前电源使用状态


猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11468544.html