2013-04-26 35 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 
相關問題