Search code examples
htmlvbscripthta

Capture HTA window close event using VBScript


I have been playing with using an hta as a means of getting familiar with HTML. I added code to minimize all windows when the hta window opened, but I have not been able to figure out how to restore all windows when the hta is closed. Any suggestions? (most of the code here is from a page I was reading on hta's)

<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>

Solution

  • Use 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>