2009-05-28 49 views
4

我有一個第三方應用程序調用一個vsbscript文件進行某些操作。我想提出一個用戶提示,可以選擇一個選項,可以是下拉列表或複選框等。但是,我所能找到的只是輸入框選項。如何使用VbScript創建選項對話框?

我不認爲HTA的是在我的情況選擇(除非有辦法從.vbs文件給他們打電話?)

我其他的想法是某種形式的ActiveX控件,但我可以」找到一個默認在WindowsXP/Vista上可用的內置驅動程序。

任何人有什麼想法,我怎麼能做到這一點?

+0

你能編輯第三方VBScript嗎?如果你能給我們更多的細節,我們可能會爲你提供更好的選擇。 – aphoria 2009-06-01 13:47:32

+0

是的,編輯vbscript正是我們正在做的。如果第三方產品存在,它會自動調用它。 – Nathan 2009-06-01 14:22:44

回答

3

簡單的答案是,你真的不能。 Tmdean的解決方案是我能想到的唯一方法。這就是說,你可以調整輸入框,所以它不看可怕的。給這個運行,我不認爲這是一個史詩失敗:

Dim bullet 
Dim response 
bullet = Chr(10) & " " & Chr(149) & " " 
Do 
    response = InputBox("Please enter the number that corresponds to your selection:" & Chr(10) & bullet & "1.) Apple" & bullet & "2.) Bannana" & bullet & "3.) Pear" & Chr(10), "Select Thing") 
    If response = "" Then WScript.Quit 'Detect Cancel 
    If IsNumeric(response) Then Exit Do 'Detect value response. 
    MsgBox "You must enter a numeric value.", 48, "Invalid Entry" 
Loop 
MsgBox "The user chose :" & response, 64, "Yay!" 
1

嘗試WshShell.Popup。根據您的數據可能適用於您...

否則,您可以調查PowerShell。

+1

如果我正確理解WshShell.Popup,那不會給我創建我自己的任意列表的選項,例如「Queue1,Queue2,Queue3」等,所以我沒有看到它給我帶來了不同於常規輸入框(http://wsh2.uw.hu/ch08b.html) – Nathan 2009-05-28 23:02:00

1

一個選項是腳本Internet Explorer。您可以使用VBScript啓動IE並加載本地HTML文件,並將VBScript sub附加到表單的提交按鈕(或任何其他JavaScript事件),然後可以在執行過程中關閉IE窗口。

1

您可以從一個VBScript啓動一個HTA。

Set shell = CreateObject("WScript.Shell") 
shell.Run "Test.hta" 

編輯

既然你擁有的VBScript的完全控制,你可以讓第三方的VBScript只需撥打您的HTA?您可以將UI和任何處理代碼放入HTA中。

3

如果您想使用hta來做到這一點,可以這樣做。
VBScript的:


Set WshShell = CreateObject("WScript.Shell") 
'Run the hta. 
WshShell.Run "Test.hta", 1, true 
'Display the results. 
MsgBox "Return Value = " & getReturn 
Set WshShell = Nothing 

Function getReturn 
'Read the registry entry created by the hta. 
On Error Resume Next 
    Set WshShell = CreateObject("WScript.Shell") 
    getReturn = WshShell.RegRead("HKEY_CURRENT_USER\Volatile Environment\MsgResp") 
    If ERR.Number 0 Then 
     'If the value does not exist return -1 
     getReturn = -1 
    Else 
     'Otherwise return the value in the registry & delete the temperary entry. 
     WshShell.RegDelete "HKEY_CURRENT_USER\Volatile Environment\MsgResp" 
    End if 
    Set WshShell = Nothing 
End Function 

然後根據需要設計HTA,幷包括以下方法



'Call this when the OK button is clicked. 
Sub OK_Click 
    For Each objradiobutton In Opt 
     If objradiobutton.Checked Then 
       WriteResponse objradiobutton.Value 
     End If 
    Next 
    window.Close 
End Sub 

'Call this when the Cancel button is clicked. 
Sub Cancel_Click 
    WriteResponse("CANCEL") 
    window.Close 
End Sub 

'Write the response to the registry 
Sub WriteResponse(strValue) 
    Set WshShell = CreateObject("WScript.Shell") 
    WshShell.RegWrite "HKEY_CURRENT_USER\Volatile Environment\MsgResp", strValue 
    Set WshShell = Nothing 
End Sub 

我使用的一組名爲「選項」單選按鈕做出選擇,但你可以使用你想要的任何控件。

因爲hta's不能返回值,所以這將創建一個temperary註冊表項。如果您對註冊表不滿意,也可以將結果寫入文本文件。

這種方法很好,因爲你可以用你喜歡的任何方式設計hta,而不是使用提供的輸入框和選擇數字(多數民衆贊成在DOS)。

如果您根據傳遞給它的參數來擴展hta以創建自己,例如傳入標題,顯示消息,選項數組和一組按鈕,這也可能很好。這樣你就可以在任何時候使用相同的hta來獲得用戶的輸入。

1

正如@ TmDean的建議的一個例子,有這個類,我有時會使用哪些腳本IE(當然,它照本宣科IE6,我沒有嘗試過更近的化身。)

class IEDisplay 
    '~ Based on original work by Tony Hinkle, [email protected] 

    private TEMPORARY_FOLDER 

    private objShell 
    private objIE 
    private objFSO 
    private objFolder 
    private strName 
    private streamOut 
    private objDIV 

    private numHeight 
    private numWidth 
    private numTop 
    private numLeft 

    private sub Class_Initialize() 
    Dim strComputer 
    Dim objWMIService 
    Dim colItems 
    Dim objItem 
    Dim arrMonitors(10, 1) 
    Dim numMonitorCount 

    Set objShell = WScript.CreateObject("WScript.Shell") 
    Set objIE = CreateObject("InternetExplorer.Application") 

    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
    Set colItems = objWMIService.ExecQuery("Select * from Win32_DesktopMonitor") 

    numMonitorCount = 0 
    For Each objItem in colItems 
     arrMonitors(numMonitorCount, 0) = objItem.ScreenHeight 
     arrMonitors(numMonitorCount, 1) = objItem.ScreenWidth 
     numMonitorCount = numMonitorCount + 1 
    Next 

    numHeight = arrMonitors(0, 0) 
    numWidth = arrMonitors(0, 1) 

    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    TEMPORARY_FOLDER = 2 
    set objFolder = objFSO.GetSpecialFolder(TEMPORARY_FOLDER) 
    strName = objFSO.BuildPath(objFolder, objFSO.GetTempName) & ".html" 
    WriteFileU strName, Join(Array("<HTML><HEAD><TITLE>Information</TITLE></HEAD>", _ 
        "<BODY SCROLL='NO'><CENTER><FONT FACE='arial black'> <HR COLOR='BLACK'>", _ 
        "<DIV id='MakeMeAnObject'></DIV>", _ 
        "<HR COLOR='BLACK'></FONT></CENTER></BODY></HTML>"), vbCRLF), WF_CREATE   
    numTop = 0 
    numLeft = 0 
    end sub 

    Sub Init(strPosition) 
    'NW, N, NE, W, CENTRE, E, SW, S, SE 
    Select Case strPosition 
    Case "NW" 
     numTop = 0 
     numLeft = 0 
    Case "N" 
     numTop = 0 
     numLeft = (numWidth/2) - 250 
    Case "NE" 
     numTop = 0 
     numLeft = numWidth - 500 
    Case "W" 
     numTop = (numHeight/2) - 55 
     numLeft = 0 
    Case "CENTRE" 
     numTop = (numHeight/2) - 55 
     numLeft = (numWidth/2) - 250 
    Case "E" 
     numTop = (numHeight/2) - 55 
     numLeft = numWidth - 500 
    Case "SW" 
     numTop = numHeight - 110 
     numLeft = 0 
    Case "S" 
     numTop = numHeight - 110 
     numLeft = (numWidth/2) - 250 
    Case "SE" 
     numTop = numHeight - 110 
     numLeft = numWidth - 500 
    Case Else 
     numTop = 0 
     numLeft = 0 
    End Select 

    SetupIE(strName) 
    Set objDIV = objIE.Document.All("MakeMeAnObject") 
    end sub 

    private sub Class_Terminate() 
    'Close IE and delete the file 
    objIE.Quit 
    '~ optionally you may want to get rid of the temp file 
    end sub 

    public sub Display(strMsg, numMillisec) 
    objDIV.InnerHTML = strMsg 
    WScript.Sleep numMillisec 
    end sub 

    Private Sub SetupIE(File2Load) 
    objIE.Navigate File2Load 
    objIE.ToolBar = False 
    objIE.StatusBar = False 
    objIE.Resizable = False 

    Do 
    Loop While objIE.Busy 

    objIE.Width = 500 
    objIE.Height = 110 
    objIE.Left = numLeft 
    objIE.Top = numTop 
    objIE.Visible = True 
    objShell.AppActivate("Microsoft Internet Explorer") 
    End Sub 

end class 

這裏是失蹤(從原發布)WriteFileU功能

Const WF_APPEND = 1 
Const WF_CREATE = 2 

Const WF_FOR_APPENDING = 8 
Const WF_FOR_WRITING = 2 
Const WF_CREATE_NONEXISTING = True 

Const CONST_READ = 1, CONST_WRITE = 2, CONST_APPEND = 8 

Const AS_SYSTEMDEFAULT = -2, AS_UNICODE = -1, AS_ASCII = 0 

Sub WriteFileU(sFilename, sContents, nMode) 
    Dim oStream 
    If nMode = WF_APPEND Then 
    Set oStream = oFSO.OpenTextFile(sFilename, WF_FOR_APPENDING, WF_CREATE_NONEXISTING, AS_UNICODE) 
    ElseIf nMode = WF_CREATE Then 
    Set oStream = oFSO.OpenTextFile(sFilename, WF_FOR_WRITING, WF_CREATE_NONEXISTING, AS_UNICODE) 
    Else 
    STOP 
    End If 

    oStream.Write sContents 
    oStream.Close 
    Set oStream = Nothing 
End Sub 

,然後作爲它的一個例子是使用

set i = new IEDisplay 
a = array("NW", "N", "NE", "W", "CENTRE", "E", "SW","S","SE") 
for each aa in a 
    i.init aa 
    i.display "Here in " & aa & " of screen", 1000 
next 

現在這不是很有用(特別是有一堆對我自己的實用程序的調用),但它提供了一個框架。通過修改存儲的HTML,您可以添加對列表框的支持等。

相關問題