How to detect (from Delphi) when the laptop is running on batteries (or AC)?
To be notified when the status changes on Vista and Windows 7 you can use RegisterPowerSettingNotification.
For Windows 2000 and later, look at GetSystemPowerStatus, or go to MSDN and read about Power Management.
(Someone always posts while I am typing :-( )
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;