gpt4 book ai didi

windows - 谁能帮我修复 Windows 10 上的 VBS 脚本,该脚本在电池电量不足时显示警告/MsgBox?

转载 作者:行者123 更新时间:2023-12-02 16:04:57 26 4
gpt4 key购买 nike

我想做的是编写一个脚本,在我的计算机电池电量降至 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(和一般编程)还很陌生,所以我不太确定如何解决这个问题。

最佳答案

您需要有一个标志,例如一个名为 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

关于windows - 谁能帮我修复 Windows 10 上的 VBS 脚本,该脚本在电池电量不足时显示警告/MsgBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69680278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com