Display a message box with a timeout value

前端 未结 5 2058
轻奢々
轻奢々 2020-12-06 02:55

The question comes from code like this.

Set scriptshell = CreateObject(\"wscript.shell\")
    Const TIMEOUT_IN_SECS = 60
    Select Case scriptshell.popup(\"         


        
5条回答
  •  無奈伤痛
    2020-12-06 03:33

    Private Declare Function MsgBoxTimeout _
         Lib "user32" _
         Alias "MessageBoxTimeoutA" ( _
             ByVal hwnd As Long, _
             ByVal MsgText As String, _
             ByVal Title As String, _
             ByVal MsgBoxType As VbMsgBoxStyle, _
             ByVal wlange As Long, _
             ByVal Timeout As Long) _
        As Long
        Dim btnOK As Boolean
        Dim btnCancel As Boolean
        Dim MsgTimeOut As Boolean
    
    Option Explicit
    
    Sub Main
    
        AutoMsgbox("Message Text", "Title", vbOkCancel , 5) '5 sec TimeOut
    
        MsgBox("Pressed OK: " & btnOK & vbNewLine & "Pressed Cancel: " & btnCancel & vbNewLine &"MsgBox Timeout: " & MsgTimeOut)
    
    End Sub
    
    Function AutoMsgbox(MsgText , Title , MsgBoxType , Timeout)
    
        Dim ReturnValue
        Dim TimeStamp As Date
        TimeStamp = DateAdd("s",Timeout,Now)
        Dim MsgText1 As String
    
        Dim TimeOutCounter As Integer
    
        For TimeOutCounter = 0 To Timeout
    
            MsgText1 = MsgText & vbNewLine & vbNewLine & " Auto Selction in " & Timeout - TimeOutCounter & " [s]"
    
            ReturnValue =  MsgBoxTimeout(0 , MsgText1 , Title, MsgBoxType, 0 ,1000)
    
            Select Case ReturnValue
                Case 1
                    btnOK       = True
                    btnCancel   = False
                    btnAbort    = False
                    btnRetry    = False
                    btnIgnore   = False
                    btnYes      = False
                    btnNo       = False
                    MsgTimeOut  = False
                    Exit Function
                Case 2
                    btnOK       = False
                    btnCancel   = True
                    btnAbort    = False
                    btnRetry    = False
                    btnIgnore   = False
                    btnYes      = False
                    btnNo       = False
                    MsgTimeOut  = False
                    Exit Function
                Case 3
                    btnOK       = False
                    btnCancel   = False
                    btnAbort    = True
                    btnRetry    = False
                    btnIgnore   = False
                    btnYes      = False
                    btnNo       = False
                    MsgTimeOut  = False
                    Exit Function
                Case 4
                    btnOK       = False
                    btnCancel   = False
                    btnAbort    = False
                    btnRetry    = True
                    btnIgnore   = False
                    btnYes      = False
                    btnNo       = False
                    MsgTimeOut  = False
                    Exit Function
                Case 5
                    btnOK       = False
                    btnCancel   = False
                    btnAbort    = False
                    btnRetry    = False
                    btnIgnore   = True
                    btnYes      = False
                    btnNo       = False
                    MsgTimeOut  = False
                    Exit Function
                Case 6
                    btnOK       = False
                    btnCancel   = False
                    btnAbort    = False
                    btnRetry    = False
                    btnIgnore   = False
                    btnYes      = True
                    btnNo       = False
                    MsgTimeOut  = False
                    Exit Function
                Case 7
                    btnOK       = False
                    btnCancel   = False
                    btnAbort    = False
                    btnRetry    = False
                    btnIgnore   = False
                    btnYes      = False
                    btnNo       = True
                    MsgTimeOut  = False
                    Exit Function
                Case 32000
                    btnOK       = False
                    btnCancel   = False
                    btnAbort    = False
                    btnRetry    = False
                    btnIgnore   = False
                    btnYes      = False
                    btnNo       = False
                    MsgTimeOut  = True
    
        Next TimeOutCounter
    
    End Function
    

提交回复
热议问题