使用VBScript捕获HTA窗口关闭事件

时间:2016-12-21 16:38:17

标签: html vbscript hta

我一直在玩hta作为熟悉HTML的一种方法。我在hta窗口打开时添加了代码以最小化所有窗口,但是当hta关闭时我无法弄清楚如何恢复所有窗口。有什么建议? (这里的大部分代码来自我正在阅读的页面

<html>
<head>
<title>processes</title>
<HTA:APPLICATION
  APPLICATIONNAME="processes"
  ID="processes"
  VERSION="1.0"/>
</head>

<script language="VBScript">

Sub Window_OnLoad
    Set objShell = CreateObject("shell.application")
    objShell.ToggleDesktop
    Dim width,height
    width=600
    height=800
    Set oShell = CreateObject("wscript.shell")
    oShell.SendKeys "% r"
    self.ResizeTo width,height
    self.MoveTo (screen.AvailWidth-width)/2,(screen.AvailHeight-height)/2

End Sub


    Sub GetProcesses

        strComputer = "."

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

        For Each objProcess in colProcesses
            strHTML = strHTML & objProcess.Name & " -- " & objProcess.ProcessID & "<br>"
        Next

        DataArea.InnerHTML = strHTML

    End Sub

</script>

<body bgcolor="white">

<input type="button" value="Processes" name="run_button"  onClick="GetProcesses">
<span id = "DataArea"></span>
</body>
</html>

1 个答案:

答案 0 :(得分:4)

使用window_unload

<html>
    <head>
    <title>processes</title>
    <HTA:APPLICATION
      APPLICATIONNAME="processes"
      ID="processes"
      VERSION="1.0"/>
    </head>

    <script language="VBScript">

    Sub Window_OnLoad
        Set objShell = CreateObject("shell.application")
        objShell.MinimizeAll
        Set objShell = Nothing
        Dim width,height
        width=600
        height=800
        Set oShell = CreateObject("wscript.shell")
        oShell.SendKeys "% r"
        self.ResizeTo width,height
        self.MoveTo (screen.AvailWidth-width)/2,(screen.AvailHeight-height)/2

    End Sub

    sub  Window_onUnload
        Set objShell = CreateObject("shell.application")
        objShell.UndoMinimizeALL
        Set objShell = Nothing
    end sub

    Sub GetProcesses

        strComputer = "."

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

        For Each objProcess in colProcesses
            strHTML = strHTML & objProcess.Name & " -- " & objProcess.ProcessID & "<br>"
        Next

        DataArea.InnerHTML = strHTML

    End Sub

    </script>

    <body bgcolor="white">

    <input type="button" value="Processes" name="run_button"  onClick="GetProcesses">
    <span id = "DataArea"></span>
    </body>
    </html>