新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
Private Type SYSTEM_POWER_STATUS
ACLineStatus As Byte
BatteryFlag As Byte
BatteryLifePercent As Byte
Reserved1 As Byte
BatteryLifeTime As Long
BatteryFullLifeTime As Long
End Type
Private Sub Command1_Click()
Dim SysStatus As SYSTEM_POWER_STATUS, LifeTime As Double
GetSystemPowerStatus SysStatus
LifeTime = SysStatus.BatteryLifeTime
If LifeTime = -1 Then
MsgBox "Using AC Power"
Else
MsgBox LifeTime / 3600 & " hours remaining"
End If
End Sub