如果从父 HTA 文件再次调用子 HTA 文件,则关闭子 HTA 文件的方法

Way to close child HTA file if it gets called again from a parent HTA file

我想要 运行 一个 HTA 文件,其中有一个循环,其中父 HTA 调用子 HTA 定期显示更新。我希望子 HTA 在使用旧更新时保持打开状态,并且在使用新更新再次调用它时应该关闭它并显示它。我尝试这样做,但无法在子 HTA 上添加关闭 HTA 条件。这导致所有 Child HTA 在后台打开。

父 HTA 文件,

代码如下

<html>
<head>
<title>Parent Application</title>
<HTA:APPLICATION
  APPLICATIONNAME="Parent Application"
  ID="ParentApplication"
  VERSION="1.0"/>
</head>

<script language="VBScript">

Sub OnClickButtonConnect()
  Dim currentDirectory,pos
  pos=InStrRev(document.location.pathname,"\")
  currentDirectory=""
  If pos>0 Then
    currentDirectory = Left(document.location.pathname,pos)
  End If

  Dim WshShell, i, g
  g = 5
  set WshShell = CreateObject("wscript.Shell")
  For i = 1 To g
  cmdline = "mshta.exe """ & currentDirectory & "child.hta"" """ & login.value & """ """ & password.Value & """"
  WshShell.Run cmdline,1,False
  next
  window.close
End Sub
</script>

<body bgcolor="white">

<!--Add your controls here-->

Login:<input type="text" name="login" id="login"><BR>
Password:<input type="password" name="password" id="password"><BR>
<input type="button" name="Connect" id="Connect" value="Connect" onclick="OnClickButtonConnect">
<!--{{InsertControlsHere}}-Do not remove this line-->
</body>
</html>

儿童 HTA

<html>
<head>
<title>Child Application</title>
<HTA:APPLICATION
  APPLICATIONNAME="Child Application"
  ID="ChildApplication"
  VERSION="1.0"/>
</head>

<script language="VBScript">

Sub Window_OnLoad
  str=""
  arguments = Split(ChildApplication.CommandLine," """)
  For i=0 To UBound(arguments)
    arguments(i)=Replace(arguments(i),"""","")
  Next
  document.body.innerhtml="login is: " & arguments(1) & "<BR>password is: " &  arguments(2)
End Sub

</script>

<body bgcolor="white">

<!--Add your controls here-->

<!--{{InsertControlsHere}}-Do not remove this line-->
</body>
</html>

在打开子 hta 之前调用此 Sub。确保 hta 的名称与其实际名称相符。

Sub CloseChild
    Set objWMIService = GetObject("winmgmts:\.\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery _
        ("Select CommandLine from Win32_Process where CommandLine like '%child.hta%'")
    For Each objProcess In colProcessList
        objProcess.Terminate()
    Next 
End Sub

编辑:我只是想为以后可能阅读此内容的任何人发表评论。将 CommandLine 放在 select 语句中并不是明确要求的,即使 属性 在 where 子句中使用也是如此。您可以 select Win32_Process class 中的任何或所有属性,包括 或排除 CommandLine

我确实建议 select 仅使用您需要的属性来提高查询速度,并且从历史上看,为了清楚起见,我 select 与我在中使用的 属性 相同where 子句,如果我实际上 不需要 一个。