代码之家  ›  专栏  ›  技术社区  ›  ashleedawg

检索计算机的充电状态和当前电池电量

  •  6
  • ashleedawg  · 技术社区  · 7 年前

    我需要一个 VBA 检索系统的 当前电池充电水平和充电状态 .

    我很惊讶在堆栈溢出上没有发现任何东西,我最终找到了它,所以我将在这里以q+a的形式共享代码。

    1 回复  |  直到 7 年前
        1
  •  6
  •   FunThomas    7 年前
    Option Explicit
    
    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
        SystemStatusFlag As Byte
        BatteryLifeTime As Long
        BatteryFullLifeTime As Long
    End Type
    
    
    Public Sub getBatteryStatus()
    'prints current battery status to immediate window
    
        Dim SPS As SYSTEM_POWER_STATUS
        GetSystemPowerStatus SPS 'get system battery power status
    
        With SPS
            Debug.Print "Battery Life:  ", ;
            Select Case .BatteryLifePercent
                Case 255:   Debug.Print "Unknown"
                Case Else:  Debug.Print .BatteryLifePercent & "%"
            End Select
    
            Debug.Print "Battery Life Time: ", ;
            Select Case .BatteryLifeTime
                Case -1:    Debug.Print "Charging"
                Case Else
                    Debug.Print Int(.BatteryLifeTime / 60) & "min / ";
                    Select Case .BatteryFullLifeTime
                        Case -1
                            If .BatteryLifePercent = 0 Then
                                Debug.Print "Unknown"
                            Else 'estimate FullLifeTime:
                                Debug.Print "~" & Int(.BatteryLifeTime / .BatteryLifePercent * 5 / 3) & "min"
                            End If
                        Case Else
                            Debug.Print .BatteryFullLifeTime & "sec"
                    End Select
            End Select
    
            Debug.Print "AC power status: ", ;
            Select Case .ACLineStatus 'show some information
                Case 0:     Debug.Print "Offline"
                Case 1:     Debug.Print "OnLine"
                Case Else:  Debug.Print "Unknown"
            End Select
    
            Debug.Print "Battery charge status: ", ;
            Select Case .BatteryFlag
                Case 0:     Debug.Print "Not Charging (33-66%)"
                Case 1:     Debug.Print "High (>66%)"
                Case 2:     Debug.Print "Low (<33%)"
                Case 4:     Debug.Print "Critical (<5%)"
                Case 8:     Debug.Print "Charging"
                Case 1 + 8: Debug.Print "High (>66%)- Charging"
                Case 2 + 8: Debug.Print "Low (<33%) - Charging"
                Case 4 + 8: Debug.Print "Critical (<5%) - Charging"
                Case 128:   Debug.Print "No System Battery"
                Case 255:   Debug.Print "Unknown Status"
            End Select
    
            Debug.Print "Battery saver: ", ;
            Select Case .SystemStatusFlag
                Case 0:     Debug.Print "Off"
                Case 1:     Debug.Print "On (Save energy where possible)" 'Windows 10 only
            End Select
    
        End With
    End Sub
    

    更多信息:

    推荐文章