windows - VBScript 关闭消息困惑

标签 windows vba vbscript

我目前正在学习如何在 Visual Basic 中编写命令,并决定制作一个简单的关机消息。该代码完美运行:当用户单击"is"时,它会关闭,而“否”、“取消”和“X”按钮会关闭消息。 但是,我决定也尝试制作一个恶作剧消息,无论选择什么选项,计算机都会关闭。我运行了脚本,但是当我单击“X”图标时(我没想到要关闭 我的 电脑!),我的电脑还是关机了 :( 有没有办法阻止这种情况的发生,或者更好的是,有没有办法让“X”图标变灰,这样用户就无法关闭消息? 这是代码:

Option Explicit
Dim result
result = MsgBox ("Do you want to shutdown?", 3+48,"Warning")
Dim objShell
Select Case result
    Case vbYes
        MsgBox("shuting down ...")
        Set objShell = WScript.CreateObject("WScript.Shell")
        objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 20"
    Case vbNo
        MsgBox("shuting down ...")
        Set objShell = WScript.CreateObject("WScript.Shell")
        objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 20"
    Case vbCancel
        MsgBox("shuting down ...")
        Set objShell = WScript.CreateObject("WScript.Shell")
        objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 20"
End Select

提前干杯! :D

最佳答案

-r : 重启

-s : 关闭

此 Vbscript 可以在您的桌面上创建一个快捷方式,询问您是否要关闭计算机。

Option Explicit
Dim MyScriptPath 
MyScriptPath = WScript.ScriptFullName
Call Shortcut(MyScriptPath,"Shutdown the computer")
Call AskQuestion()
'**********************************************************************************************
Sub Shortcut(PathApplication,Name)
    Dim objShell,DesktopPath,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If Name = "" Then
        Name = MyTab(UBound(MyTab))
    End if
    DesktopPath = objShell.SpecialFolders("Desktop")
    Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
    objShortCut.TargetPath = Dblquote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-28"
    objShortCut.Save
End Sub
'**********************************************************************************************
Sub AskQuestion()
    Dim Question,Msg,Title
    Title = "Shutdown the computer"
    Msg = "Are you sure to shutdown the computer now ?"& Vbcr &_
    "If yes, then click [YES] button "& Vbcr &_
    "If not, then click [NO] button"
    Question = MsgBox (Msg,VbYesNo+VbQuestion,Title)
    If Question = VbYes then
        Call Run_Shutdown(30)
    else
        WScript.Quit()
    End if
End Sub
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub Run_Shutdown(N)
    Dim ws,Command,Execution
    Set ws = CreateObject("wscript.Shell")
    Command = "Cmd /c Shutdown -s -t "& N &" -c "& DblQuote("Save your work because your PC will shut down in "& N &" seconds")
    Execution = ws.run(Command,0,True)
End sub
'**********************************************************************************************

关于windows - VBScript 关闭消息困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33855528/

相关文章:

ms-access - Microsoft Access 2007 和 2010 : "Run-Time Error ' 42 9': ActiveX component can' t create object"

excel - 在 Excel 函数中将十六进制字符串转换为 base64

c# - C# 中的 VBScript 错误处理

javascript - 在 JavaScript 中使用枚举器

events - 向我的 COM 对象的 VBScript 用户公开事件处理程序

python - 从 git bash for windows 激活 venv

java - Inno 安装程序 : Extending Windows default apps list

c++ - CreateProcess 不会使用参数启动进程

c++ - LDAP函数中有没有不使用ldap_search获取用户信息的函数?

excel - VBA数据层错误处理