User Tools

Site Tools

blog:2024-10-30-001



2024-10-30 Share: [C#]使用GetSystemPowerStatus API查看目前電源使用狀態

Local Backup

  • 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();
            }
        }
    }

TAGS

  • 14 person(s) visited this page until now.

Permalink blog/2024-10-30-001.txt · Last modified: 2024/10/30 08:53 by jethro

oeffentlich