Code: Select all
Private Structure SYSTEM_POWER_STATUS
Public ACLineStatus As Byte
Public BatteryFlag As Byte
Public BatteryLifePercent As Byte
Public Reserved1 As Byte
Public BatteryLifeTime As Integer
Public BatteryFullLifeTime As Integer
End Structure
Private Declare Function GetSystemPowerStatus Lib "kernel32" (ByRef lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Long
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim power_status As SYSTEM_POWER_STATUS
If GetSystemPowerStatus(power_status) = 0 Then
lblACStatus.Text = "Error"
Else
Select Case power_status.ACLineStatus
Case 0
lblACStatus.Text = "Battery"
Case 1
lblACStatus.Text = "On line"
Case 255
lblACStatus.Text = "Unknown"
End Select
Dim txt As String = ""
If power_status.BatteryFlag And 1 Then txt &= ", High (> 66%)"
If power_status.BatteryFlag And 2 Then txt &= ", Low (< 33%)"
If power_status.BatteryFlag And 4 Then txt &= ", Critical (< 5%)"
If power_status.BatteryFlag And 8 Then txt &= ", Charging"
If power_status.BatteryFlag And 128 Then txt &= ", No system battery"
If power_status.BatteryFlag = 255 Then txt &= ", Unknown"
If txt.Length > 0 Then txt = txt.Substring(2)
lblBatteryStatus.Text = txt
If power_status.BatteryFullLifeTime = -1 Then
lblFullLifetime.Text = "Unknown"
Else
lblFullLifetime.Text = power_status.BatteryFullLifeTime & " seconds"
End If
If power_status.BatteryLifeTime = -1 Then
lblRemainingLifetime.Text = "Unknown"
Else
lblRemainingLifetime.Text = power_status.BatteryLifeTime & " seconds"
End If
If power_status.BatteryLifePercent = 255 Then
lblPercentLifetime.Text = "Unknown"
Else
lblPercentLifetime.Text = power_status.BatteryLifePercent & "%"
End If
End If
End Sub