我想做的是编写一个脚本,当我的计算机电池电量降至 20% 时,该脚本会创建一个消息框/弹出窗口。我希望消息框仅出现一次\xe2\x80\x94,在单击“确定”后,它不应再次出现,除非我为计算机充电并让它再次耗尽 20%。
\n问题是,当我的电池电量低于 20% 时,我的代码会导致消息框每隔几分钟出现一次。我的代码如下:
\nset oLocator = CreateObject("WbemScripting.SWbemLocator")\nset oServices = oLocator.ConnectServer(".","root\\wmi")\nset oResults = oServices.ExecQuery("select * from batteryfullchargedcapacity")\n \nfor each oResult in oResults\n iFull = oResult.FullChargedCapacity\nnext\n\nwhile (1) \n set oResults = oServices.ExecQuery("select * from batterystatus")\n for each oResult in oResults\n iRemaining = oResult.RemainingCapacity\n bCharging = oResult.Charging\n next\n iPercent = Round((iRemaining / iFull) * 100)\n \n 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."\n wscript.sleep 30000 ' 5 minutes\nwend\nRun Code Online (Sandbox Code Playgroud)\n我对 VBScript(以及一般编程)非常陌生,所以我不太确定如何解决这个问题。
\n小智 6
您需要有一个标志,例如名为 iShow 的变量。它以值 1 开始(可以弹出消息),单击“确定”后,iShow 将为零(不再显示)。下一次该标志的值为 1 时是当电池电量超过 21% 时,因此当电池电量再次降至 21% 以下时,该消息可能会再次弹出,但仅弹出一次。这是代码:
set oLocator = CreateObject("WbemScripting.SWbemLocator")
set oServices = oLocator.ConnectServer(".","root\wmi")
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
Run Code Online (Sandbox Code Playgroud)