谁能帮助我修复Windows 10上显示低电池警告/MsgBox的VBS脚本?



我要做的是写一个脚本,创建一个消息框/弹出时,我的电脑的电池下降到20%。我希望消息框只出现一次,它不应该在我点击"确定"后再次出现。除非我给电脑充电,让它再次耗尽20%的电量。

问题是我的代码导致消息框每隔几分钟出现,而我的电池低于20%。我的代码如下:

set oLocator = CreateObject("WbemScripting.SWbemLocator")
set oServices = oLocator.ConnectServer(".","rootwmi")
set oResults = oServices.ExecQuery("select * from batteryfullchargedcapacity")

for each oResult in oResults
iFull = oResult.FullChargedCapacity
next
while (1) 
set oResults = oServices.ExecQuery("select * from batterystatus")
for each oResult in oResults
iRemaining = oResult.RemainingCapacity
bCharging = oResult.Charging
next
iPercent = Round((iRemaining / iFull) * 100)

if iRemaining and not bCharging and (iPercent < 21) and (iPercent > 10) Then msgbox "Your battery power is low (" & iPercent & "%). If you need to continue using your computer, either plug in your computer, or shut it down and then change the battery.",vbInformation, "Your battery is running low."
wscript.sleep 30000 ' 5 minutes
wend

我对VBScript(和一般编程)非常陌生,所以我不确定如何解决这个问题。

您需要一个标志,例如一个名为iShow的变量。它从值1开始(消息可以弹出),单击OK后,iShow将为零(不再显示)。下一次这个标志的值为1是当电池电量超过21%时,所以当它再次下降到不足21%时,消息可以再次弹出,但只有一次。下面是代码:

set oLocator = CreateObject("WbemScripting.SWbemLocator")
set oServices = oLocator.ConnectServer(".","rootwmi")
set oResults = oServices.ExecQuery("select * from batteryfullchargedcapacity")
for each oResult in oResults
iFull = oResult.FullChargedCapacity
next
Dim iShow
iShow=1
while (1) 
set oResults = oServices.ExecQuery("select * from batterystatus")
for each oResult in oResults
iRemaining = oResult.RemainingCapacity
bCharging = oResult.Charging
next
iPercent = Round((iRemaining / iFull) * 100)

if (iPercent>21) then iShow=1
if (iShow=1) and not bCharging and (iPercent < 21) Then
msgbox "Your battery power is low (" & iPercent & "%). If you need to continue using your computer, either plug in your computer, or shut it down and then change the battery.",vbInformation, "Your battery is running low."
iShow=0
end if
wscript.sleep 30000 ' 5 minutes
wend

相关内容

  • 没有找到相关文章

最新更新