Thema: Delphi Hat's einen Akku?

Einzelnen Beitrag anzeigen

OLDIE1950

Registriert seit: 2. Jan 2018
22 Beiträge
 
#9

AW: Hat's einen Akku?

  Alt 13. Sep 2018, 17:26
Das folgende Progrämmchen zeigt den Status in der Titelzeile an:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

const
 GUID_ACDC_POWER_SOURCE: TGUID =
 '{5D3E9A59-E9D5-4B00-A6BD-FF34FF516548}';
 PBT_POWERSETTINGCHANGE = 32787;

type
  PPowerbroadcastSetting = ^TPowerbroadcastSetting;
  POWERBROADCAST_SETTING = packed record
    PowerSetting: TGUID;
    DataLength: DWord;
    Data: array[0..0] of Char;
  end;
  {$EXTERNALSYM POWERBROADCAST_SETTING}
  TPowerbroadcastSetting = POWERBROADCAST_SETTING;

type
  TForm1 = class(TForm)
    procedure PowerMessage(var msg: TMessage); message
    WM_POWERBROADCAST;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

function RegisterPowerSettingNotification(hRecipient: THandle;
         PowerSettingGuid: PGUID; Flags: DWORD): THandle; stdcall;
external 'user32.dll';
function UnregisterPowerSettingNotification(Handle: THandle): BOOL; stdcall;
external 'user32.dll';

var
  Form1: TForm1;
  psn: THandle;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  psn := RegisterPowerSettingNotification(Form1.Handle,
  @GUID_ACDC_POWER_SOURCE , 0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   UnregisterPowerSettingNotification(psn);
end;

procedure TForm1.PowerMessage(var msg: TMessage);
var pb_setting: TPowerbroadcastSetting;
    buf: DWord;
begin
  if msg.WParam = PBT_POWERSETTINGCHANGE then
  begin
    pb_setting := PPowerbroadcastSetting(msg.LParam)^;
    if GUIDToString(pb_setting.PowerSetting) = GUIDToString(GUID_ACDC_POWER_SOURCE) then
    begin
      buf := 0;
      move(pb_setting.Data, buf, SizeOf(DWord));
      case buf of
        0: Caption:= 'Netzbetrieb';
        1: Caption:= 'Akkubetrieb';
        2: Caption:= 'USV-Betrieb';
      end;
    end;

  end;
end;

end.
  Mit Zitat antworten Zitat