我想同时显示Win 7和Win 10的电池百分比。如果我只能做一个或另一个,那么Win10。我正在以VB风格使用.NET,但是我可以读取C#。>
我找到了SystemInformation.PowerStatus。电源模式更改有一个事件“ SystemEvents.PowerModeChanged”,但由于电池电量耗尽而不会触发。如果我插入/拔下充电器,则会触发该事件。 除非有必要,否则我尽量不要使用计时器。如果必须使用计时器,将其设置为1分钟应该不会占用太多资源。至少我认为不会。
基本上,我创建了一个用户控件,该控件具有标签,进度条和显示插头的图片框。如果不插入图片框,我将其隐藏起来。 我还添加了一个用于调试的计数器,以便可以查看是否触发了该事件,并且正在更新错误消息。仅当我插入和拔下充电器时才会触发。
Public Overrides Sub Refresh()
RefreshPowerStatus()
MyBase.Refresh()
End Sub
Dim tmpCount As Integer = 0
Public Sub RefreshPowerStatus()
Dim psBattery As PowerStatus = SystemInformation.PowerStatus
BatteryChargingImage.Visible = psBattery.PowerLineStatus = PowerLineStatus.Online
Sleep(50)
psBattery = SystemInformation.PowerStatus
Dim BattPercent As Integer = 0
Dim BattTime As String = ""
If psBattery.BatteryLifeRemaining = -1 Then
BattTime = ""
ElseIf psBattery.BatteryLifeRemaining > 3600 Then
BattTime = "Hr: " & Math.Round(psBattery.BatteryLifeRemaining / 3600, 2).ToString
Else
BattTime = "Min: " & Math.Round(psBattery.BatteryLifeRemaining / 60, 0).ToString
End If
If psBattery.BatteryChargeStatus = BatteryChargeStatus.Critical Then
pbBatteryInfo.ForeColor = Drawing.Color.Red
ElseIf psBattery.BatteryChargeStatus = BatteryChargeStatus.Low Then
pbBatteryInfo.ForeColor = Drawing.Color.Yellow
Else
pbBatteryInfo.ForeColor = Drawing.Color.Green
End If
BattPercent = Math.Round(psBattery.BatteryLifePercent * 100, 0)
If psBattery.BatteryChargeStatus = BatteryChargeStatus.NoSystemBattery Then
lblBatteryInfo.Text = "No Battery"
pbBatteryInfo.Visible = False
Return
ElseIf psBattery.BatteryChargeStatus = BatteryChargeStatus.Charging Then
lblBatteryInfo.Text = "Charging"
Else
lblBatteryInfo.Text = BattPercent.ToString & "% " & BattTime
End If
tmpCount += 1
lblBatteryInfo.Text &= " (" & tmpCount & ")"
pbBatteryInfo.Value = BattPercent
End Sub
这是我的设置代码...(所有SystemEvents_PowerModeChanged()所做的都是调用用户控件的刷新[以上代码]
Dim psBattery = SystemInformation.PowerStatus
Dim tmpDeviceHasNoBatt As Boolean = psBattery.BatteryChargeStatus = BatteryChargeStatus.NoSystemBattery
If IsDebug Then tmpDeviceHasNoBatt = False
If tmpDeviceHasNoBatt Then PowerStatusDisplay.Visible = False
If PowerStatusDisplay.Visible Then
Call PowerStatusDisplay.Refresh()
AddHandler SystemEvents.PowerModeChanged, AddressOf SystemEvents_PowerModeChanged
End If
我希望可以使用与计时器不同的事件。在当今的事件驱动世界中,计时器似乎很奇怪。但是,嘿,如果有谁可以争论。