それが良いのか悪いのかは分かりませんが、とりあえず外部スクリプトを作成してそちらから実行するようにすれば動作するようです。
以下がサンプル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> | |
<body> | |
<button id=btn0>HTAのJavaScriptから</button><br> | |
<br> | |
<br> | |
<button id=btn1>HTAのVBScriptから①</button><br> | |
<br> | |
<br> | |
<button id=btn2>HTAのVBScriptから②</button><br> | |
<br> | |
<br> | |
<button id=btn3>jsから</button><br> | |
<br> | |
<br> | |
</body> | |
<script language=JavaScript> | |
getPID=function(){ | |
// 子プロセスを作ってそのPIDを取得し | |
shell = new ActiveXObject('WScript.Shell') | |
var childPID = shell.Exec('wscript //B').ProcessID | |
// 子の親(自身)のPIDを特定する | |
var res = new Enumerator(GetObject("winmgmts:root\\CIMV2").ExecQuery("SELECT * FROM Win32_Process where ProcessID="+childPID)) | |
return res.atEnd() ? (void 0) : res.item().ParentProcessID | |
} | |
// 起動後初回はなぜか必ず取得に失敗するので、最初に一度実行しておく。 | |
getPID() | |
PID = document.title = getPID() | |
btn0.onclick=function(){ | |
setTimeout(function(){ shell.AppActivate(PID) }, 3000) | |
} | |
btn1.onclick=function(){ | |
setTimeout(function(){ sub1() }, 3000) | |
} | |
btn2.onclick=function(){ | |
setTimeout(function(){ sub2() }, 3000) | |
} | |
共用=function(){ | |
shell = new ActiveXObject('WScript.Shell') | |
fs = new ActiveXObject('Scripting.FileSystemObject') | |
gsf2 = fs.GetSpecialFolder(2) | |
Read = function(path){var r=fs.OpenTextFile(path,1,true), v=r.AtEndOfStream?'':r.ReadAll(); r.Close(); return v} | |
Write = function(path,str){var w=fs.CreateTextFile(path); w.Write(str); w.Close()} | |
} | |
共用() | |
btn3.onclick=function(){ | |
var path=gsf2+'/AppActivate.js' | |
var fun=function(PID){ | |
shell.AppActivate(PID) | |
} | |
Write(path, [';('+共用+')();',';('+fun+')('+PID+');'].join('\r\n')) | |
setTimeout(function(){ shell.run(path) }, 3000) | |
} | |
</script> | |
<script language=VBScript> | |
sub sub1 | |
shell.AppActivate PID | |
end sub | |
sub sub2 | |
CreateObject("WScript.Shell").AppActivate(PID) | |
end sub | |
</script> | |
</html> |
各ボタンを押すと3秒後にAppActivateが実行されます。
AppActivateが正常に機能すれば、HTAのウィンドウがアクティブ状態になります。
ボタンを押してすぐに他のウィンドウをクリックするなどしてHTAウィンドウを隠した後、3秒後にHTAのウィンドウが表示されれば成功ですが、うまくいくのは「jsから」のボタンだけでした。
ちなみに、user32.dllのShowWindowというものも以下のように試してみましたが、動作しませんでした。

引数の順序と数が合っていないので駄目、ということなのだろうと思います。
0 件のコメント:
コメントを投稿