WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Dialog Boxes

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

WSHMessage UDF

 Keywords: WSHMessage UDF WSH Windows Scripting Host Popup Message Dialog Box Icons Auto Timeout YES NO CANCEL RETRY ABORT IGNORE AskYesNO WshShell Popup

Please try this UDF I wrote using the Windows Scripting Shell object to display a standard looking Windows message box with definable timeout, icons and buttons.
;==============
;WSHMessage UDF - use windows scripting to popup a message box with defined buttons, icons and auto timeout.   ....IFICantBYTE  reference: http://technet.microsoft.com/en-us/library/ee156593.aspx
;==============
;Timeout        = Seconds before the message automatically closes (would return a -1 when it does)
;TitleText      = The message box's window name - displayed in the window title
;MessageText    = The main message text you want to display
;DisplayFlags   = a value containing binary ORed (|) values to control the type of button options and icons displayed - see values listed below
;                 Icon values:    STOP = 16 , ? = 32 , ! = 48 , i = 64
;                 Button values:  OK = 0 , OK CANCEL = 1 , ABORT RETRY IGNORE = 2 , YES NO CANCEL = 3 , YES NO = 4 , RETRY CANCEL = 5
;                                 *The following values were found with experimentation and were NOT documented on the Microsoft site - they may be unavailable on some Windows versions - use at own risk
;                                 *CANCEL TRY AGAIN CONTINUE = 6 , *Add System menu icon to top left corner = 4096 , *Append HELP button in addition to other buttons = 16384 - doesn't return a value (guessing it needs a windows message event hook to detect)
;                 Default button: Left = 0 , Middle = 256 , Right = 512
; Developer: IFICantBYTE
;==============
;Returned Value = Timeout = -1 , OK = 1 , CANCEL = 2 , ABORT = 3 , RETRY = 4 , IGNORE = 5 , YES = 6 , NO = 7 , , , *TRY AGAIN = 10 , *CONTINUE = 11
;==============
#DefineFunction WSHMessage(Timeout,TitleText,MessageText,DisplayFlags)
oWSHShell = ObjectCreate("WScript.Shell")
Ret = oWSHShell.popup(MessageText,Timeout,TitleText,DisplayFlags)
ObjectClose(oWSHShell)
Return(Ret)
#EndFunction
;==============


:EXAMPLES
;A few different examples showing some of the possible display options and the returned values from each at the end.

Test1 = WSHMessage(5,"Test 1","5 second timeout with STOP icon and OK button.",16)
Test2 = WSHMessage(7,"Test 2","7 second timeout with QUESTION MARK icon and OK and CANCEL buttons.",32|1)
Test3 = WSHMessage(8,"Test 3","8 second timeout with EXCLAMATION icon and ABORT RETRY IGNORE buttons. IGNORE has been made the DEFAULT option.",48|2|512)
Test4 = WSHMessage(4,"Test 4","4 second timeout with NO icon and just an OK button.",0)

TestResults = StrCat("Here is a list of the values returned by each previous test message",@CRLF,"-1 is a timeout, other values depend on the button that was pushed:",@CRLF,Test1,@CRLF,Test2,@CRLF,Test3,@CRLF,Test4)

WSHMessage(0,"Test Results:",TestResults,64); Show the returned values from the above tests with No timeout

Again = WSHMessage(0,"Do you want to show them again?","Do you want to go again? %@CRLF% Press YES to do the examples again, NO to quit",32|4)

If Again <> 6 Then Exit ; If anything other than 6 (YES) was returned, then Exit
Goto EXAMPLES ; go around again

Exit


Example 2

;   ----------------------------------------------------------------------------
;   Script NameL        PopUp_Demo.WBT
;   Synopsis:           Demonstrate the PopUp method of the WshShell object.
;   Author:             David A. Gray
;   Date Written:       Sunday, 10 March 2013
;   ----------------------------------------------------------------------------

    WAIT_FOREVER                    = 0

    ;   ------------------------------------------------------------------------
    ;   The commnents to the right of each constant list the corresponding
    ;   MessageBox constant. Hence, you should be able to drop this into code in
    ;   place of a MessageBox. Indeed, if you use WAIT_FOREVER as the value of
    ;   the iTimeout argument, the two behave identically.
    ;   ------------------------------------------------------------------------

    BTN_OK                          = 0                         ; MB_OK (Unless overridden, this sets MB_APPLMODAL and MB_DEFBUTTON1.)
    BTN_OK_CANCEL                   = 1                         ; MB_OKCANCEL
    BTN_OK_RETRY_IGNORE             = 2                         ; MB_ABORTRETRYIGNORE
    BTN_YES_NO_CANCEL               = 3                         ; MB_YESNOCANCEL
    BTN_YES_NO                      = 4                         ; MB_YESNO
    BTN_RETRY_CANCEL                = 5                         ; MB_RETRYCANCEL
    BTN_CANCEL_TRY_AGAIN_CONTINUE   = 6                         ; MB_CANCELTRYCONTINUE

    ICON_STOP                       = 16                        ; MB_ICONSTOP or MB_ICONERROR or MB_ICONHAND
    ICON_QUESTION                   = 32                        ; MB_ICONQUESTION
    ICON_EXCLAMATION                = 48                        ; MB_ICONEXCLAMATION or MB_ICONWARNING
    ICON_INFORMATION                = 64                        ; MB_ICONINFORMATION or MB_ICONASTERISK

    FLAG_DEFAULT_SECOND             = 256                       ; MB_DEFBUTTON2
    FLAG_DEFAULT_THIRD              = 512                       ; MB_DEFBUTTON3 (MB_DEFBUTTON4 is unsupported.)
    FLAG_SYSTEM_MODAL               = 4096                      ; MB_SYSTEMMODAL (MB_TASKMODAL, MB_DEFAULT_DESKTOP_ONLY, MB_SETFOREGROUND, and MB_SERVICE_NOTIFICATION are unsupported.)
    FLAG_RIGHT_JUSTIFY_TEXT         = 524288                    ; MB_RIGHT
    FLAG_RIGHT_TO_LEFT_TEXT         = 1048576                   ; MB_RTLREADING

    CLICKED_NONE                    = -1                        ; Since it doesn't time out, MessageBox never returns this value.
    CLICKED_OK                      = 1                         ; IDOK
    CLICKED_CANCEL                  = 2                         ; IDCANCEL
    CLICKED_ABORT                   = 3                         ; IDABORT
    CLICKED_RETRY                   = 4                         ; IDRETRY
    CLICKED_IGNORE                  = 5                         ; IDIGNORE
    CLICKED_YES                     = 6                         ; IDYES
    CLICKED_NO                      = 7                         ; IDNO
    CLICKED_TRY_AGAIN               = 10                        ; IDTRYAGAIN
    CLICKED_CONTINUE                = 11                        ; IDCONTINUE

    sProgramFQFN        = IntControl ( 1004 , 0 , 0 , 0 , 0 )
    sProgramDirNm       = FilePath ( sProgramFQFN )             ; This is the same as DirScript().
    sProgramName        = FileRoot ( sProgramFQFN )

    Drop ( sProgramFQFN )                                       ; No longer useful.

    oWshShell           = CreateObject ( 'WScript.Shell' )
    iTimeout            = 10
    nType               = BTN_YES_NO | ICON_EXCLAMATION | FLAG_DEFAULT_SECOND
    sTitle              = sProgramName

    iButton             = oWshShell.Popup ( 'Hello, world. This is the WScript.Shell object.' , iTimeout , sTitle , nType )

    iTimeout            = WAIT_FOREVER
    nType               = BTN_OK | ICON_INFORMATION
    sMessage            = 'The button you pressed was '

    Switch iButton
        Case CLICKED_NONE
            sMessage    = sMessage : 'None.'
            Break

        Case CLICKED_OK
            sMessage    = sMessage : 'OK.'
            Break

        Case CLICKED_CANCEL
            sMessage    = sMessage : 'Cancel.'
            Break

        Case CLICKED_ABORT
            sMessage    = sMessage : 'Abort.'
            Break

        Case CLICKED_RETRY
            sMessage    = sMessage : 'Retry.'
            Break

        Case CLICKED_IGNORE
            sMessage    = sMessage : 'Ignore.'
            Break

        Case CLICKED_YES
            sMessage    = sMessage : 'Yes.'
            Break

        Case CLICKED_NO
            sMessage    = sMessage : 'No.'
            Break

        Case CLICKED_TRY_AGAIN
            sMessage    = sMessage : 'Try Again.'
            Break

        Case CLICKED_CONTINUE
            sMessage    = sMessage : 'Continue'
            Break
    EndSwitch   ; switch iButton

    iButton             = oWshShell.Popup ( sMessage , iTimeout , sTitle , nType )



Article ID:   W18354
Filename:   WSHMessage UDF.txt
File Created: 2013:03:11:09:16:34
Last Updated: 2013:03:11:09:16:34