有没有办法在消息框中添加超时,所以如果没有输入则默认为否?
我希望它在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
答案 0 :(得分:2)
这是一个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操作,一切都更快。