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

ProgressBar Control

Posted on September 18, 2013October 26, 2015

Imports System Imports System.Collections Imports System.Data Imports System.Drawing Imports System.Windows.Forms Imports System.ComponentModel Imports System.Drawing.Drawing2D Imports System.IO Public Class MainClass Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub End Class Public Class Progress Inherits System.Windows.Forms.UserControl #Region " Windows Form Designer generated code " Public Sub New()…

Read More

Ektron Show duplicate content

Posted on August 11, 2015October 26, 2015

If you have more than one person in the site administration you can sometimes get duplicates, this script will help you find them. SET NOCOUNT ON DECLARE @Domain VARCHAR(50) SET @Domain = ‘/’ SELECT c.[content_title], CASE content_type WHEN – 1 THEN CAST(content_type AS VARCHAR(5)) + ‘ – AllTypes’ WHEN 1…

Read More

Mac OSX Terminal – Xcode Reset

Posted on December 5, 2016December 13, 2016

Make sure Xcode is closed. defaults delete com.apple.dt.Xcode rm -rfv ~/Library/Application\ Support/Developer/Shared/Xcode rm -rfv ~/Library/Saved\ Application\ State/com.apple.dt.Xcode.savedState rm -rfv ~/Library/Preferences/com.apple.dt.Xcode.* rm -rfv ~/Library/Preferences/com.apple.dt.xcodebuild.plist rm -rfv ~/Library/MobileDevice/Provisioning\ Profiles/* Originally Posted on December 5, 2016Last Updated on December 13, 2016 All information on this site is shared with the intention to help….

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

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
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes
 

Loading Comments...
 

You must be logged in to post a comment.