アクティブにしたいHTAがシングルインスタンス制限を有効にしても良いならば、上記の方法より短いコードで同様の効果を得る方法がありました。
ソース
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<HTA:Application | |
applicationName="activate.hta" | |
SINGLEINSTANCE="yes" | |
> | |
<title>activate.hta</title> | |
</head> | |
<script> | |
resizeTo(400,150) | |
shell = new ActiveXObject('WScript.Shell') | |
function appActivate(){ setTimeout(main,3000) } | |
function main(){ | |
shell.run(location.href, 1, true) | |
setTimeout(check,1) | |
} | |
function check(){ | |
// appActivateを実行したにも関わらずアクティブになっていない場合は再起動する。 | |
if(document.hasFocus()){return} | |
shell.run(location.href) | |
window.close() | |
} | |
</script> | |
<body> | |
<button onclick=appActivate()>3秒後にアクティブにする</button> | |
</body> | |
</html> |
起動すると以下の画面が表示されます。
画面上のボタンを押して3秒以内に他のウィンドウを重ねると
main関数が実行されたタイミングで、ウィンドウがアクティブになります。
main実行時にウィンドウが最小化されている場合、ウィンドウがアクティブになりません。
その場合、document.hasFocus()の返り値がfalseになるので、WScript.ShellのRunで自身のPathを起動して、その起動完了を待たずに既存のインスタンスは終了します。
※最小化されている場合ウィンドウがアクティブにならないのはWScript.ShellのAppActivateメソッドも同じです。
最小化を解除する方法として試したことと、結果
返信削除・confirm → メイン画面が最小化状態の間は表示されない。最小化解除するとconfirmのウィンドウも表示される。
・alert → 同上
・window.showModalDialog → 同上
・VBScriptのmsgbox → msgboxのウィンドウだけは表示される。メインウィンドウは最小化状態のまま。
・window.open → ブラウザが起動するだけ。メインウィンドウは最小化状態のまま。
(new ActiveXObject('WScript.Shell')).popup('http://www.google.co.jp', 1, 123, 3) → msgboxと同じ結果。
返信削除