Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Embarcadero Delphi – Battery Indicator/Check

Posted on January 5, 2017 By David Kittell

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.

Related

Code Delphi

Post navigation

Previous post
Next post

Related Posts

Create Shortcut (VB, VB.Net, PowerShell)

Posted on December 14, 2013December 10, 2019

Sub Create_ShortCut(ByVal TargetPath As String, ByVal ShortCutPath As String, ByVal ShortCutname As String, Optional ByVal WorkPath As String = "", Optional ByVal Window_Style As Short = 0, Optional ByVal IconNum As Short = 0) Dim VbsObj As Object VbsObj = CreateObject("WScript.Shell") Dim MyShortcut As Object ShortCutPath = ShortCutPath MyShortcut =…

Read More

WordPress – Ninja Forms – Disable Form Saves

Posted on December 18, 2015December 21, 2015

If you have a concern about PHI or other concerns of saving data on your SQL database of the Ninja Forms follow the step on their site http://docs.ninjaforms.com/customer/en/portal/articles/2045697-faq#faq_save on the save-sub.php file. The save-sub.php location may vary based on your operating system but if you are on a UNIX based…

Read More

Reorganize Directory By File Count

Posted on May 26, 2020August 17, 2024

$filesperfolder = 1000 $sourcePath = "c:\Pictures" $destPath = "c:\Pictures\imgOrg" $i = 0 $folderNum = 1 if (!(Test-Path "$destPath")) { New-Item -Path "$destPath" -Type Directory -Force } Get-ChildItem "$sourcePath\*.jpg" | % { New-Item -Path ($destPath + "\" + $folderNum) -Type Directory -Force Move-Item $_ ($destPath + "\" + $folderNum) -Verbose $i++…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • OpenSSL Generate Salt, Key and IV
  • PowerShell - Backup / Restore IIS Site and Configuration
  • PowerShell - UNIX SED Equivalent - Change Text In File
  • PowerShell - Get .NET Framework / Core Version

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • OpenSSL Generate Salt, Key and IV
  • PowerShell - Backup / Restore IIS Site and Configuration
  • PowerShell - UNIX SED Equivalent - Change Text In File
  • PowerShell - Get .NET Framework / Core Version
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes
David Kittell
  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support