我目前正在处理 VBS 脚本,但我需要用户与脚本进行交互。基本上我需要两个按钮和 4 个复选框(复选框并不重要)。
问问题
35768 次
3 回答
7
VBScript 有对话框,只有不多且没有复选框,您需要一个 COM 对象来执行此操作(并且有)。我确定你知道 Messagebox,这里有一个鲜为人知的 Popup 示例
Dim WshShell, BtnCode
Set WshShell = WScript.CreateObject("WScript.Shell")
BtnCode = WshShell.Popup("Do you feel alright?", 7, "Answer This Question:", 4 + 32)
Select Case BtnCode
case 6 WScript.Echo "Glad to hear you feel alright."
case 7 WScript.Echo "Hope you're feeling better soon."
case -1 WScript.Echo "Is there anybody out there?"
End Select
但是,在 vbscript 中拥有更多对话框的最佳方法是使用 HTA。这里有一个例子
<HTML><HEAD>
<HTA:APPLICATION
ID = "oApp"
APPLICATIONNAME = "Example"
BORDER = "thick"
CAPTION = "yes"
ICON = "app.ico"
SHOWINTASKBAR = "yes"
SINGLEINSTANCE = "yes"
SYSMENU = "yes"
WINDOWSTATE = "normal"
SCROLL = "yes"
SCROLLFLAT = "yes"
VERSION = "1.0"
INNERBORDER = "yes"
SELECTION = "no"
MAXIMIZEBUTTON = "yes"
MINIMIZEBUTTON = "yes"
NAVIGABLE = "yes"
CONTEXTMENU = "yes"
BORDERSTYLE = "normal"
>
<SCRIPT language="vbscript">
sub SimpleExeample()
document.body.innerHTML = "<form name=myform><input type=checkbox name=chk1>Check me<br><br><button onclick='alert(myform.chk1.checked)'>Show if checked</button></form>"
end sub
</SCRIPT>
</HEAD>
<BODY onLoad="SimpleExeample()">
</BODY>
</HTML>
有一点我同意 Cody 的观点,如果您开始编程,请选择另一种语言,vbscript 几乎已死。看看 Ruby,开始很容易学习,而且很有趣。这是一个使用鞋子作为 GUI 的 ruby 脚本示例
require 'green_shoes'
Shoes.app{
button("Click me!"){alert("You clicked me.")}
}
编辑:由于我的 Ruby 替代方案提出了一些问题,因此这里采用更传统的方式更接近 Vbscript 对相同样本的使用。上面的示例更多地用于功能链式编程方式。
require 'green_shoes'
Shoes.app do
button("Click me!") do
alert("You clicked me.")
end
end
于 2013-04-17T07:23:12.357 回答
0
我发现的最佳方法是使用“WScript.Shell Run”从 VBS 打开 HTA 文件,并使用 XML 文件与 VBS 进行通信。
示例(dialog.vbs)
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.GetFile(Wscript.ScriptFullName)
sFolder = oFile.ParentFolder
sHtaFilePath = sFolder & "\dialog.hta"
Dim oShell: Set oShell = CreateObject("WScript.Shell")
oShell.Run sHtaFilePath, 1, True
'Load return data from XML File
If fso.FileExists(sHtaFilePath & ".xml") Then
Set oXml = CreateObject("Microsoft.XMLDOM")
oXML.async = False
oXML.load sHtaFilePath & ".xml"
MsgBox "Value 1: " & oXML.SelectSingleNode("/root/txt1").text
MsgBox "Value 2: " & oXML.SelectSingleNode("/root/txt2").text
fso.DeleteFile sHtaFilePath & ".xml"
End If
示例 HTA (dialog.hta)
<html>
<title>Test</title>
<head>
<HTA:APPLICATION
ID=oHTA
SINGLEINSTANCE="yes"
SCROLL="no"
/>
</head>
<script language="vbscript">
Window.ResizeTo 300, 200
Set fso = CreateObject("Scripting.FileSystemObject")
Sub Send()
Dim sFilePath: sFilePath = Replace(location.href,"file:///","")
sFilePath = Replace(sFilePath,"/","\")
sFilePath = Replace(sFilePath," %20"," ")
'Save return date to XML File
Set oXml = CreateObject("Microsoft.XMLDOM")
Set oRoot = oXml.createElement("root")
oXml.appendChild oRoot
AddXmlVal oXml, oRoot, "txt1", txt1.value
AddXmlVal oXml, oRoot, "txt2", txt2.value
oXml.Save sFilePath & ".xml"
self.Close()
End Sub
Sub AddXmlVal(oXml, oRoot, sName, sVal)
Set oNode = oXml.createElement(sName)
oNode.Text = sVal
oRoot.appendChild oNode
End Sub
</script>
<body>
<div>
Value 1
<input id="txt1" style="width: 100px;">
</div>
<div>
Value 2
<select id=txt2 style="width: 100px;">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">D</option>
</select>
</div>
<p align=center>
<input type="button" value="Send" onclick="Send()">
<input type="button" value="close" onclick="self.Close()">
</p>
</body>
</html>
更好的方法是由 VBS 文件创建 HTA 文件,然后将其删除。这样,HTA 就不必与 VBS 一起分发。
Set fso = CreateObject("Scripting.FileSystemObject")
sHtml = "<div>Value 1 " & _
"<input id='txt1' style='width: 100px'>" & _
"</div>" & _
"<div>Value 2 " & _
"<select id='txt2' style='width: 100px'>" & _
"<option value='A'>A</option>" & _
"<option value='B'>B</option>" & _
"<option value='C'>D</option>" & _
"</select>" & _
"</div>" & _
"<p align=center>" & _
"<input type='button' value='Send' onclick='Send()'> " & _
"<input type='button' value='Close' onclick='self.Close()'>" & _
"</p>"
Set oRet = OpenDialog(sHtml, "txt1,txt2", 300, 200, "Dialog 1")
MsgBox "Value 1: " & oRet("txt1") & ", Value 2: " & oRet("txt2")
'==================================
Function OpenDialog(sHtml, sFields,iWidth,iHeight, sTitle)
sHtaFilePath = Wscript.ScriptFullName & ".hta"
CreateHtaFile sHtaFilePath, sHtml, sFields,iWidth,iHeight,sTitle
Set f = fso.GetFile(sHtaFilePath)
f.attributes = f.attributes + 2 'Hidden
Dim oShell: Set oShell = CreateObject("WScript.Shell")
oShell.Run """" & sHtaFilePath & """", 1, True
If fso.FileExists(sHtaFilePath) Then
fso.DeleteFile sHtaFilePath
End If
Set oRet = CreateObject("Scripting.Dictionary")
'Load return data from XML File
If fso.FileExists(sHtaFilePath & ".xml") Then
Set oXml = CreateObject("Microsoft.XMLDOM")
oXML.async = False
oXML.load sHtaFilePath & ".xml"
For each sField In Split(sFields,",")
oRet.Add trim(sField), oXML.SelectSingleNode("/root/" & trim(sField)).text
Next
fso.DeleteFile sHtaFilePath & ".xml"
End If
Set OpenDialog = oRet
End Function
Sub CreateHtaFile(sHtaFilePath, sHtml, sFields, iWidth, iHeight, sTitle)
Set f = fso.CreateTextFile(sHtaFilePath, True)
f.WriteLine "<html><title>FL Reporting</title><head><HTA:APPLICATION ID=oHTA SINGLEINSTANCE=""yes"" SCROLL=""no""/></head>"
f.WriteLine "<script language=""vbscript"">"
f.WriteLine "Window.ResizeTo " & iWidth & ", " & iHeight
f.WriteLine "Set fso = CreateObject(""Scripting.FileSystemObject"")"
f.WriteLine ""
f.WriteLine "Sub Send()"
f.WriteLine " Dim sFilePath: sFilePath = Replace(location.href,""file:///"","""")"
f.WriteLine " sFilePath = Replace(sFilePath,""/"",""\"")"
f.WriteLine " sFilePath = Replace(sFilePath,""%20"","" "")"
f.WriteLine " Set oXml = CreateObject(""Microsoft.XMLDOM"")"
f.WriteLine " Set oRoot = oXml.createElement(""root"")"
f.WriteLine " oXml.appendChild oRoot"
For each sField In Split(sFields,",")
f.WriteLine " AddXmlVal oXml, oRoot, """ & sField & """, GetVal(" & sField & ")"
Next
f.WriteLine " oXml.Save sFilePath & "".xml"""
f.WriteLine " self.Close()"
f.WriteLine "End Sub"
f.WriteLine ""
f.WriteLine "Sub AddXmlVal(oXml, oRoot, sName, sVal)"
f.WriteLine " Set oNode = oXml.createElement(sName)"
f.WriteLine " oNode.Text = sVal"
f.WriteLine " oRoot.appendChild oNode"
f.WriteLine "End Sub"
f.WriteLine ""
f.WriteLine "Function GetVal(o)"
f.WriteLine " GetVal = o.value"
f.WriteLine " If o.Type = ""checkbox"" Then"
f.WriteLine " If o.checked = False Then"
f.WriteLine " GetVal = """""
f.WriteLine " End If"
f.WriteLine " End If"
f.WriteLine "End Function"
f.WriteLine "</script>"
f.WriteLine "<body>"
f.WriteLine sHtml
f.WriteLine "</body></html>"
f.Close
End Sub
于 2019-07-13T05:33:43.997 回答
0
今天,我想介绍两个你会感兴趣的项目。
- wsh-js-gtk - 使用GTK-server从 JScript/VBScript 制作您自己的 GUI
- welsonjs - 使用 ES6/HTML5 兼容 polyfills、 module.exports (NodeJS) 样式的模块实现构建基于 WSH/HTA 的 Windows 桌面应用程序。
于 2020-07-20T09:08:43.177 回答