2013-04-26 38 views
1

使用诸如“对话框”之类的工具找到显示GUI对话框的bash脚本是很常见的。有没有办法从Windows批处理文件启动对话框?

enter image description here

我想写这可以通过某种GUI元素像一些按钮的输入框的用户交互的微软Windows批处理文件。什么是更容易/更快的方式来做到这一点?

+0

注意:我在寻找一个本地的解决方案。不应该使用Cygwin或其他外部工具链。 – 2013-04-26 07:27:14

+0

http://stackoverflow.com/questions/3367265/gui-using-batch – Freak 2013-04-26 07:27:26

+0

你的本地需求 - 是因为你不希望有安装*你* PC上的任何东西,或在*用户*?有免费和开源的方式来做到这一点,产生一个简单的.exe没有其他运行时的需要,可以像批处理文件一样容易地分发。 – 2013-04-27 07:39:54

回答

1

那么,答案取决于你认为“本地解决方案。”下面的批处理文件使用所有现代Windows版本中包含的Cscript.exe外部命令,所以我认为它符合“本地解决方案”的条件。但是,它确实是一个混合脚本,它包含一个JScript部分,它从批处理部分获取参数,激活弹出式GUI对话框,并将结果作为错误级别返回给批处理代码。

@if (@CodeSection == @Batch) @then 

@echo off 

rem Popup.bat: Example of use of Popup JScript method 
rem Antonio Perez Ayala 
rem http://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx 

rem Include auxiliary values for Popup JScript method 
call Popup.inc 

rem Call Popup JScript method with a 7 second timeout. 
set /A buttons=YesNoandCancel + QuestionMark 
CScript //nologo //E:JScript "%~F0" "Question:" "Do you feel alright?" /B:%buttons% /T:7 
set btn=%errorlevel% 
if %btn% equ %YesButton% (
    rem Yes button pressed. 
    echo Glad to hear you feel alright. 
) else if %btn% equ %NoButton% (
    rem No button pressed. 
    echo Hope you're feeling better soon. 
) else if %btn% equ %TimedOut% (
    rem Timed out. 
    echo Is there anybody out there? 
) 

goto :EOF 

End of Batch section 


@end 


// JScript section 

// Displays text in a pop-up message box. 
// CScript //nologo //E:JScript "%~F0" ["Title" ["Text"]] [/B:ButtonsType] 

[/T:SecondsToWait] 
// set ButtonClicked=%errorlevel% 

var title = "", text = "", buttons = 0, seconds = 0; 
var args = WScript.Arguments; 
if (args.Unnamed.Length >= 1) title = args.Unnamed.Item(0); 
if (args.Unnamed.Length >= 2) text = args.Unnamed.Item(1); 
if (args.Named.Exists("B")) { 
    buttons = parseInt(args.Named.Item("B")); 
} 
if (args.Named.Exists("T")) { 
    seconds = parseInt(args.Named.Item("T")); 
} 
var WshShell = WScript.CreateObject("WScript.Shell"); 
WScript.Quit(WshShell.Popup(text,seconds,title,buttons)); 

Popup JScript方法显示一个需要用户回复的弹出消息框。此方法在ButtonsType参数和buttonClicked返回值中使用某些数值;

rem Popup.inc.bat: Define auxiliary variables for Popup JScript method 
rem Antonio Perez Ayala 


rem Button Types 

set i=0 
for %%a in (OK OKandCancel  AbortRetryandIgnore 
       YesNoandCancel YesandNo 
       RetryandCancel CancelTryAgainandContinue) do (
    set %%a=!i! 
    set /A i+=1 
) 

rem Icon Types 

set i=16 
for %%a in (StopMark QuestionMark ExclamationMark InformationMark) do (
    set %%a=!i! 
    set /A i+=16 
) 

rem Default Button 

set i=256 
for %%a in (DefaultButton2 DefaultButton3) do (
    set %%a=!i! 
    set /A i+=256 
) 

rem Button Clicked 

set TimedOut=-1 
set i=1 
for %%a in (OKButton CancelButton AbortButton  RetryButton IgnoreButton 
      YesButton NoButton _ _ TryAgainButton ContinueButton) do (
    set %%a=!i! 
    set /A i+=1 
) 

set _= 
set i= 


rem Popup.inc.bat: End of file 

一个JScript程序可以通过DynamicWrapperX使用其它类型的的Win-32的对话框: http://www.script-coding.com/dynwrapx_eng.html

+0

这不是批次 – 2013-04-28 07:42:58

+0

这正是我的观点!此解决方案仅包含一个带.BAT扩展名的文件,并使用外部命令(Cscript。exe)比其他批量命令(如Choice或Forfiles,不包含在Win XP中)具有更广泛的分布。从这个角度来看,这是一个“本地批量解决方案”。当然,我必须同意这不是“纯粹的批量解决方案”。 ;-) – Aacini 2013-04-28 16:48:27

1

Windows批处理中没有任何东西可以让您构建GUI。

1

如果您可以通过调用它的同伴Popup.inc.bat文件定义包含这些值的辅助变量这是可以接受的GUI界面弹出一个新窗口,而不是被约束到命令提示符窗口,看看HTML应用程序(HTA)
http://en.wikipedia.org/wiki/HTML_Application

这是标准的Windows技术,采用IE引擎在类似浏览器的窗口(无工具栏)中呈现HTML,并提供您可能需要的所有GUI优势。

很好的介绍在这里 - “改头换面:在GUI界面向上包住脚本”
http://technet.microsoft.com/en-us/library/ee692768.aspx

0

虽然我与大卫同意,没有什么批量本身,有一个解决方案,完全是天然的和只需要一个批处理文件就可以工作(无需安装/非本地工具)它需要在文件系统运行时才能对文件系统进行写入访问。

@ECHO OFF 

SET WINDOWTITLE="Enter Ip Address" 
SET QUESTION="Answer the question being asked" 
SET DETAIL="Description" 
SET DEFAULTANSWER="10.45.76.156" 

REM name the vbs file the same as this batch file except .vbs 
set FILENAME=%~n0 
REM echo the vbs 
    echo MSG = InputBox(%QUESTION% ^& VBCRLF ^& VBCRLF ^& VBCRLF ^& %DETAIL%, %WINDOWTITLE%, %DEFAULTANSWER%) >> "%~dp0\%FILENAME%.vbs" 
    echo CreateObject("Scripting.FileSystemObject").OpenTextFile("%~dp0\answer.txt",2,True).Write MSG >> "%~dp0\%FILENAME%.vbs" 
cscript "%~dp0\%FILENAME%.vbs" 
del "%~dp0\%FILENAME%.vbs" 

SET /P answer=<"%~dp0\answer.txt" 

del "%~dp0\answer.txt" 

echo you typed "%answer%" 
echo. 
pause 
相关问题