如何检测笔记本电脑何时使用电池?

WeG*_*ars 11 delphi

如何检测(来自Delphi)笔记本电脑何时使用电池(或AC)运行?

Rea*_*cal 16

要在Vista和Windows 7上状态发生变化时收到通知,您可以使用RegisterPowerSettingNotification.

对于Windows 2000及更高版本,请查看GetSystemPowerStatus或访问MSDN并阅读有关电源管理的信息.

(有人总是在我打字时发帖:-()

function GetBattery : Boolean;
var
  SysPowerStatus: TSystemPowerStatus;
begin
  Win32Check(GetSystemPowerStatus(SysPowerStatus));
  case SysPowerStatus.ACLineStatus of
    0: Result := False;
    1: begin
      Result := True;
      // You can return life with
      // String := Format('Battery power left: %u percent.', SysPowerStatus.BatteryLifePercent]);
    end;
    else
      raise Exception.Create('Unknown battery status');
  end;
end;
Run Code Online (Sandbox Code Playgroud)