问题
I have an HTA that is minimised while it carries out a backup. After this backup is complete I'd like to restore the HTA, but I'm having trouble.
I've tried a few things (below), but with no success. Is anyone able to point me towards a definitive solution?
First I tried to simply activate the HTA, but this failed. I'm no expert, but my understanding is that doing this should mimick a mouse click on the HTA in the task bar, thus restoring it -
Sub RestoreBackupHTA()
Shell.AppActivate "Backup"
End Sub
Next I tried to activate the HTA and then send the keys to "restore" the active window, but that also failed -
Sub RestoreBackupHTA()
Shell.AppActivate "Backup"
Shell.SendKeys "% r"
End Sub
Finally I tried to first activate the HTA and then send the keys to "restore" the active window after a short timeout (I found a couple of posts suggesting this could help to ensure that the HTA was fully active before sending the keys to restore it), but again, failure -
Sub RestoreBackupHTA()
Shell.AppActivate "Backup"
Call window.setTimeout("RestoreBackupHTAAfterWait", 500, "VBScript")
End Sub
Sub RestoreBackupHTAAfterWait()
'** Create a tempory Shell object (required as the global Shell object is out of scope for some reason *'
Dim tmpShell
Set tmpShell = CreateObject("Wscript.Shell")
tmpShell.SendKeys "% r" ' Restore the HTA
Set tmpShell = Nothing ' Destroy the tmpShell object
End Sub
Notes -
- The HTA has the ID "Backup" (
<HTA:APPLICATION ID="Backup" ... />, and so "Backup" is displayed in the title bar when it is running (and as the title in the task manager). This is why I'm using it as the
titleparameter for
Shell.AppActivate`, and according to this MSDN page that should be correct. Shell
is declared and set globally, thus it is available to theRestoreBackupHTA
procedure in the examples above.
回答1:
Hmm. It seemed to work for me. My window title shows the full path to the hta, so I just tried the following (after using setTimeout
):
With CreateObject("WScript.Shell")
.AppActivate("test.hta")
.SendKeys "% r"
End With
And didn't have any problems.
But here's another one way. You can use nircmd (always handy to have on hand).
CreateObject("WScript.Shell").Run "nircmd.exe win normal ititle Backup"
This will restore any windows with "Backup" in the title. You can filter many ways (title, class, PID, etc) if you need finer control.
回答2:
Because you are using HTA, you can make use of the SINGLEINSTANCE
parameter
<HTA:APPLICATION ID="RestoreMe" SINGLEINSTANCE="yes">
so that when you relaunch the HTA application it will trick windows to restore the existing HTA application:
Set objShell = CreateObject("WScript.Shell")
objShell.run location.href
To illustrate this, I created a mock HTA application which you must minimize within the first 5 seconds of launching it. After 5 seconds, it will restore itself:
<!DOCTYPE html>
<HEAD>
<TITLE>Restore Me</TITLE>
<HTA:APPLICATION ID="RestoreMe" SINGLEINSTANCE="yes">
<SCRIPT language="VBScript">
Sub RestoreMe
Set objShell = CreateObject("WScript.Shell")
objShell.run location.href
End Sub
Call window.setTimeout("RestoreMe", 5000)
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
来源:https://stackoverflow.com/questions/30801000/restore-a-minimised-application-via-vbs-within-an-hta