【问题标题】:Time out on msgboxmsgbox 超时
【发布时间】:2023-03-21 22:10:02
【问题描述】:

有没有办法给消息框添加超时,如果没有输入,它默认为 no?

我希望它在 1 小时后默认为否

@echo off
Call :YesNoBox "Are you sure you want to do that?"
if "%YesNo%"=="7" (
Call :MessageBox "You answered NO" "Heading"
exit /b
)

exit /b
:YesNoBox
REM returns 6 = Yes, 7 = No. Type=4 = Yes/No
set YesNo=
set MsgType=4
set heading=%~2
set message=%~1
echo wscript.echo msgbox(WScript.Arguments(0),%MsgType%,WScript.Arguments(1)) >"%temp%\input.vbs"
for /f "tokens=* delims=" %%a in ('cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"') do set YesNo=%%a
exit /b

:MessageBox
set heading=%~2
set message=%~1
echo msgbox WScript.Arguments(0),0,WScript.Arguments(1) >"%temp%\input.vbs"
cscript //nologo "%temp%\input.vbs" "%message%" "%heading%"
exit /b

【问题讨论】:

  • 您要处理消息框输出的批处理还是 vbsript 文件?在 vbs 中处理的一种方法是here
  • 为自己找到答案只需将 //T:36 添加到 "cscript //nologo //T:36 "%temp%\input.vbs" "%message%" "%heading%" " 会在 36 秒后超时
  • @user14875 - 巧妙的解决方法。
  • intButton = wshshell.Popup(strText,[nSecondsToWait],[strTitle],[nType]) 来自download.microsoft.com/download/winscript56/Install/5.6/…

标签: batch-file cmd vbscript timeout msgbox


【解决方案1】:

这是一个jscript/bat hybrid 脚本,带有是/否按钮弹出窗口。它使用popup 具有超时选项的对象:

 @if (@x)==(@y) @end /***** jscript comment ******
     @echo off

     cscript //E:JScript //nologo "%~f0" "%~nx0" %*
     exit /b 0

 @if (@x)==(@y) @end ******  end comment *********/


var wshShell = WScript.CreateObject("WScript.Shell");
var args=WScript.Arguments;
var title=args.Item(0);

var timeout=-1;
var message="";

function printHelp() {
    WScript.Echo(title + "[-title Title] [-timeout m] [-message \"pop-up message\"]");
    WScript.Echo(title + "if time out not defined will wait only for button pressing");
}

if (WScript.Arguments.Length==1){
    runPopup();
    WScript.Quit(0);
}

if (args.Item(1).toLowerCase() == "-help" ||  args.Item(1).toLowerCase() == "-h" ) {
    printHelp();
    WScript.Quit(0);
}

if (WScript.Arguments.Length % 2 == 0 ) {
    WScript.Echo("Illegal arguments ");
    printHelp();
    WScript.Quit(10);
}

for (var arg = 1 ; arg<args.Length;arg=arg+2) {

    if (args.Item(arg).toLowerCase() == "-title") {
        title = args.Item(arg+1);
    }

    if (args.Item(arg).toLowerCase() == "-timeout") {
        timeout = parseInt(args.Item(arg+1));
        if (isNaN(timeout)) {
            timeout=-1;
        }
    }


    if (args.Item(arg).toLowerCase() == "-message") {
        message = args.Item(arg+1);
    }
}

function runPopup(){
    var btn = wshShell.Popup(message, timeout, title, 0x4 + 0x20);
    //WScript.Echo(btn)
    switch(btn) {
        // yes pressed.
        case 6:
            WScript.Echo("yes");
            WScript.Quit(btn);
            break;
        // no  pressed.
        case 7:
            WScript.Echo("no");
            WScript.Quit(btn);
            break;

        // Timed out.
        case -1:
           WScript.Echo("timeout");
           WScript.Quit(btn);
           break;
    }
}

runPopup();

要使用它,请尝试:

for /f %%a in ('yesnopopup.bat -title "T.I.T.L.E." -timeout 5 -tom "timeout"  -message "Hello"') do (
  if "%%~a" equ "yes" echo YES PRESSED
  if "%%~a" equ "no" echo NO PRESSED
  if "%%~a" equ "timeout" echo TIMED OUT
)

我更喜欢使用 jscript/bat 混合,因为它使代码更易于阅读,它们使用单​​个文件,从而减少了 i/o 操作并且一切都更快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多