我有 3 个批处理文件。其中之一需要用户输入。我想知道如何才能拥有一个带有 3 个按钮(每个批处理文件 1 个)和一个用于用户输入的文本框(将由其中一个批处理文件使用)的 GUI?
我正在尝试使用 this answer使其如下图所示:
<!-- :: Batch section
@echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<body bgcolor="cyan">
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick="call C:\Users\user1\Documents\bat1.bat";closeHTA(1);>Bat1</button>
<button onclick="call C:\Users\user1\Documents\bat2.bat;closeHTA(2);">Bat2</button>
<button onclick="call C:\Users\user1\Documents\bat3.bat;closeHTA(3);">Bat3</button>
</BODY>
</HTML>
我面临几个问题:
最佳答案
尝试这样:
<!-- :: Batch section
@echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<body bgcolor="cyan">
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,100);
function closeHTA(reply){
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(reply);
window.close();
}
function callShellApplication(command){
var r = new ActiveXObject("WScript.Shell");
var res=r.Exec(command);
new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(res.StdOut.ReadLine());
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
<button onclick='callShellApplication("C:\\test.bat")'>Bat1</button>
<button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat2</button>
<button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat3</button>
</BODY>
</HTML>
使用 javascript 或 html 时,您需要使用双反斜杠。您不能直接从 HTA 调用 bat,但需要使用 WScript.Shell 对象
。
编辑要传递参数(如评论中所要求的),您可以尝试类似的操作:
<!-- :: Batch section
@echo off
setlocal
echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"
echo End of HTA window, reply: "%HTAreply%"
goto :EOF
-->
<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >
<body bgcolor="cyan">
<TITLE>HTA Buttons</TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(374,300);
function sleepFor( sleepDuration ){
var now = new Date().getTime();
while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}
function callShellApplication(command){
var args=document.getElementById('args').value;
var r = new ActiveXObject("WScript.Shell");
var res=r.Exec(command +" "+args);
var out="";
while (res.Status == 0)
{
sleepFor(100);
}
while (!res.StdOut.AtEndOfStream){
out=out+"\r\n"+res.StdOut.ReadLine();
}
var StS=new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1);
StS.Write(out);
//new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(res.StdOut.ReadLine());
window.close();
}
</SCRIPT>
</HEAD>
<BODY>
Aruments:<textarea name="args" cols="40" rows="1"></textarea>
<hr>
<button onclick='callShellApplication("C:\\test.bat")'>Bat1</button>
<button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat2</button>
<button onclick='callShellApplication("C:\\Users\\user1\\Documents\\bat2.bat")'>Bat3</button>
</BODY>
</HTML>
关于batch-file - 用于运行批处理文件的 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49729727/