用于显示电池状态的VB代码

时间:2013-10-13 13:20:16

标签: vb.net

我在VB 6.0中使用此代码显示电池状态。但显示'语法错误'我应该导入什么以及如何导入?

Dim power As PowerStatus = SystemInformation.PowerStatus
Dim percent As Single = power.BatteryLifePercent
MsgBox ("Percent battery life remaining: " & percent * 100)

1 个答案:

答案 0 :(得分:1)

如果您正在寻找VB6代码(我猜到了)那么我找到了代码here: -

Private Sub Form_Load()
    MsgBox "Battery status: " & getBatteryStatus(), vbInformation + vbOKOnly, "Battery Status"
    End
End Sub

Public Function getBatteryStatus() As Integer
  Dim obj As Object, obj2 As Object, stat As Integer
'  Get Battery Status
'  Return Value Meaning
'0 No battery
'1 The battery is discharging.
'2 The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging.
'3 Fully Charged
'4 Low
'5 Critical
'6 Charging
'7 Charging and High
'8 Charging and Low
'9 Charging and Critical
'10 Undefined
'11 Partially Charged
  stat = 0
  Set obj = GetObject("winmgmts:").InstancesOf("Win32_Battery")
  For Each obj2 In obj 'loop in objects
    stat = obj2.BatteryStatus
  Next
  getBatteryStatus = stat
End Function

在VB.NET中你可以尝试这样: -

'Assembly: System.Windows.Forms.dll
'Namespace: System
'Namespace: Microsoft.VisualBasic
'Namespace: System.Windows.Forms

Dim power As PowerStatus = SystemInformation.PowerStatus
Dim percent As Single = power.BatteryLifePercent
MsgBox("Percent battery life remaining: " & percent * 100)