Answer:
Here is an easy way to get the power status for the current computer.
This code also works on desktops, but will only return information that the computer is connected to a power source. (AC)
All this information is reterived with the Windows API "GetSystemPowerStatus"
When running this code on a notebook you will get information such as:
- AC Line Status
- Battery status (High, Low, Critical, Charging, No battery)
- Battery Life in Percent (value between 0-100)
- Current time till battery uncharged (time in seconds)
- Battery time when fully charged. (time in seconds)
Below is the code. Modify Button1Click to your own needs.
----
function GetSystemPowerInfo(var PowerInfo : TSystemPowerStatus) : boolean;
begin
result := GetSystemPowerStatus(PowerInfo);
end;
procedure TForm1.Button1Click(Sender: TObject);
var PowerInfo : TSystemPowerStatus;
begin
FillChar(PowerInfo,SizeOf(TSystemPowerStatus),0);
if GetSystemPowerInfo(PowerInfo) then begin
Case PowerInfo.ACLineStatus of
0: Label1.Caption := 'Offline';
1: Label1.Caption := 'Online';
else
Label1.Caption := 'Unknown status.';
end;
Case PowerInfo.BatteryFlag of
1: Label2.Caption := 'High';
2: Label2.Caption := 'Low';
4: Label2.Caption := 'Critical';
8: Label2.Caption := 'Charging';
128: Label2.Caption := 'No system battery';
else
Label2.Caption := 'Unknown status';
end;
if PowerInfo.BatteryLifePercent <= 100 then begin
Label3.Caption := inttostr(PowerInfo.BatteryLifePercent )+'%';
end else Label3.Caption := 'No system battery';
if PowerInfo.BatteryLifeTime < MAXDWORD then begin
Label4.Caption := inttostr(PowerInfo.BatteryLifeTime div 60) + ' min';
end else Label4.Caption := 'No system battery';
if PowerInfo.BatteryFullLifeTime < MAXDWORD then begin
Label5.Caption := inttostr(PowerInfo.BatteryFullLifeTime div 60) + ' min';
end else Label5.Caption := 'No system battery';
end else Label1.Caption := 'Failed to get power status';
end;
---
|