GetSystemPowerStatus 填充 TSystemPowerStatus 結構,提供交流電狀態、電池旗標、電量百分比、剩餘/總運作秒數。適用於筆電專用軟體、電源管理工具、無人值守系統的優雅關機邏輯。
_SYSTEM_POWER_STATUS = packed record
ACLineStatus: Byte; // 0:斷電 1:正常 255:未知
BatteryFlag: Byte; // 1:充足 2:低 4:極低 8:充電中 128:無電池 255:未知
BatteryLifePercent: Byte; // 0..100, 255:未知
Reserved1: Byte; // 保留
BatteryLifeTime: DWORD; // 剩餘秒數, -1:未知
BatteryFullLifeTime: DWORD; // 總秒數, -1:未知
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Power: TSystemPowerStatus;
begin
GetSystemPowerStatus(Power);
Memo1.Clear;
with Memo1.Lines do
begin
Add(Format('交流電狀態: %d', [Power.ACLineStatus]));
Add(Format('電池旗標: %d', [Power.BatteryFlag]));
Add(Format('電量百分比: %d', [Power.BatteryLifePercent]));
Add(Format('剩餘秒數: %d', [Power.BatteryLifeTime]));
Add(Format('總秒數: %d', [Power.BatteryFullLifeTime]));
end;
end;

9 (1+8):充足且正在充電10 (2+8):低電量且充電中12 (4+8):極低電量緊急充電搭配 WM_POWERBROADCAST 訊息可即時回應電源狀態變化。