我想要做的是撰寫一個腳本,當我的計算機電池電量下降到 20% 時創建一個訊息框/彈出視窗。我希望訊息框只出現一次 - 在我單擊“確定”后它不應該再次出現,除非我給我的電腦充電并讓它再次耗盡 20%。
問題是當我的電池電量低于 20% 時,我擁有的代碼會導致訊息框每隔幾分鐘出現一次。我的代碼如下:
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
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(以及一般的編程)很陌生,所以我不確定如何解決這個問題。
uj5u.com熱心網友回復:
您需要有一個標志,例如一個名為 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/335568.html
