How to detect when the laptop is running on batteries?

前端 未结 3 805
萌比男神i
萌比男神i 2021-02-08 07:27

How to detect (from Delphi) when the laptop is running on batteries (or AC)?

3条回答
  •  生来不讨喜
    2021-02-08 07:50

    To be notified when the status changes on Vista and Windows 7 you can use RegisterPowerSettingNotification.

    For Windows 2000 and later, look at GetSystemPowerStatus, or go to MSDN and read about Power Management.

    (Someone always posts while I am typing :-( )

    function GetBattery : Boolean;
    var
      SysPowerStatus: TSystemPowerStatus;
    begin
      Win32Check(GetSystemPowerStatus(SysPowerStatus));
      case SysPowerStatus.ACLineStatus of
        0: Result := False;
        1: begin
          Result := True;
          // You can return life with
          // String := Format('Battery power left: %u percent.', SysPowerStatus.BatteryLifePercent]);
        end;
        else
          raise Exception.Create('Unknown battery status');
      end;
    end;
    

提交回复
热议问题