Embarcadero Delphi – Battery Indicator/Check

|

If your application runs on a computer or device that has a battery it’s helpful to know the battery level.

Setup your application canvas with 3 labels, 1 Gauge (progress bar) and 1 timer.

To keep it simple I’m not changing the names of the elements so you should have: Label1, Label2, Label3, Gauge1 and Timer1

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Gauges, ExtCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Gauge1: TGauge;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
var
pSysPowerStatus : SYSTEM_POWER_STATUS;
s               : string;
begin
 if GetSystemPowerStatus(pSysPowerStatus) then
  begin
    if pSysPowerStatus.ACLineStatus = 0 then
    begin
      showmessage('Please plug into power before continuing.');
    end;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
pSysPowerStatus : SYSTEM_POWER_STATUS;
s               : string;
begin
// WinApi command to get the status of the battery.
  if GetSystemPowerStatus(pSysPowerStatus) then
  begin

    // get the line status
    case pSysPowerStatus.ACLineStatus of
      0   : s:='Offline';
      1        : s:='Online';
      255 : s:='Unknown status.';
    end;
    label1.caption:='AC power status: '+s;

    // get the battery flag
    // Battery charge status. This parameter can be a combination of the following values:
    s:='';
    if pSysPowerStatus.BatteryFlag and 1 = 1 then s:=s+'High ';
    if pSysPowerStatus.BatteryFlag and 2 = 2 then s:=s+'Low ';
    if pSysPowerStatus.BatteryFlag and 4 = 4 then s:=s+'Critical ';
    if pSysPowerStatus.BatteryFlag and 8 = 8 then s:=s+'Charging ';
    if pSysPowerStatus.BatteryFlag and 128 = 128 then s:=s+'No system battery ';
    if pSysPowerStatus.BatteryFlag and 255 = 255 then s:=s+'Unknown status ';
    label2.caption:='Battery charge status: '+s;

    // BatteryLifePercent
    // Percentage of full battery charge remaining.
    // This member can be a value in the range 0 to 100, or 255 if status is unknown.

    case pSysPowerStatus.BatteryLifePercent of
      0..100 : s:=inttostr(pSysPowerStatus.BatteryLifePercent)+' %';
      255    : s:='unknown';
    end;
    label3.caption:='Percentage of full battery charge remaining: '+s;
    if pSysPowerStatus.BatteryLifePercent<101 then // status known
    begin
      gauge1.MinValue:=0;
      gauge1.MaxValue:=100;
      gauge1.Progress:=pSysPowerStatus.BatteryLifePercent;
    end;
  end else
  begin
    // error, could not grab the SYSTEM POWER STATUS
  end;

end;

end.
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.