我正在使用VBScript在HTA中创建一个应用程序,它允许您为视频游戏选择一个文件目录,并将其保存到文件中。然后,您可以按“法语”或“德语”按钮,这将运行批处理文件,将游戏语言更改为法语或德语,这就是保存游戏目录以便批处理文件读取的原因。 / p>
当其中一个MsgBox出现并关闭时,应用程序的窗口会出错,背景变为空白,只显示“1”。我试过四处寻找,但我找不到这样的问题。我猜它试图显示错误或其他什么,虽然我真的不知道这意味着什么。
其中很多内容都是从互联网上复制而来并稍作修改。This is how the application looks like when you first start it, after you press French or German, and after you press OK or X on the popup
<html>
<head>
<title>SelectInput</title>
<hta:application id = "objHTA"
applicationName = "SelectInput"
border = "thin"
borderStyle = "normal"
caption = "yes"
maximizeButton = "no"
minimizeButton = "yes"
showInTaskbar = "yes"
scroll = "no"
singleInstance = "yes"
sysmenu = "yes"
version = "1.0"/>
</head>
<script language="vbscript">
'----------------------------------------------------------------------------------------------------------------------------
'Initialization Section
'----------------------------------------------------------------------------------------------------------------------------
Option Explicit
Dim objFSO, scriptBaseName
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
scriptBaseName = objFSO.GetBaseName(Wscript.ScriptFullName)
If Err.Number <> 0 Then
Wscript.Quit
End If
On Error Goto 0
CenterWindow
'----------------------------------------------------------------------------------------------------------------------------
'Functions Processing Section
'----------------------------------------------------------------------------------------------------------------------------
'Name : ProcessScript -> Primary Function that controls all other script processing.
'Parameters : None ->
'Return : None ->
'----------------------------------------------------------------------------------------------------------------------------
Function ProcessScript(buttonPressed)
Dim fileSpec, folderSpec
Dim objFSO, objFolder, objShell, objTextFile, objFile, Wsc
Dim strDirectory, strFile, strText
strDirectory = "C:\test"
strFile = "\directory.txt"
strText = folderSpec
Select Case LCase(buttonPressed)
Case "folder"
If Not SelectFolder(folderSpec) Then
Exit Function
Else
MsgBox "You selected " & folderSpec, vbInformation
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
WScript.Echo "Just created " & strDirectory & strFile
End If
set objFolder = nothing
set objFile = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForWriting = 2
set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForWriting, True)
' Writes strText every time you run this VBScript
objTextFile.WriteLine folderSpec
objTextFile.Close
' Bonus or cosmetic section to launch explorer to check file
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
End If
End Select
End Function
'----------------------------------------------------------------------------------------------------------------------------
'Name : CenterWindow -> Centers the HTA window vertically and horizontally in the middle of the screen.
'Parameters : None ->
'Return : None ->
'----------------------------------------------------------------------------------------------------------------------------
Function CenterWindow
Dim wmi, results, result
Dim displayWidth, displayHeight, x, y
On Error Resume Next
Set wmi = GetObject("winmgmts:\\.\root\cimv2")
Set results = wmi.ExecQuery("Select * From Win32_DesktopMonitor")
For Each result In results
displayWidth = result.ScreenWidth
displayHeight = result.ScreenHeight
Next
x = (displayWidth - 250) / 2
y = (displayHeight - 120) / 2
If x < 0 Or y < 0 Then
x = 0
y = 0
End If
window.resizeTo 500,500
window.moveTo x, y
On Error Goto 0
End Function
'----------------------------------------------------------------------------------------------------------------------------
'Name : SelectFolder -> Opens the shell object to allow the user to browse for and select the file on their computer.
'Parameters : folderSpec -> Output: The UNC folder path.
'Return : SelectFolder -> Returns an False or True and the UNC path of the file selected.
'----------------------------------------------------------------------------------------------------------------------------
Function SelectFolder(folderSpec)
Dim objShell, objFolder
SelectFolder = False
On Error Resume Next
Set objShell = CreateObject("Shell.Application")
If Err.Number <> 0 Then
Exit Function
End If
Set objFolder = objShell.BrowseForFolder(0, scriptBaseName, 0)
If Err.Number <> 0 Then
Exit Function
Else
folderSpec = objFolder.Self.Path
If Err.Number <> 0 Then
Exit Function
End If
End If
On Error Goto 0
SelectFolder = True
End Function
'----------------------------------------------------------------------------------------------------------------------------
'Name : SelectFile -> Opens a dialog box to allow the user select a file on their computer.
'Parameters : folderSpec -> Output: The UNC folder path.
' : fileName -> Output: The full file name including the folder path.
' : filter -> A string containing the filter for the UserAccounts.CommonDialog object.
'Return : SelectFile -> Returns an False or True and the name of the file selected.
'----------------------------------------------------------------------------------------------------------------------------
Function French
dim shell, answer
If objFSO.FileExists("C:\test\directory.txt") Then
set shell=createobject("wscript.shell")
shell.run "French.bat"
set shell=nothing
answer=MsgBox("Language is now French",0,"French")
document.write(answer)
Else
answer=MsgBox("Please select your game directory first",0,"Error")
document.write(answer)
End If
End Function
Function German
dim shell, answer
If objFSO.FileExists("C:\test\directory.txt") Then
set shell=createobject("wscript.shell")
shell.run "German.bat"
set shell=nothing
answer=MsgBox("Language is now French",0,"French")
document.write(answer)
Else
answer=MsgBox("Please select your game directory first",0,"Error")
document.write(answer)
End If
End Function
</script>
<body style = "font:8pt tahoma; color:white; background-color:#3A6EA5">
<table align = center width = "100%" height = "50" border = "0">
<caption>Please select a folder</caption>
<tr height = "25">
<td align = center> <input id = runbutton0 type = "button" name = "folderButton0" value = "Select Folder" onClick = 'ProcessScript("folder")'></td>
<td align = center> <input id = runbutton1 type = "button" name = "languageButton0" value = "French" onClick = 'French'></td>
<td align = center> <input id = runbutton1 type = "button" name = "languageButton1" value = "German" onClick = 'German'></td>
</tr>
</table>
</body>