如何得知 Notebook中 電池 的使用狀況

如何得知 Notebook中 電池 的使用狀況

如何取得 Notebook 中 電池 的剩餘使用量

VB.Net 寫法 :

方法 1 :

呼叫 API 取得電池目前使用狀況 ( 計量 )

' API 宣告

Private Declare Function GetSystemPowerStatus Lib "kernel32" _

(ByRef lpSystemPowerStatus As SYSTEM_POWER_STATUS) As Integer

' 自訂型態

Private Structure SYSTEM_POWER_STATUS

Dim ACLineStatus As Byte

Dim BatteryFlag As Byte

Dim BatteryLifePercent As Byte

Dim Reserved1 As Byte

Dim BatteryLifeTime As Integer

Dim BatteryFullLifeTime As Integer

End Structure

Private Sub cmdBatteryLife_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs) _

Handles cmdBatteryLife.Click

Dim SysPower As SYSTEM_POWER_STATUS

Dim strLifeTime, strLifePercent As String

GetSystemPowerStatus(SysPower) ' Call API 取得系統電池計量

' BatteryLifeTime 剩餘時間 (秒)

' BatteryLifePercent 剩餘 %

With SysPower

MessageBox.Show("估計電力剩餘 : " & M2H(.BatteryLifeTime \ 60), .BatteryLifePercent & " %")

End With

End Sub

================================================================

方法 2 :

透過 WMI 取得電池目前使用狀況 ( 計量 )

Private Sub cmdGetBattery_Click(ByVal Sender As System.Object, ByVal eventArgs As System.EventArgs) _

Handles cmdGetBattery.Click

Dim objWMIcls, obj As Object

objWMIcls = GetObject("winmgmts:").InstancesOf("Win32_Battery")

' EstimatedRunTime 剩餘時間 (分鐘)

' EstimatedChargeRemaining 剩餘 %

For Each obj In objWMIcls

MessageBox.Show("估計電力剩餘 : " & M2H(obj.EstimatedRunTime), obj.EstimatedChargeRemaining & " %")

Next obj

obj = Nothing

objWMIcls = Nothing

End Sub

' 將 分鐘 轉成 N 時 N 分

Private Function M2H(ByVal strMM As String) As String

If CDbl(strMM) > 60 Then

M2H = CStr(CDbl(strMM) \ 60)

strMM = CStr(CDbl(strMM) Mod 60)

If CDbl(strMM) = 0 Then

M2H = M2H & " 時 "

Else

M2H = M2H & " 時 " & strMM & " 分 "

End If

End If

End Function

================================================================

VB6 寫法 :

方法 1 :

呼叫 API 取得電池目前使用狀況 ( 計量 )

' API 宣告

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 cmdBatteryLife_Click()

Dim SysPower As SYSTEM_POWER_STATUS

Dim strLifeTime As String, strLifePercent As String

GetSystemPowerStatus SysPower ' Call API 取得系統電池計量

' BatteryLifeTime 剩餘時間 (秒)

' BatteryLifePercent 剩餘 %

With SysPower

MsgBox "估計電力剩餘 : " & M2H(.BatteryLifeTime \ 60), , .BatteryLifePercent & " %"

End With

End Sub

================================================================

方法 2 :

透過 WMI 取得電池目前使用狀況 ( 計量 )

Private Sub cmdGetBattery_Click()

Dim objWMIcls As Object, obj As Object

Set objWMIcls = GetObject("winmgmts:").InstancesOf("Win32_Battery")

' EstimatedRunTime 剩餘時間 (分鐘)

' EstimatedChargeRemaining 剩餘 %

For Each obj In objWMIcls

MsgBox "估計電力剩餘 : " & M2H(obj.EstimatedRunTime), , obj.EstimatedChargeRemaining & " %"

Next

Set obj = Nothing

Set objWMIcls = Nothing

End Sub

================================================================

' 將 N 分鐘 轉成 N 時 N 分

Private Function M2H(ByVal strMM As String) As String

If strMM > 60 Then

M2H = strMM \ 60

strMM = strMM Mod 60

If strMM = 0 Then

M2H = M2H & " 時 "

Else

M2H = M2H & " 時 " & strMM & " 分 "

End If

End If

End Function