Код: Выделить всё
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure UpdateInfo(Info: TStrings);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// При старте программы выводим текущую информацию
UpdateInfo(Memo1.Lines);
end;
procedure TForm1.UpdateInfo(Info: TStrings);
// Получаем и выводим информацию о батарее в TStrings
var
st: TSystemPowerStatus;
s: string;
begin
GetSystemPowerStatus(st); // Получить информацию
case st.ACLineStatus of // текущее подключение
0: s := 'от батареи';
1: s := 'от сети';
else s := 'не известно';
end;
Info.Add('Питание'#9 + s);
case st.BatteryFlag of // Уровень заряда батареи
0: s := 'нормальный';
1: s := 'высокий'; // >= 66%
2: s := 'низкий'; // <= 33%
4: s := 'критический'; // <=5%
8: s := 'заряжается';
128: s := 'батарея отсутствует';
else s := 'нет информации';
end;
Info.Add('Заряд батарей'#9 + s);
Info.Add('Уровень заряда'#9 + IntToStr(st.BatteryLifePercent) + '%'); // В %
if Integer(st.BatteryLifeTime) < 0 then s := 'неизвестно' // В секундах
else s := TimeToStr(st.BatteryLifeTime / SecsPerDay);
Info.Add('Время работы от батерей'#9 + s);
if Integer(st.BatteryFullLifeTime) = -1 then s := 'неизвестно' // Макс. время батареи
else s := TimeToStr(st.BatteryFullLifeTime / SecsPerDay);
Info.Add('Максимальное время работы'#9 + s);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
// При нажатии кнопки обновляем данные
Memo1.Clear;
UpdateInfo(Memo1.Lines);
end;
end.